HI im trying to added extra fields to my register.php , but i keep getting a error message saying 'Error:user not added to database'

I've simply tried to edit the original code using the same methods used for the previous fields yet it still doesn't work. im sure my mysql database is setup fine i just think its got something to do with how i edited the code can somebody please help...thanks

Ive commented out the parts ive added with '!!!ADDED!!!' along side...hope somebody can help me.


Thanks

<?php
// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("_mmServerScripts/dbConfig.php");
 
 
//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
 {
 $bInputFlag = false;
 foreach ( $_POST as $field )
  {
  if ($field == "")
   {
   $bInputFlag = false;
   }
  else
   {
   $bInputFlag = true;
   }
  }
 // If we had problems with the input, exit with error
 if ($bInputFlag == false)
  {
  die( "Problem with your registration info. "
   ."Please go back and try again.");
  }
 
 // Fields are clear, add user to database
 //  Setup query
 $q = "INSERT INTO `dbUsers` (`username`,`email`,`password`)"           /*`firstname`,`lastname`,`company`,) "        !!!ADDED!!! */
  ."VALUES ('".$_POST["username"]."', "
					  
  
  /*."VALUES ('".$_POST["firstname"]."', "   		CANT EXCUTE MORE FIELDS TO SAVE DATA				!!!ADDED!!!!
  ."VALUES ('".$_POST["lastname"]."', "
  ."VALUES ('".$_POST["company"]."', "*/
					  
					  
  ."PASSWORD('".$_POST["password"]."'), "
  ."'".$_POST["email"]."')";
 //  Run query
 $r = mysql_query($q);
 
 // Make sure query inserted user successfully
 if ( !mysql_insert_id() )
  {
  die("Error: User not added to database.");
  }
 else
  {
  // Redirect to thank you page.
Header("Location: register.php?op=thanks");
  }
 } // end if
 
 
//The thank you page
elseif ( $_GET["op"] == "thanks" )
 {
 echo "<h2> Thanks for registering!</h2> ";
 }
 
//The web form for input ability
else
 {
 echo "<form action=\"?op=reg\" method=\"POST\"> \n";
 echo "Username: <input name=\"username\" MAXLENGTH=\"16\"> <br /> \n";
 
 /*echo "First Name: <input name=\"firstname\" MAXLENGTH=\"16\"> <br /> \n";						!!!ADDED!!!
 echo "Last Name: <input name=\"lastname\" MAXLENGTH=\"16\"> <br /> \n";
 echo "Company: <input name=\"company\" MAXLENGTH=\"20\"> <br /> \n";*/
 
 echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"> <br /> \n";
 echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"> <br /> \n";
 echo "<input type=\"submit\"> \n";
 echo "</form> \n";
 }
// EOF
?>

Dani AI

Generated

Quick checklist before changing code: confirm the new columns actually exist in the dbUsers table (as pointed out), make sure the HTML form uses the exact name= attributes you expect, and avoid leaving commented fragments inside the SQL string — those commonly break the generated query. Also note the original input loop toggles a flag per field so it only reflects the last field checked; that logic needs changing. Posting in the PHP section (as suggested) is a good idea for deeper PHP-specific help.

Concrete fixes and a modern example:

  • Validation: require each POST value and break early on the first empty one.
  • Insert: the column list must match the VALUES order, or use named placeholders so order does not matter.
  • Passwords: do not use MySQLs PASSWORD() for user auth; use PHPs password_hash() and store the hash.
  • Use prepared statements (PDO or mysqli) to avoid SQL injection.

Example (PDO + password_hash):

$hash = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt = $pdo->prepare(
  "INSERT INTO dbUsers (username,email,password,firstname,lastname,company)
   VALUES (:username,:email,:password,:firstname,:lastname,:company)"
);
$stmt->execute([
  ':username'=>$_POST['username'],
  ':email'=>$_POST['email'],
  ':password'=>$hash,
  ':firstname'=>$_POST['firstname'],
  ':lastname'=>$_POST['lastname'],
  ':company'=>$_POST['company']
]);

Debugging tips and references: if an insert fails, output the DB error while debugging (or use PDO exceptions). Check for UNIQUE constraints that silently prevent inserts. For background on password_hash() and prepared statements see the PHP manual password_hash and PDO prepared statements. For SQL syntax reference see MySQL INSERT docs INSERT. For secure password storage guidance see the OWASP Password Storage Cheat Sheet.

Recommended Answers

All 2 Replies

have you setup the additional rows in the database?

You should try posting this in the php section on the forum. You might have more success.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.