gpt4 book ai didi

php - 通过 PHP 更新 MySQL 不工作

转载 作者:可可西里 更新时间:2023-11-01 07:58:13 25 4
gpt4 key购买 nike

我的网页上有这段运行 PHP 的代码。我一定有一件小事不对,因为当我点击此页面表单上的提交按钮时,它什么也没做!几个小时以来,它一直让我发疯。

这是表格:

<form method="POST">
<strong><br>
</strong><p><input name="creaturein" type="hidden" value="Goblar"><br>
</p><table style="border: 1px;">
<tbody><tr>
<td></td>
<td>Creature</td>
<td>Stage</td>
<td>Gender</td>
<td>Frozen</td>
</tr>
<tr>
<td rowspan="2"><img src="http://static.eggcave.com/90by90/goblar_2.png"></td>
<td>Goblar</td>
<td><select name="stagein"><option selected="" disabled="">Unspecified</option><option value="Unspecified">Unspecified</option><option value="Stage1">Stage 1(Egg)</option><option value="Stage2">Stage 2</option><option value="Stage3">Stage 3</option><option value="Stage4">Stage 4</option></select></td>
<td><select name="genderin"><option selected="" disabled="">Unspecified</option><option value="Unspecified" selected="">Unspecified</option><option value="Female">Female</option><option value="Male">Male</option></select></td>
<td><select name="frozenin"><option selected="" disabled="">Unspecified</option><option value="Unspecified">Unspecified</option><option value="Yes">Yes</option><option value="No">No</option></select></td>
</tr><tr>
<td colspan="2">Notes: <input name="notesin" type="text" value=""></td>
<td><input name="update" type="submit" id="update" value="Update"></td>
<td><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</tbody></table>
</form>

下面是应该更新表的代码:

 // Info to connect to the Wishlist database
$servername = "****";
$dbusername = "****";
$password = "****";
$dbname1 = "****";
$dbname2 = "****";

// To connect to the database please
$conn = mysqli_connect($servername, $dbusername, $password, $dbname1);

// If unable to connect to the database display this error
if ($conn->connect_error) {
echo "Connection to wishlist failed";
die("Connection failed: " . $conn->connect_error);
}

// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;

if(isset($_POST['update'])){
$stage = $_POST['stagein'];
$gender = $_POST['genderin'];
$frozen = $_POST['frozenin'];
$notes = $_POST['notesin'];
$creature = $_POST['creaturein'];

$sql2 = 'UPDATE $username SET Stage = "$stage" AND Gender = "$gender" AND Frozen= "$frozen"' .
' AND Notes = "$notes" WHERE Creature = "$creature"';
if ($conn->query($sql2) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

// To connect to the database please
$conn2 = new mysqli($servername, $dbusername, $password, $dbname2);

// If unable to connect to the database display this error
if ($conn2->connect_error) {
echo "Connection to Creatures failed";
die("Connection failed: " . $conn2->connect_error);
}

$sql3 = "SELECT Stage$stage FROM Creatures WHERE Name = '$creature'";
if ($conn2->query($sql3) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn2->error;
}

$sql4 = "UPDATE $username SET Picture='$retval' WHERE Creature = '$creature'";
if ($conn->query($sql4) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$conn2->close();
}

而且删除按钮也不起作用。

if(isset($_POST['delete'])){
$stage = $_POST['stagein'];
$gender = $_POST['genderin'];
$frozen = $_POST['frozenin'];
$notes = $_POST['notesin'];
$creature = $_POST['creaturein'];

$sql5 = "DELETE FROM $username WHERE Creature = '$creature' AND Stage = '$stage' AND " .
"Gender = '$gender' AND Frozen = '$frozen' AND Notes = '$notes'";
if ($conn->query($sql5) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}

// Close the connection to the database
$conn->close();

我没有收到任何错误消息。我只是迷路了。紧急求救!

-------------------------------------------- ------------------------------

-------------------------------------------- ------------------------------


我有这个!这在很大程度上是有效的

if(isset($_POST['update'])){
// prepare and bind
$stmt = $conn->prepare("UPDATE " . $username. " SET Stage = ?, Gender = ?, Frozen = ?, Notes = ? WHERE Creature = ?");
$stmt->bind_param('sssss', $stagebind, $genderbind, $frozenbind, $notesbind, $creaturebind);

// set parameters and execute
$stagebind = $_POST['stagein'];
$genderbind = $_POST['genderin'];
$frozenbind = $_POST['frozenin'];
$notesbind = $_POST['notesin'];
$creaturebind = $_POST['creaturein'];
$stmt->execute();

$stmt->close();
exit();
// To connect to the database please
$conn2 = mysqli_connect($servername, $dbusername, $password, $dbname2);

// If unable to connect to the database display this error
if ($conn2->connect_error) {
echo "Connection to Creatures failed";
die("Connection failed: " . $conn2->connect_error);
}

// prepare and bind
$stmt2 = $conn2->prepare("SELECT ? FROM Creatures WHERE Name = ?");
$stmt2->bind_param('ss', $stagebind, $creaturebind);

// set parameters and execute
$creaturebind = $_POST['creaturein'];

$stmt2->bind_result($picture);
$stmt2->fetch();

直到这里。它不会在我的心愿单数据库中保存 $picture 信息。

    // prepare and bind
$stmt3 = $conn->prepare("UPDATE " . $username . " SET Picture = ? WHERE Creature = ?");
$stmt3->bind_param('ss', $picture, $creaturebind);

// set parameters and execute
$creaturebind = $_POST['creaturein'];

$stmt3->execute();
$stmt3->close();
$stmt2->close();
$conn2->close();
}

最佳答案

您忘记添加 <form method="POST"> .默认情况下,当没有添加方法时,html 考虑表单 method='GET'。

更新:您忘记添加用于通知字段类型的第一个参数。见下文:

$stmt->bind_param('sssss',$stagebind, $genderbind, $frozenbind, $notesbind, $creaturebind);

更新 2:移除 exit()。另一个调整,检索列的问号不起作用,只在 where 条件下使用问号。见下文:

// prepare and bind
$stmt2 = $conn2->prepare("SELECT $stagebind FROM Creatures WHERE Name = ?");
$stmt2->bind_param('s',$creaturebind);

关于php - 通过 PHP 更新 MySQL 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38548758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com