Jump to content

Inserting $_POST form data to sql database


Prefect

Recommended Posts

 

Hello all.  I'm new to PHP and this forum, and need some guidance on the subject of inserting $_POST form data to a sql database.  I have the two scripts noted below but have  been unable to make them work.  Please show me the corrected syntax and what I'm doing wrong.  Many thanks. 

 

 

HTML Form

 

<html><head><title></title></head>

<body>

<form action = "customer_2.php" method = "POST">

<br>

<input type = "text" name = "firstname" />

First Name

<br><br>

<input type = "text" name = "lastname" />

Last Name

<br><br>

<input type = "submit" value = "Submit Registration" />

<br><br>

</form>

</body>

</html>

 

 

 

PHP insert script

 

$conn = mysql_connect("localhost", "root", "");

if (!$conn) {

    echo "Unable to connect to DB: " . mysql_error();

    exit;

}

if (!mysql_select_db("customer_temp_database")) {

    echo "Unable to select mydbname: " . mysql_error();

    exit;

}

$query = "INSERT INTO customer (firstname, lastname)

VALUES ('$_POST[firstname]','$_POST[lastname]')";

<?php

$conn = mysql_connect("localhost", "root", "");
if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
if (!mysql_select_db("customer_temp_database")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}
$firstName = mysql_real_escape_string($_POST[firstname]);
$lastName = mysql_real_escape_string($_POST[lastname]);
$query = "INSERT INTO customer (firstname, lastname)
   VALUES ('$firstName','$lastName')";
$result = mysql_query($query);

if(mysql_affected_rows() > 0)
{
  echo 'Inserted to database sucessfully';
}
?>

 

Nate

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.