Inserting more than one record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bob44
    New Member
    • Aug 2007
    • 2

    Inserting more than one record

    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
    [code=html]
    <html>
    <head>
    </head>
    <center>
    <form method="post" action="script. php">
    <input type="hidden" name="id" value="null">
    <table>
    <tr><td align="left">Na me</td>
    <td><input type="text" name="name"></td>
    </tr>
    <tr><td align="left">Te lephone</td>
    <td><input type="text" name="telephone " size="20"></td>
    </tr>
    <tr><td align="left">Bi rthday</td>
    <td><input type="text" name="birthday" size="20"></td>
    </tr>
    <tr><td colspan="2">
    <p align="center">
    <input type="submit" value="Enter record">
    </td>
    </tr>
    </table>
    </form>
    </center>
    </html>
    [/code]
    And Heres the code for the php script
    [code=php]
    <?
    $DBhost = "localhost" ;
    $DBuser = "username";
    $DBpass = "password";
    $DBName = "database name";
    $table = "table name";

    mysql_connect($ DBhost,$DBuser, $DBpass) or die("Unable to connect to database");

    @mysql_select_d b("$DBName") or die("Unable to select database $DBName");

    $sqlquery = "INSERT INTO $table VALUES('$id','$ name','$telepho ne','$birthday' )";

    $results = mysql_query($sq lquery);

    mysql_close();

    print "<html><body><c enter>";
    print "<p>You have just entered this record<p>";
    print "Name : $name<br>";
    print "Telephone : $telephone<br>" ;
    print "Birthday :$birthday";
    print "</body></html>";
    ?>[/code]
    Last edited by ak1dnar; Aug 7 '07, 05:49 AM. Reason: Added [CODE=xxx] Tags
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    $sqlquery = "INSERT INTO $table VALUES('$id','$ name','$telepho ne','$birthday' )";
    Did you create a $_POST Array to Collect the VALUES? I didn't see it on the Script.php.

    Comment

    • bob44
      New Member
      • Aug 2007
      • 2

      #3
      No I did not use an array in this case, but instead submitted each value individually.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by bob44
        No I did not use an array in this case, but instead submitted each value individually.
        Change your results line to

        [CODE=php] $results =mysql_query($s qlquery) or die("Error : ".mysql_error() );[/CODE]
        Also don't forget to make use of the poor man's debugger( echo, printf, or whatever you call those things in PHP).

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by bob44
          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.

          [CODE=php]$name=$_POST['name'];
          $telephone=$_PO ST['telephone'];
          $birthday=$_POS T['birthday'];[/CODE]

          Pass The varibales to the MySQL Queary String.

          [CODE=php]$sqlquery = "INSERT INTO sample_table (name,telephone ,birthday)
          VALUES (
          '$name', '$telephone', '$birthday'
          );";
          [/CODE]

          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?

          Comment

          • afraze
            New Member
            • Aug 2007
            • 18

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

            Kind Regards...

            Comment

            Working...