我有问题,但我找不到。我改变了每一次尝试所有不同的东西,我最终得到了这个编码,它看起来不错,但仍然无法正常工作。我想知道是否有人可以帮我找到它。
这里是表格的动作:
<?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";
?>
先谢谢了,我尝试了一切,但它不起作用。
我的错误是我没有任何错误......一切正常但只是不写在数据库上!
请帮忙!
答案 0 :(得分:1)
最好查找任何错误:
$result = mysql_query("INSERT ....");
if (!$result) {
echo mysql_errno();
die("Some error message");
}
更新:从您的更新中,在连接后添加此内容
$db_select = mysql_select_db("my_database", $con);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
答案 1 :(得分:0)
尝试一下,确保您的数据库列和表单输入与您发布的现有信息相匹配。
但首先,MySQL_
是 deprecated 。请切换到MySQLi_
和/或 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\n";
mysql_close($con);
?>
您也可以尝试替换这些:'$lname'
=&gt; '" . $lname . "'
等。
VALUES ('$lname', '$mi', '$fname', '$month', '$day', '$year', '$address', '$city', '$state', '$zip', '$hphone', '$cphone', '$email', '$email2', '$country')");
为:
VALUES ('" . $lname . "', '" . $mi . "', '" . $fname . "', '" . $month . "', '" . $day . "', '" . $year . "', '" . $address . "', '" . $city . "', '" . $state . "', '" . $zip . "', '" . $hphone . "', '" . $cphone . "', '" . $email . "', '" . $email2 . "', '" . $country . "')");
答案 2 :(得分:0)
您没有正确构建查询。你忘了连接字符串了。 改为:
$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.')");
至少应该为查询创建一个合适的字符串。 不过,你应该使用准备好的陈述。