Well I have a problem, but I can't find it. I changed everithing and tried everything different everytime, I ended up with this coded which it looks good but still doesnt work. I would like to know if someone can help me to find it.
Here the action of the form:
<?php
$username = "Test";
$password = "dbpw";
$hostname = "localhost";
//connection to the database
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL";
?>
<?php
$db_select = mysql_select_db("CS450Game", $con);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
?>
<?php
$lname = mysql_escape_string($_POST['txt_Lname']);
$mi = mysql_escape_string($_POST['txt_MI']);
$fname = mysql_escape_string($_POST['txt_Fname']);
$month = mysql_escape_string($_POST['lst_dobmonth']);
$day = mysql_escape_string($_POST['lst_dobday']);
$year = mysql_escape_string($_POST['txt_dobyear']);
$address = mysql_escape_string($_POST['txt_address']);
$city = mysql_escape_string($_POST['txt_city']);
$state = mysql_escape_string($_POST['lst_state']);
$zip = mysql_escape_string($_POST['txt_zip']);
$hphone = mysql_escape_string($_POST['txt_HPhone']);
$cphone = mysql_escape_string($_POST['txt_CPhone']);
$email = mysql_escape_string($_POST['txt_email']);
$email2 = mysql_escape_string($_POST['txt_email2']);
$country = mysql_escape_string($_POST['lst_country']);
mysql_query("INSERT INTO `Customer` (`txt_Lname`, `txt_MI`, `txt_Fname`, `lst_dobmonth`, `lst_dobday`, `txt_dobyear`, `txt_address`, `txt_city`, `lst_state`, `txt_zip`, `txt_HPhone`, `txt_CPhone`, `txt_email`, `txt_email2`, `lst_country`)
VALUES ('$lname', '$mi', '$fname', '$month', '$day', '$year', '$address', '$city', '$state', '$zip', '$hphone', '$cphone', '$email', '$email2', '$country')");
echo "You info has been submitted";
?>
Thanks in advanced, I tried everything but it doesn't work.
My error is that I don't have any error... It works everything fine but just doesn't write on the database!
Please help!
It is good practice to look up any errors :
$result = mysql_query("INSERT ....");
if (!$result) {
echo mysql_errno();
die("Some error message");
}
UPDATE: From your update, add this after connect
$db_select = mysql_select_db("my_database", $con);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
Give this a try and make sure that your DB columns and form inputs match your existing info that you posted.
But first, MySQL_
is deprecated. Do switch to either MySQLi_
and/or PDO.
<?php
//connection to the database
$hostname = "localhost";
$username = "Test";
$password = "dbpw";
$con = mysql_connect($hostname, $username, $password);
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$lname = mysql_escape_string($_POST['txt_Lname']);
$mi = mysql_escape_string($_POST['txt_MI']);
$fname = mysql_escape_string($_POST['txt_Fname']);
$month = mysql_escape_string($_POST['lst_dobmonth']);
$day = mysql_escape_string($_POST['lst_dobday']);
$year = mysql_escape_string($_POST['txt_dobyear']);
$address = mysql_escape_string($_POST['txt_address']);
$city = mysql_escape_string($_POST['txt_city']);
$state = mysql_escape_string($_POST['lst_state']);
$zip = mysql_escape_string($_POST['txt_zip']);
$hphone = mysql_escape_string($_POST['txt_HPhone']);
$cphone = mysql_escape_string($_POST['txt_CPhone']);
$email = mysql_escape_string($_POST['txt_email']);
$email2 = mysql_escape_string($_POST['txt_email2']);
$country = mysql_escape_string($_POST['lst_country']);
$sql = mysql_query("INSERT INTO `Customer` (`txt_Lname`, `txt_MI`, `txt_Fname`, `lst_dobmonth`, `lst_dobday`, `txt_dobyear`, `txt_address`, `txt_city`, `lst_state`, `txt_zip`, `txt_HPhone`, `txt_CPhone`, `txt_email`, `txt_email2`, `lst_country`)
VALUES ('$lname', '$mi', '$fname', '$month', '$day', '$year', '$address', '$city', '$state', '$zip', '$hphone', '$cphone', '$email', '$email2', '$country')");
mysql_select_db('your_Database_name'); // change this to your DB name
$retval = mysql_query( $sql, $con ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
echo "Entered data successfully
";
mysql_close($con);
?>
You could also try replacing these: '$lname'
=> '" . $lname . "'
etc.
VALUES ('$lname', '$mi', '$fname', '$month', '$day', '$year', '$address', '$city', '$state', '$zip', '$hphone', '$cphone', '$email', '$email2', '$country')");
to:
VALUES ('" . $lname . "', '" . $mi . "', '" . $fname . "', '" . $month . "', '" . $day . "', '" . $year . "', '" . $address . "', '" . $city . "', '" . $state . "', '" . $zip . "', '" . $hphone . "', '" . $cphone . "', '" . $email . "', '" . $email2 . "', '" . $country . "')");
You are not building the query properly. You are forgetting to concatenate the strings. Change to:
$sql = mysql_query("INSERT INTO `Customer` (`txt_Lname`, `txt_MI`, `txt_Fname`, `lst_dobmonth`, `lst_dobday`, `txt_dobyear`, `txt_address`, `txt_city`, `lst_state`, `txt_zip`, `txt_HPhone`, `txt_CPhone`, `txt_email`, `txt_email2`, `lst_country`) VALUES ('.$lname.', '.$mi.', '.$fname.', '.$month.', '.$day.', '.$year.','.$address.', '.$city.', '.$state.', '.$zip.', '.$hphone.','.$cphone.','.$email.','.$email2.', '.$country.')");
And you should at least create a proper string for your query. Still, you should use prepared statements.