-4

嗯,我有一个问题,但我找不到它。我改变了一切,每次都尝试了不同的东西,我最终得到了这个看起来不错但仍然不起作用的编码。我想知道是否有人可以帮助我找到它。

这里表格的动作:

 <?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";

?>

在此先感谢,我尝试了一切,但它不起作用。

我的错误是我没有任何错误......它一切正常,但只是没有写在数据库上!

请帮忙!

4

3 回答 3

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());
 }
于 2013-11-04T15:25:15.103 回答
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.')");

而且您至少应该为您的查询创建一个适当的字符串。不过,您应该使用准备好的语句。

于 2013-11-04T16:03:28.057 回答
0

试一试,确保您的数据库列和表单输入与您发布的现有信息相匹配。

但首先,MySQL_推荐使用。请切换到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'=>'" . $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 . "')");
于 2013-11-04T15:55:48.407 回答