Hey guys,
I’m not great at PHP, but learning. I want to simply insert values passed by a form into a mySQL table. This is the code I have so far:
[code=php]$email = $_POST[’email’];
$first_name = $_POST[‘first_name’];
$last_name = $_POST[‘last_name’];
$gender = $_POST[‘gender’];
if(!$email == “” && (!strstr($email,”@”) || !strstr($email,”.”))) {
die (“Missing or invalid email address. Please go back and fill this out again.”);
}
if(empty($first_name) || empty($last_name)) {
die (“Sorry, you left a required field blank. Please go back and fill this out again.”);
}
$con=mysql_connect(localhost, username, password);
if (!$con) {
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(mydatabase, $con);
$sql=”INSERT INTO myTable (email, first_name, last_name, gender)
VALUES ($email, $first_name, $last_name, $gender)”; //do these need to be enclosed by quotes? ex ‘$email’,’$first_name’,…
…
Now, I know I can do this:
$sql=”INSERT INTO myTable (email, first_name, last_name, gender)
VALUES (‘$_POST[first_name]’,’$
But I figured that I had to call the variables beforehand, for the validation (is that correct)?
Also, my form names are first_name, last_name, gender and so are the table fields. Do I have to change the names in the form for this to work?
Thanks a lot for any help.