Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Could someone debug some php for me plz


ghost's Avatar
0 0

Ok, I was working on the CMS for other challenges, and realized that when it came to SQL I knew very little. My first script here worked, but is bare and doesn't prevent from inserting blank values:

<?php
$username=$_POST['username'];
$password=$_POST['password'];
$con = mysql_connect("localhost:phpmyadmin","censor","censor");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("slpctrl_users", $con);

mysql_query("INSERT INTO users (username, password) 
VALUES ('$username', '$password')");

mysql_close($con);
?>

Now as I said, that works. But when I try to add a few extra things, I.E. disallowing blank fields and using addslashes to prevent a SQL injection here:

<?php
$con = mysql_connect("localhost:phpmyadmin","censor","censor");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if( isset( $_POST['username'] ) && isset( $_POST['password'] ) )
{
$password=addslashes($_POST['password']);
$username=addslashes($_POST['username']);
}
else
{
echo ("Username or Password not set");
}

mysql_select_db("slpctrl_users", $con);

mysql_query("INSERT INTO users (username, password) 
VALUES ('$username', '$password')");

mysql_close($con);
?>

Anyone see an error I'm missing?


Mr_Cheese's Avatar
0 1

if( isset( $_POST['username'] ) && isset( $_POST['password'] ) )


if( isset( $_POST['username'] ) && $_POST['username'] != "" && isset( $_POST['password'] ) && $_POST['password'] != "" )

^^ that'll stop blank entries.


ghost's Avatar
0 0

Use mysql_real_escape_string() instead of addslashes(), it's safer.


ghost's Avatar
0 0

Alright, good shit. I was just wondering about 1 more question that I can't seem to find. Is it possible for me to count the number of columns, so that I can identify the column number with the order in which people 'register'? I.E. In the first column is my info….column 1. Or do I have to add another row for that? Btw mr cheese or any admin, don't accept my first CMS, I did a half assed job on it and tried to hurry through it but now I want to actually make a decent one with an interface :D