Pls i dont relly want to understand what is really wrong with the code below!
I want to insert record into the database i have written one and it is working well for two record but for these is not working. And more so it work anytime it like it there anything that i am doing wrong cos sometime one record may be blank and the other fill in or vise versa
I collect the data from a form

<table width="100%" border="0" cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      
      <tr>
        <td width="3" height="1"></td>
          <td width="184"></td>
          <td width="336" rowspan="11" valign="top"><form id="form1" name="form1" method="post" action="reg_forminsert.php">
              <input type="text" name="Username" />
              <br />
              <input type="text" name="Password" />
              <br />
              <input type="text" name="Confirmpass" />
              <br />
              <input type="text" name="Name" />
              <br />
              <input type="text" name="Comment" />
              <input type="submit" name="Submit" value="Submit" />
              <br />
          </form></td>
        </tr>
      <tr>
        <td height="19"></td>
          <td valign="top">Username</td>
        </tr>
      <tr>
        <td height="4"></td>
          <td></td>
        </tr>
      
      <tr>
        <td height="19"></td>
          <td valign="top">Password</td>
        </tr>
      <tr>
        <td height="4"></td>
          <td></td>
        </tr>
      
      
      
      <tr>
        <td height="19"></td>
          <td valign="top">Confirm Password </td>
        </tr>
      <tr>
        <td height="4"></td>
          <td></td>
        </tr>
      
      
      
      
      
      
      
      <tr>
        <td height="19"></td>
          <td valign="top">Name</td>
        </tr>
      <tr>
        <td height="2"></td>
          <td></td>
        </tr>
      
      <tr>
        <td height="19"></td>
          <td valign="top">Comment</td>
        </tr>
      <tr>
        <td height="2"></td>
          <td></td>
        </tr>
      
      <tr>
        <td height="44"></td>
          <td></td>
          <td></td>
        </tr>
      
      
      
    </table>

reg_forminser.php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("forum", $con);
echo "connection successful";

mysql_query("INSERT INTO 'forum'.'reg_info' (Username,Password,Comfirm_pass,Name,Comment) 
VALUES ('$Username', '$Password', '$Confirmpass', '$Name', '$Comment')");

echo "Record Inserted";
mysql_close($con);

Dani AI

Generated

Three likely causes explain the symptoms in ’s original post (sometimes working, sometimes blank fields): the script is using uninitialized PHP variables instead of the POST array, SQL identifiers are quoted incorrectly, and the selected database/table or column names may not match (typos or wrong DB name). was on the right track to use POST values, and ’s note about quotes/datatypes is also relevant — the intermittent behaviour is a classic sign of relying on register_globals on some systems (that can make $Username appear to exist sometimes) rather than always reading from $_POST. See .

Quick, practical debugging steps to apply immediately:

  • Turn on error reporting and inspect what the form actually sends.
  • Dump the incoming POST array to confirm exact field names and values (case and underscore differences matter).
  • Check the return value of the INSERT and print the database error message when it fails.

A minimal debug snippet to run temporarily (put this at the top of the insert script) will show errors and the incoming fields:

ini_set('display_errors', 1);
error_reporting(E_ALL);
var_dump($_POST);

Concrete checks to perform: confirm the form name attributes match the keys being read; ensure mysql_select_db() actually selects the intended database; verify the table and column names for typos (for example, mismatches like Comfirm_pass vs Confirm_pass will break the query); stop using single quotes for identifiers — identifiers should be unquoted or wrapped in backticks, not single quotes. Always check and show the SQL error string from the DB driver when a query fails.

Fix and harden: explicitly read and validate/sanitize inputs from $_POST, then use parameterized queries (mysqli or PDO prepared statements) instead of building raw SQL strings. If staying with the old mysql extension, escape inputs with the appropriate function before interpolation. Remove debug output and disable display_errors in production.

Recommended Answers

All 4 Replies

HI. I don't know if this will help you but here is how I have inserted similar info to a database:

mysql_query("INSERT INTO 'forum'.'reg_info'  (Username, Password, Comfirm_pass, Name, Comment) VALUES ('$_POST[Username]', '$_POST[Password]', '$_POST[Confirm_pass]', '$_POST[Name]', '$_POST[Comment]')")

Otherwise I think you would have to specify what the definitions are.
EG.

$Username = $_POST[Username];
$Password = $_POST[Password];
$Confirmpass = $_POST[Confirm_pass];
$Name = $_POST[Name];
$Comment = $_POST[Comment];

This would have to be before the SQL query.

I hope that this may be of some help. There probably more knowledgeable people on the forum, but at least I tried.

HI. I don't know if this will help you but here is how I have inserted similar info to a database:

mysql_query("INSERT INTO 'forum'.'reg_info'  (Username, Password, Comfirm_pass, Name, Comment) VALUES ('$_POST[Username]', '$_POST[Password]', '$_POST[Confirm_pass]', '$_POST[Name]', '$_POST[Comment]')")

Otherwise I think you would have to specify what the definitions are.
EG.

$Username = $_POST[Username];
$Password = $_POST[Password];
$Confirmpass = $_POST[Confirm_pass];
$Name = $_POST[Name];
$Comment = $_POST[Comment];

This would have to be before the SQL query.

I hope that this may be of some help. There probably more knowledgeable people on the forum, but at least I tried.

Thank you very much for the reply but really i tried code you gave me yet is having no reflect on the database and i really have to work on my project cos time is runing out pls if you could know any way of helping me out i will really appreciate it.

Most problems with inserts have to do with the use of quotes in queries and datatypes. Do any of the fields in the query have a numeric datatype? If so, they will not require a single quote around them in the query.

HI again. I copied your files and tested them. I have got the following PHP code to work for me. This would be saved as: reg_forminsert.php

<?

  $server	= "localhost";       // the database location (usually this will be localhost)
  $dbusername	= "root";	// the database user with all rights to the database
  $dbpassword	= "";		// the database password to the above user
  $db_name	= "forums";         // the name of the database

$con = @mysql_connect($server,$dbusername,$dbpassword) or die(mysql_error());

$table_name = "reg_info";

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

echo "connection successful<br />";

$db = @mysql_select_db($db_name,$con) or die(mysql_error());

$sql = "INSERT INTO $table_name  (Username, Password, Comfirm_pass, Name, Comment) VALUES ('$_POST[Username]', '$_POST[Password]', '$_POST[Confirmpass]', '$_POST[Name]', '$_POST[Comment]')";

$result = @mysql_query($sql,$con)or die(mysql_error());

echo "Record Inserted<br />";
mysql_close($con); 

?>

I hope this will help you. There may be better ways of doing this but this is how I have been doing it for my projects.

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.