【发布时间】:2015-05-19 10:52:36
【问题描述】:
我在大学里一直在做一个项目,到目前为止它进展顺利,但我遇到了障碍......
通过它传递的信息不会在 phpmyadmin 数据库中更新。我不明白为什么。 Snippet 2 包含更新表格的所有代码,但由于某种原因它不会。
有人可以帮忙吗,因为我只剩下 2 天的时间提交这个。我已附上我的代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Book library</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
ini_set ( 'display_errors', 'On' );
$username = $_POST ['username'];
$password = $_POST ['password'];
$customerno = $_POST ['customerno'];
echo "<pre>\$_post", print_r ( $_POST ), "</pre>";
@ $db = mysql_pconnect ( 'localhost', 'root', '' );
if (! $db) {
echo 'Error: Could not connect to database. Please
try again later.';
exit ();
}
mysql_select_db ( "assessment" );
$query = "select * from customers where username = '$username'
and password = '$password'";
$result = mysql_query ( $query );
$num_results = mysql_num_rows ( $result );
$row = mysql_fetch_array ( $result );
extract ( $row ); //extract -
creates variables with the same name as the fields in the
array if ($num_results != 0) {
echo "<p>If you would
like to change your information below, enter it in the boxes
below.</p>";
echo "<form action='updateaccount.php'
method='post'> Customer No<br> <input type = 'text' name =
'customerno' value = '$customerno' readonly> </br> First name<br
> <input type = 'text' name = 'firstname' value = '$firstname'>
</br> Last name<br> <input type = 'text' name = 'lastname'
value = '$lastname'> </br> Address<br> <input type =
'text' name = 'address' value = '$address'> </br> Town<br>
<input type = 'text' name = 'town' value = '$town'> </br>
Postcode<br> <input type = 'text' name = 'postcode' value =
'$postcode'> </br> Email<br> <input type='text' name='email'
maxlength='60' size='30' value = '$email'></br> <input name=
'submit' type='submit' value='Continue'> </form>";
} else {
echo "Incorrect try again!!";
}
?> </p>
</body>
</html>
更改信息的页面
<html>
<head>
<title>Scotia Books Entry Results</title>
</head>
<body>
<h1>Scotia Books Entry Results</h1> <?php
echo "<pre>\$_post", print_r ( $_POST ), "</pre>";
$firstname = $_POST ['customerno'];
$firstname = $_POST ['firstname'];
$lastname = $_POST ['lastname'];
$address = $_POST ['address'];
$town = $_POST ['town'];
$postcode = $_POST ['postcode'];
$email = $_POST ['email'];
if (! $firstname || ! $lastname || ! $address || ! $town || ! $postcode || ! $email) {
echo 'You have not entered all the information<br />' . 'Please go back and try again.';
exit ();
}
$db = mysql_connect ( 'localhost', 'root', '' );
if (! $db) {
echo 'Error: Could not connect to database. Please try again later.';
exit ();
} else
mysql_select_db ( 'assessment' );
$query = "update customers set firstname = '$firstname', lastname = '$lastname', address = '$address', town='$town', postcode = '$postcode, email = '$email' where customerno = '$customerno'";
$result = mysql_query ( $query );
if ($result)
echo mysql_affected_rows () . ' Information updated';
else
echo 'did not work';
mysql_close ( $db );
?> </body>
</html>
【问题讨论】:
-
第 33 行有什么?上面是
updateaccount.php -
从哪里获得 $_POST['username']、$_POST['password'] 等?这些变量在 $_POST 中不存在,因此您会收到此错误。
-
在代码 sn-p-2 中,您在其中定义了
$customerno。检查第 9 行:$firstname=$_POST['customerno']; -
请stop using
mysql_*functions。它们不再被维护并且是officially deprecated。改为了解 prepared statements,并考虑使用 PDO,it's not as hard as you think。