我的PHP表单有问题,应该更新我的MySQL数据库表的一个人的年龄。该列名为Age。我得到的问题是当我尝试更新它不会更新并显示的年龄时
UPDATE Persons SET Age=Array WHERE FirstName ='Array'
array(13) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" [6]=> string(0) "" [7]=> string(0) "" [8]=> string(0) "" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" [12]=> string(3) "544" } Cannot update
在我的页面底部。您可以在http://thetotempole.ca/phptester/editpage.php
查看我的页面我页面的所有代码:
<?php
$hostname = "";//host name
$dbname = "";//database name
$username = "";//username you use to login to php my admin
$password = "";//password you use to login
//CONNECTION OBJECT
//This Keeps the Connection to the Databade
$conn = new MySQLi($hostname, $username, $password, $dbname) or die('Can not connect to database')
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//$id=$_GET['FirstName'];
//Create a query
$sql = "SELECT * FROM Persons";
//submit the query and capture the result
$result = $conn->query($sql) or die(mysql_error());
$query=getenv(QUERY_STRING);
parse_str($query);
?>
<h2>Update Record <?php echo $sql;?></h2>
<a href="cool.php">Click Here to see the Table Data</a>
<?php $counter = 0; ?>
<form action="" method="post">
<?php
while ($row = $result->fetch_assoc()) {?>
<table border="0" cellspacing="10">
<tr>
<td>Age:</td>
<td><?php echo $row['Age'][$counter];?></td>
<td><?php echo $row['FirstName'];?></td>
<td><input type="text" name="Age[]"></td>
<td><input type="text" name="FirstName[]" value="<?php echo $row['FirstName'];?>" /></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php
$counter++; }
?>
</form>
<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
$age = $_POST['Age'];
$id = $_POST['FirstName'];
$sql="UPDATE Persons SET Age=$age WHERE FirstName ='$id'";
echo $sql."<br>";
var_dump($_POST[Age]);
$conn->query($sql) or die("Cannot update");//update or error
}
?>
</body>
</html>
我将代码更新为我当前的代码。
答案 0 :(得分:0)
您可以使用数组:
<td><input type="text" name="person[<?php echo $row['FirstName'];?>]"></td>
和
if(isset($_POST['Submit']))
foreach ($_POST['person'] as $id => $age)
{
$sql="UPDATE Persons SET Age='".$age."' WHERE firstname ='".$id."'";
$conn->query($sql) or die("Cannot update");//update or error
}
另外,不要忘记表单输入名称中FirstName中的mysql字符串转义和html实体。
答案 1 :(得分:0)
以下是phpfiddle的内容,以便于访问:
<?php
if(isset($_POST['Submit'])){ //if ANY submit button has been clicked
if (is_numeric($_POST['Age'])) {
$age = (int) $_POST['Age'];
} elseif ($age == '') {
$age = (string) 'NULL';
die('Please go back and enter a valid age.');
} else {
die('Please go back and enter a valid age.');
}
$id = mysqli_real_escape_string($conn,$_POST['FirstName']);
$sql="UPDATE Persons SET Age=$age WHERE FirstName ='$id'";
$conn->query($sql) or die("Cannot update"); //update or error
}
$sql="SELECT * FROM Persons";
$conn->query($sql) or die("Cannot update"); //update or error
while ($row = $result->fetch_assoc()){
$age = (int) $row['Age'];
$id = mysqli_real_escape_string($conn,$row['FirstName']);
?>
<form action="" method="post">
<table border="0" cellspacing="10">
<tr>
<td>Age:</td>
<td><?php echo $age;?></td>
<td><?php echo $id;?></td>
<td><input type="text" name="Age"></td>
<td><input type="text" name="FirstName" value="<?php echo $id;?>"><?php echo $id;?></input></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
</form>
<?php
}
?>
如果您的变量是数字,那么更新语句不需要单引号。我只纠正了Age,因为我不确定名字。此外,PHP不需要将变量串联到由双引号括起来的字符串中。
希望这有帮助。
编辑:当您转义字符串时,请务必使用函数的mysqli版本而不是mysql,因为后者已被弃用。