By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,423 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,423 IT Pros & Developers. It's quick & easy.

Inserting more than one record

P: 2
Hi, I recently created a mysql database using phpmyadmin. I then proceeded to make a form to insert data into the database, but the problem is that the form is only able to insert one record, and then if I try inserting another record, the new one is not seen in the database. There is no error messages seen in the form when entering a new record.

Heres the code for the form
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. </head>
  4. <center>
  5. <form method="post" action="script.php">
  6. <input type="hidden" name="id" value="null">
  7. <table>
  8. <tr><td align="left">Name</td>
  9. <td><input type="text" name="name"></td>
  10. </tr>
  11. <tr><td align="left">Telephone</td>
  12. <td><input type="text" name="telephone" size="20"></td>
  13. </tr>
  14. <tr><td align="left">Birthday</td>
  15. <td><input type="text" name="birthday" size="20"></td>
  16. </tr>
  17. <tr><td colspan="2">
  18. <p align="center">
  19. <input type="submit" value="Enter record">
  20. </td>
  21. </tr>
  22. </table>
  23. </form>
  24. </center>
  25. </html>
  26.  
And Heres the code for the php script
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $DBhost = "localhost";
  3. $DBuser = "username";
  4. $DBpass = "password";
  5. $DBName = "database name";
  6. $table = "table name";
  7.  
  8.  mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
  9.  
  10. @mysql_select_db("$DBName") or die("Unable to select database $DBName");
  11.  
  12. $sqlquery = "INSERT INTO $table VALUES('$id','$name','$telephone','$birthday')";
  13.  
  14. $results = mysql_query($sqlquery);
  15.  
  16. mysql_close();
  17.  
  18. print "<html><body><center>";
  19. print "<p>You have just entered this record<p>";
  20. print "Name : $name<br>";
  21. print "Telephone : $telephone<br>";
  22. print "Birthday :$birthday";
  23. print "</body></html>";
  24. ?>
Aug 7 '07 #1
Share this Question
Share on Google+
5 Replies


ak1dnar
Expert 100+
P: 1,584
$sqlquery = "INSERT INTO $table VALUES('$id','$name','$telephone','$birthday')";
Did you create a $_POST Array to Collect the VALUES? I didn't see it on the Script.php.
Aug 7 '07 #2

P: 2
No I did not use an array in this case, but instead submitted each value individually.
Aug 7 '07 #3

10K+
P: 13,264
No I did not use an array in this case, but instead submitted each value individually.
Change your results line to

Expand|Select|Wrap|Line Numbers
  1.      $results =mysql_query($sqlquery) or die("Error : ".mysql_error());
Also don't forget to make use of the poor man's debugger( echo, printf, or whatever you call those things in PHP).
Aug 7 '07 #4

ak1dnar
Expert 100+
P: 1,584
No I did not use an array in this case, but instead submitted each value individually.
Please you may better to read here.

Get the HTML form elements to variables. I think Now you might know what is $_POST.

Expand|Select|Wrap|Line Numbers
  1. $name=$_POST['name'];
  2. $telephone=$_POST['telephone'];
  3. $birthday=$_POST['birthday'];
Pass The varibales to the MySQL Queary String.

Expand|Select|Wrap|Line Numbers
  1. $sqlquery = "INSERT INTO sample_table (name,telephone,birthday)
  2. VALUES (
  3. '$name', '$telephone', '$birthday'
  4. );";
  5.  
I didn't add the id formelement (may be it is the PK of your Table Structure). Actually value for that, has set up as Null. Still Only a String value. Its not same as the NULL on MySQL.

How is your Table Structure goes?
Aug 7 '07 #5

P: 18
Your id column must be declared with auto_increment property. If u dont want to add id to your db by manually..

Kind Regards...
Aug 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.