Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts

    why can't insert data into the database table?

    the html file
    Code:
    <form method="post" action="add.php"><input type="hidden" name="id" value="NULL" /><table><tr height="20"><td colspan="2"><font size="+0" face="verdana">Below is a sample form for our php</td></tr><tr height="50"><td></td></tr><tr><td align="left"><font size="+0" face="verdana"><b>Your name<br />Your Email Address</b></td><td><input type="text" name="name"><br /><input type="text" name="email" /></td> </tr><tr><td colspan="2"><center><select name="opinion"><option value="is greate">I like your site</option><option value="is OK">you site is ok</option><option value="is horrible">your site is horrible</option></select><input  type="submit" value="tell us"!/></td></tr></table></form>
    add.php file
    PHP Code:

    $DBhost 
    ="localhost"//mysql-server
    $DBuser "root"//mysqluser
    $DBpass "123";
     
    $DBName "learnphp";
    $table "information";
    mysql_connect($DBhost,$DBuser,$DBpass) or die("error");
    mysql_select_db("$DBName") or die("error");
    $sqlquery "INSERT INTO $table (id,name,email,opinion)VALUES ('NULL','$_POST[name]','$_POST[email]','$_POST[opinion]')";
    $results=mysql_query($sqlquery);
    mysql_close();
    echo 
    $_POST['id']. "<br />";
    echo 
    $_POST['name']. "<br />";
    echo 
    $_POST['email']. "<br />";
    echo 
    $_POST['opinion']. "<br />"

  • #2
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    Not going to code it out for you, but here's some tips for you

    - in the databasequery:

    $sqlquery = "INSERT INTO $table (id,name,email,opinion)VALUES ('NULL','$_POST[name]','$_POST[email]','$_POST[opinion]')";

    This isnt going to work.

    Try putting a space between de column naming and ' VALUES', further the inputted values ($_POST[name]) isnt going to work. $_POST['name'] is the correct one. If you simply assign some variables to the posts, it will be easyer to insert those in a query.

    There might be more, but this is what I got after 1 minute of looking.

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Try this

    HTML Code (Few tags r not closed and I changed this <input type="submit" value="tell us"! /> ; Even without making any changes it worked for me. )

    Code:
    <form method="post" action="add.php">
      <input type="hidden" name="id" value="NULL" />
      <table>
        <tr height="20">
          <td colspan="2"><font size="+0" face="verdana">Below is a sample form for our php</font></td>
        </tr>
        <tr height="50">
          <td></td>
        </tr>
        <tr>
          <td align="left"><font size="+0" face="verdana"><b>Your name</font><br />
            Your Email Address</b></td>
          <td><input type="text" name="name">
            <br />
            <input type="text" name="email" /></td>
        </tr>
        <tr>
          <td colspan="2"><center>
            <select name="opinion">
              <option value="is greate">I like your site</option>
              <option value="is OK">you site is ok</option>
              <option value="is horrible">your site is horrible</option>
            </select>
            <input  type="submit" value="tell us!" /></td>
        </tr>
      </table>
    </form>
    I used the same PHP Code above..

    It worked for me... Hope this helps for you too..

    If it wont work for you, please tell the error you are getting.. may be some problem with your database connection or so..
    Last edited by shanu.chaudhary; 06-24-2010 at 10:06 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •