php - 使用 HTML 表单更新 MySQL

标签 php html mysql

我正在尝试创建一个表单,允许您使用 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

我已检查数据库,但它没有更新任何内容。

最佳答案

亲爱的,我认为您根据 Name 更改记录,因为您可以在 where 子句中使用 $name,并且还可以更改 Name 而不是 true where 子句,以便您的查询成功执行,但未影响任何行。

you want to get for editable record and that's unique id base update row it will defiantly work.

关于php - 使用 HTML 表单更新 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40436849/

相关文章:

javascript - 如何检查具有特定类名的元素是否可见?

Heroku mysqldump 到远程主机

javascript - CORS 在 php slim 框架中不工作

php - 添加 css 以打印或回显

jquery - 如何在点击表格数据时设置背景颜色?

mysql - 用于在 Highcharts 图表上显示记录的 SQL 查询

php - mysqli::mysqli(): (HY000/2002): 无法通过socket 'MySQL'连接到本地MySQL服务器(2)

javascript - 从查询sql数据库获取数据到javascript

PHP 数组查找对象的索引

javascript - Jquery 滑出菜单项