PHP Post to MySQL help

Can someone write me a file that posts a form to mysql database?

After many failed attempts, I am really getting frustrated. :’( :’( :’( :’(

Thanks,
Nick

I don’t think anyone is here going to do your work for you, but if you post the code you’ve got so far we can help you get it working. Sound good?

okie doke

Form…
[php]


Audition Portal


Student First Name:

    <input type="text" name="sfname">
    <br>

Student Last Name:

    <input type="text" name="slname">
    <br>

Gender

    <select name="sgender">
      <option value="">Select...</option>
      <option value="M">Male</option>
      <option value="F">Female</option>
    </select>
    <br>

Phone:


Parent First Name:

    <input type="text" name="pfname">
    <br>

Parent Last Name:

    <input type="text" name="plname">
    <br>
    <br>

Please check ALL submitted paperwork:



Recommendation Letter #1



Yes



No



Recommendation Letter #2



Yes



No



Report Card/Transcript



Yes



No






[/php]

Form Processor…

[php]<?php

define(‘DB_NAME’, ‘vpabooke_student1’)
define(‘DB_USER’, ‘vpabooke’)
define(‘DB_PASSWORD’, ‘Password’)
define(‘DB_HOST’, ‘localhost’)

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!link) {
die('Could not connect: ’ . mysql_error(();
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can’t use ’ . DB_NAME . ': ’ . mysql_error(();
}

$sfname = $_POST[‘sfname’];
$slname = $_POST[‘slname’];
$sgender = $_POST[‘sgender’];
$phone = $_POST[‘phone’];
$pfname = $_POST[‘pfname’];
$plname = $_POST[‘plname’];
$rec1 = $_POST[‘rec1’];
$rec2 = $_POST[‘rec2’];
$reportcard = $_POST[‘reportcard’];

$sql = “INSERT INTO students (sfname, slname, sgender, phone, pfname, plname, rec1, rec2, reportccard) VALUES (’$sfname’, ‘$slname’, ‘$sgender’, ‘$phone’, ‘$pfname’, ‘$plname’, ‘$Rec1’, ‘$Rec2’, ‘$ReportCard’,)”;

if (!mysql_query($sql)) {
die('Error: ’ . mysql_error());
}

mysql_close();

?>
[/php]

Are you geting any errors returned? For one I would suggest taking out the extra comma at the end of the query before the closing ) :

'$Rec1', '$Rec2', '$ReportCard',)";

I would also use mysql_real_escape_string() when using $_POST and putting something into your database:

[php]
$sfname = $_POST[‘sfname’];
[/php]
Would be
[php]
$sfname = mysql_real_escape_string($_POST[‘sfname’]);
[/php]

Ok I will try this. I have not received any errors. It will stay on the form page and the url will change to .com/students.php?sfname=blah&slname= etc… I don’t understand why its doing that since the method is post not get.

Thanks!

In the form code you haven’t specified a form action, or method.
[php]

Audition Portal

[/php]

The form should be
[php]

[/php] This guarantees it uses post. There is the chance your host is defaulting to GET as the method.
Sponsor our Newsletter | Privacy Policy | Terms of Service