我已经计算出一个错误,在我将数据更新到数据库后无法查看我的数据。
作为我的数据库: 名称:item_id;类型:char;键入:主键
错误讯息:
Notice: Undefined index: item_id in ...\\Testing\itemEdit.php on line 14
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ...\\Testing\itemEdit.php on line 17
Not found Data
这是我的itemEdit.php代码:
<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "db_test";
// make the connect to mysql
$link = mysql_connect($host, $username, $password);
If ($link == false){
die ('Could not connect'.mysql_error());
}
//$item_id=$row_item['item_id'];
$itemID = mysql_escape_string($_GET['item_id']);
$strSQL = "SELECT * FROM item_db WHERE item_id = '$itemID'";
$objQuery = mysql_query($strSQL);
while ($objResult = mysql_fetch_array($objQuery)){
if(!$objResult)
{
echo "Not found Data";
}
else
{
echo "Error Save [".$strSQL."]";
}
}
mysql_close($link);
?>
<html>
<head>
<title>Staff Edit Record</title>
</head>
<body>
<div style = "border: solid 1px black;
background-color: beige;">
<p>
<label for="itemID"> Item ID:</label>
<input type="text" name="itemID" size="25" value="<?php echo $objResult["item_id"];?>">
</p>
</div>
</body>
</html>
请帮助具体并提供代码,因为我是初学者在php ..
非常感谢...
答案 0 :(得分:0)
将您的代码更新为以下代码。
<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "db_test";
// make the connect to mysql
$link = mysql_connect($host, $username, $password);
if($link == false)
{
die ('Could not connect'.mysql_error());
}
$itemID = mysql_escape_string($_GET['item_id']);
$strSQL = "SELECT * FROM item_db WHERE item_id = '$itemID'";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if(isset($_POST))
{
//$item_id=$row_item['item_id'];
$newItemID = mysql_escape_string($_POST['item_id']);
$strSQL = "UPDATE item_db SET item_id = '$newItemID' WHERE item_id = '$itemID'";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
if(!$objResult)
{
echo "Not found Data";
}
else
{
echo "Error Save [".$strSQL."]";
}
}
}
mysql_close($link);
?>
<html>
<head>
<title>Staff Edit Record</title>
</head>
<body>
<div style = "border: solid 1px black; background-color: beige;">
<form method="post" action="">
<p>
<label for="itemID"> Item ID:</label>
<input type="text" name="item_id" size="25" value="<?php echo $objResult["item_id"];?>">
</p>
<input type="submit" value="UPDATE">
</form>
</div>
</body>
</html>
自PHP 5.5.0起, 注意 mysql
扩展名已弃用,将来会被删除。相反,应该使用MySQLi或PDO_MySQL扩展。