Jump to content

Posting Form Data to MySQL Database


coolhandluke012

Recommended Posts

Hi everyone,

 

First off thank you very kindly to anyone who can provide some enlightenment to this total php/mysql newb.  I am creating my first database and while I have been able to connect to the database within my php script (or so I believe), the form data does not appear to be actually getting sent. 

 

I have development experience in other languages but this is completely new to me so if I've missed something that appears painfully obvious like a parse error of some sort, I do apologize.  I am creating a website using Godaddy as the hosting account, and attempting to connect to the mysql database at the following URL (maybe this is where I'm going wrong):

 

"pnmailinglist.db.4662743.hostedresource.com"

 

Below is my very simple code:

 

<?php

 

//Verify successful connection to database.

 

$connect = mysql_connect("pnmailinglist.db.4662743.hostedresource.com", "*********", "*********");

 

if(!$connect)

{die("Could not connect!");

}

 

//Initialize variables with form data.

 

$firstname = $_POST['FirstName'];

$lastname = $_POST['LastName'];

$email = $_POST['Email'];

$howfound = $_POST['HowFound'];

 

//Post data to database, or return error.

 

mysql_select_db("pnmailinglist", $connect);

 

mysql_query("INSERT INTO mailinglist (First, Last, Email, How_Found) VALUES ($firstname,$lastname,$email,$howfound)");

 

mysql_close($connect);

 

echo "Thank you for joining our mailing list!  We will contact you soon with the dates and times of our upcoming events.";

 

?>

 

 

 

Thank you again very much for any pointers or hints as to where I'm screwing up.  I get no runtime errors, no syntax errors, and the echo message does display fine at the end -- just no data when I go to check my database! 

 

 

Best Regards,

 

CL

Link to comment
Share on other sites

I would make a few changes to do some basic debugging, such as echoing values, adding or die() statement, etc.

 

<?php
//Verify successful connection to database.
$connect = mysql_connect("pnmailinglist.db.4662743.hostedresource.com", "*********", "*********");
if(!$connect) {
die("Could not connect!");
}
echo '<pre>';
print_r($_POST);
echo '</pre>';

//Initialize variables with form data.
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$email = $_POST['Email'];
$howfound = $_POST['HowFound'];

//Post data to database, or return error.

mysql_select_db("pnmailinglist", $connect);

$query = "INSERT INTO mailinglist (First, Last, Email, How_Found) VALUES ($firstname,$lastname,$email,$howfound)";
mysql_query($query) or die( '<br />Query string: ' . $query . '<br />Produced error: ' . mysql_error() . '<br />');
echo 'Rows affected by INSERT query: ' . mysql_affected_rows();
mysql_close($connect);

echo "Thank you for joining our mailing list!  We will contact you soon with the dates and times of our upcoming events.";
?>

Link to comment
Share on other sites

Hi Pikachu,

 

Thank you for getting back to me so quickly, I am very grateful for your time and advice.  I ran the code with your recommendations, and found that my data is being properly read in to the array, and also that I am connecting to the database.  When you added the mysql_error() call there, this is what I receive when inputting the variables to the VALUES() as $varname, $varname2, $varname3:

 

"Query string: INSERT INTO `MAILINGLIST` (`id`,`First`, `Last`, `Email`, `How_Found`) VALUES (NULL, $firstname, $lastname, $email, $howfound);

Produced error: Unknown column '$firstname' in 'field list'

 

 

When I ran the query directly on myPHPAdmin, of course I couldn't use the variable names at that point so I directly substituted some values for each - this worked. 

 

It appears to me therefore that I am tripping up somewhere on the syntax involving how the VALUES() portion replaces the variable names with the actual instance data for each.  There is no column of course with those names, so I'm not sure why it is looking for a column at that point, I specified the column names in the first list there. 

 

 

Given the data is being read in properly to the _POST[] array, and also my variables are being initialized properly (verified this by doing an echo of each variable after each initialization), any ideas why when I go to put in the VALUES() as the variables, it all goes to crap? 

 

 

MySQL server version is 5.0.91-log if that is of assistance.  I am completely stuck here.  I've now echoed out everything I can think to echo out, verified I am actually connecting to the database/table, and that all variables are getting initialized properly.

 

 

Thanks a million in advance for any feedback or ideas you may have about why this is happening. 

 

 

CL

Link to comment
Share on other sites

Missed it last night, but string type data needs to be enclosed in quotes in the query string.

 

Table and field names should be enclosed in backticks, and although it isn't necessarily required, it can save you headaches in the future.

 

$query = "INSERT INTO `mailinglist` (`First`, `Last`, `Email`, `How_Found`) VALUES ('$firstname', '$lastname', '$email', '$howfound')";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.