我试图创建一个允许您使用php更新数据库表的表单。 我是PHP的新手,请原谅我,如果我在代码中犯了一个愚蠢的错误。
这是我的edit.php代码:
<html>
<head>
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","root","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM cats");
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<?php
while($row = mysqli_fetch_array($result))
{
$name = $row['name'];
$email = $row['email'];
$rank = $row['rank'];
$birth = $row['birth'];
$joined = $row['joined'];
$steamid = $row['steamid'];
?>
<td width="100"></td>
<td><?=$name?></td>
</tr>
<tr>
<td width="100">Email</td>
<td><input name="emailid" type="text" value="<?=$email?>"></td>
</tr>
<tr>
<td width="100">Rank</td>
<td><input name="rankid" type="text" value="<?=$rank?>"></td>
</tr>
<tr>
<td width="100">Birth</td>
<td><input name="birthid" type="text" value="<?=$birth?>"></td>
</tr>
<tr>
<td width="100">Joined</td>
<td><input name="joinedid" type="text" value="<?=$joined?>"></td>
</tr>
<tr>
<td width="100">Steamid</td>
<td><input name="steamidid" type="text" value="<?=$steamid?>"></td>
</tr>
<?php } ?>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['update']))
{
$name = $row['nameid'];
$email = $row['emailid'];
$rank = $row['rankid'];
$birth = $row['birthid'];
$joined = $row['joinedid'];
$steamid = $row['steamidid'];
$update = mysqli_query($con,"UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';");
$retval = mysqli_query($con,"UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';");
if (!$update) {
echo "Could not update data: " . mysqli_error($con);
}
echo "Updated data successfully\n";
}
mysqli_close($con);
?>
</body>
</html>
它显示表格和信息,但更新不起作用。
Updated data successfully
我已经检查了数据库,但没有更新任何内容。
答案 0 :(得分:1)
亲爱的,我认为您可以根据名称更改记录,因为您可以在where子句中使用$ name,也可以更改名称而不是真正的where子句,以便您的查询已成功执行但未在任何行上生效。
您希望获得可编辑记录以及该功能的唯一ID基准更新行。
答案 1 :(得分:0)
<强>信息:强>
使用mysqli_error()
,您需要写下您想要获得错误的连接,例如:
mysqli_error($con);
使用mysqli_query()
,您需要提供两个参数connection
和query
,如下所示:
$update = mysqli_query($con,"UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';");
如何调试:
如果您想检查UPDATE
查询是否返回任何错误,您可以执行以下操作:
if (!$update) {
echo "Could not update data: " . mysqli_error($con);
}
您可以尝试使用以下内容调试查询:
$sql = "UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';";
echo $sql; // this output write in your phpMyadmin to check if there are any errors.
$update = mysqli_query($con, $sql);
我们遇到的其他问题:
1。我认为您的代码中应该有else
,f.ex。:
if (!$update) {
echo "Could not update data: " . mysqli_error($con);
} else {
echo "Updated data successfully\n";
}
2。您无法从$_POST
获取数据:
$name = $_POST['nameid']; // not $row['nameid']
$email = $_POST['emailid'];
$rank = $_POST['rankid'];
$birth = $_POST['birthid'];
$joined = $_POST['joinedid'];
$steamid = $_POST['steamidid'];
有关已使用功能的更多信息:
在您的情况下,它是程序风格
答案 2 :(得分:0)
尝试使用PHP PDO数据库访问函数,您的代码很容易受到SQL注入攻击! PDO还将使调试和使用数据库变得更加容易。
我认为您在$ _POST中检查“更新”是行不通的,因为更新不是表单中的字段,而是提交按钮本身,请尝试检查其中一个字段。