I cannot get my php file to post data to my database.

Avatar
  • Answered
I have followed the directions from the article "Using PHP to INSERT data into a database" that is posted in your support center but the form will still not post data to the database.
I can connect to the database and had the support center verify that I can connect. I think there may be something wrong with my SQL command but cannot figure it out.

I am working in Dreamweaver CS5 and setup the database on the cpanel using the MySQL Database Wizard.

Here is part of my PHP code:
{
$connection = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $connection);

//setup posting to the database
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$title = $_POST['title'];
$company = $_POST['company'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state_province = $_POST['state_province'];
$postal_code = $_POST['postal_code'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$email = $_POST['email'];

$firstname = htmlspecialchars($firstname);
$lastname = htmlspecialchars($lastname);
$title = htmlspecialchars($title);
$company = htmlspecialchars($company);
$address = htmlspecialchars($address);
$address2 = htmlspecialchars($address2);
$city = htmlspecialchars($city);
$state_province = htmlspecialchars($state_province);
$postal_code = htmlspecialchars($postal_code);
$country = htmlspecialchars($country);
$phone = htmlspecialchars($phone);
$fax = htmlspecialchars($fax);
$email = htmlspecialchars($email);

$query = "
INSERT INTO 'dadcon5_calculator'.'Registration' ('firstname','lastname','title','company','address','address2','city','state_province','postal_code','country','phone','fax','email') VALUES ('$firstname','$lastname','$title','$company','$address',$address2','$city','$state_province','$postal_code','$country','$phone','$fax','$email');";

mysql_query($query);

//execute the SQL statement
if (mysql_query($sql,$connection)) {
echo "record added";
} else {
echo "fubar";
mysql_close($connection);
}

Avatar
Scott
Hello DADCO_MKT,

When troubleshooting the query, you will first want to test the query outside the code in the phpmyadmin tool using the SQL editor.

Also, try using backticks for the quotes around the table/columnames and regular single quotes for the values. The backtick key is the key that resembles a single quote to the left of the 1 key. I personally do not use quotes for the table and column names and have no issues, as long as everything else is in order. For example:

INSERT INTO table (firstname,lastname, address) VALUES ('$firstname','$lastname','$address')

Give either or both of those methods a try to see if it helps.

Also, I see you are testing the results for either "record added" (success) or "fubar" (failure). When your query goes through, are you getting the "fubar" echo or are you getting "record added" but the data in the database is blank?

I do see several rows in the database, but all fields are filled with BLOBS so they do not show the content in phpmyadmin.

I hope this answers your question. If you have any more questions or information specific to the issue please leave a comment below so we can further assist you.

Best Regards,
Scott M