Help adding a MySQL record using PHP

Discussion in 'PHP' started by jawinn, Oct 2, 2006.

  1. #1
    I can't figure out how to add a record to MySQL from a PHP form.

    Here is the form:

    <form method="post" action="answer.php" />
    First Name&nbsp;&nbsp;&nbsp;Last Name<br>
    <input type="text" name="fname" /><input type="text" name="lname" /><br/><br>
    City&nbsp;&nbsp;&nbsp;State<br>
    <input type="text" name="city" /><select name="state" />
    <option>Alabama</option>
    <option>Wyoming</option>
    </select><br/><br>
    E-mail Address<br>
    <input type="text" name="email" /><br/><br>
    Answer: <input type="radio" name="answer" value="A" /> A<input type="radio" name="answer" value="B" /> B<input type="radio" name="answer" value="C" /> C<input type="radio" name="answer" value="D" /> D<br/>
    <input type="checkbox" name="remember" value="1" />Remember Me<br/><br/>
    <input type="submit" name="submit" value="Submit" />
    </form>

    Here is my processor:

    <?php

    // connect to the database

    include('conn.php');

    $error = false;

    // run this only, once the user has hit the "Add Contact" button

    if (isset($_POST['centries'])) {

    // assign form inputs

    $fname = $_POST['fname'];

    $lname = $_POST['lname'];

    $email = $_POST['email'];

    $city = $_POST['city'];

    $state = $_POST['state'];

    $answer = $_POST['answer'];

    // validate inputs

    $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('".$fname."','".$lname."',''".$email."','".$city."','".$state."','".$answer."')";

    $result = $database->query($query);
    ?>

    Any help is much appreciated.

    Thanks in advance,
    Jawinn
     
    jawinn, Oct 2, 2006 IP
  2. digdogger

    digdogger Well-Known Member

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #2
    Is include('conn.php'); actually giving you a connected database object?

    Are you getting any errors, or is it just not inserting?

    How is the database table defined? States are often only given 2 letters for the code.
     
    digdogger, Oct 2, 2006 IP
  3. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No-one can tell what that code does unless you describe the $database object.

    Use error_reporting(E_ALL ^ E_STRICT) at the top of your code to catch as many warnings and notices as possible. Also use the error function of your database interface, whatever it is.
     
    penagate, Oct 2, 2006 IP
  4. jawinn

    jawinn Active Member

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Yes

    I changed my code slightly to this:

    <?php

    include('conn.php');

    $error = false;

    if(isset($_POST['submit'])) {

    $form = array();
    $form['fname'] = $_POST['fname'];
    $form['lname'] = $_POST['lname'];
    $form['email'] = $_POST['email'];
    $form['city'] = $_POST['city'];
    $form['state'] = $_POST['state'];
    $answer = $_POST['answer'];

    if(!ini_get('magic_quotes_gpc')) {
    // Build safe query values string
    foreach($form as $key => $value) {
    $form[$key] = mysql_escape_string($value);
    }
    }

    $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('{$form['fname']}', '{$form['lname']}', '{$form['email']}', '{$form['city']}','{$form['state']}', '{$form['answer']}',)";

    $result = $database->query($query);

    ?>

    I get this error now:

    Parse error: parse error, unexpected $ in xxx/xxx/xxx/answer.php on line 29
     
    jawinn, Oct 2, 2006 IP
  5. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Try this:

    
    $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('" . $form['fname'] . "', '" . $form['lname'] . "', '" . $form['email'] . "', '" . $form['city'] . "','" . $form['state'] . "', '" . $form['answer'] . "')";
    Code (markup):
     
    SoKickIt, Oct 2, 2006 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    You are using the ' sign as
    INSERT INTO table values('{$form['email']}');

    See the ' sign inside $form['email'] and before and after '{}'
    That's why the code error.
    Try this code below.
    Secondly, I think you also need an auto increment field, else you might have problems displaying the output.

    <?php

    include('conn.php');

    $error = false;

    if(isset($_POST['submit'])) {

    $formfname = $_POST['fname'];
    $formlname = $_POST['lname'];
    $formemail = $_POST['email'];
    $formcity = $_POST['city'];
    $formstate = $_POST['state'];
    $formanswer = $_POST['answer'];

    if(!ini_get('magic_quotes_gpc')) {
    // Build safe query values string
    foreach($form as $key => $value) {
    $form[$key] = mysql_escape_string($value);
    }
    }

    $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('$formfname', '$formlname', '$formemail','$formcity','$formstate','$formanswer')";
    $result = $database->query($query);
    ?>

    Hope that helps you. :)
     
    JEET, Oct 2, 2006 IP