How to add records to a mysql database using a html form?

Discussion in 'PHP' started by smetten, Nov 28, 2005.

  1. #1
    This is what I did, but it doesn´t work:
    - The database is called test
    - The table is also called test
    - There are three fields (name, email, city)
    - I got this code from a tutorial so i guess it must work.

    1) First I created a file called form.html

    <form name="toSave" method="post"
    action="save_it.php">
    <input type="text" name="name" size="20" /><br> <input type="text" name="email" size="20" /><br> <input type="text" name="city" size="20" /><br> <input type="submit" value="Submit"></form>

    2) Then I created a file called save_it.php

    <?php
    $db=mysql_connect("localhost", "root", "") or die("Could not connect to localhost."); mysql_select_db("test", $db) or die("Could not find visitors.");
    $querySQL = "insert into test (d_name, d_email,
    d_city) values ($name, $email, $city)";
    if(!$querySQL) error_message(sql_error());
    ?>

    When I try to enter a new record through the html form it is not being added to the database, any ideas?

    Greetz

    Smetten
     
    smetten, Nov 28, 2005 IP
  2. luvkycool

    luvkycool Guest

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    because after submitting the form,the values can be found in the $_POST array.
    so $_POST['name'] and $_POST['email'] and $_POST['city']
    will contain the proper values not
    $name
     
    luvkycool, Nov 28, 2005 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,838
    Likes Received:
    4,542
    Best Answers:
    123
    Trophy Points:
    665
    #3
    what happens with this code...
    <?php
    $db=mysql_connect("localhost", "root", "") or die("Could not connect to localhost."); 
    mysql_select_db("test", $db) or die("Could not find visitors.");
    $sql = "insert into `test` (`d_name`, `d_email`, `d_city`) 
        values ('$name', '$email', '$city')";
    $result = mysql_query($sql) or die(mysql_error() . '<br />' . $querySQL);
    ?>
    PHP:
    and yes, if register_globals is off you will need
    <?php
    $db=mysql_connect("localhost", "root", "") or die("Could not connect to localhost."); 
    mysql_select_db("test", $db) or die("Could not find visitors.");
    $sql = "insert into `test` (`d_name`, `d_email`, `d_city`) 
        values ('{$_POST['name']}', '{$_POST['email']}', '{$_POST['city']}')";
    $result = mysql_query($sql) or die(mysql_error() . '<br />' . $querySQL);
    ?>
    PHP:
     
    sarahk, Nov 28, 2005 IP
  4. smetten

    smetten Active Member

    Messages:
    269
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thanx for your replies,

    I made the changes to my files but when i now push the submit button on the form, it asks if I want to download the file save_it.php
     
    smetten, Nov 28, 2005 IP
  5. smetten

    smetten Active Member

    Messages:
    269
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Problem solved !

    Thank u very much

    Smetten
     
    smetten, Nov 28, 2005 IP