我这里的代码不会更新。如果我在输入文本中输入任何内容并按提交,那么' samp.php'仍会显示我已更新的数据库的原始信息。
index.php的代码
<?php
include 'connect.php';
?>
<html>
<head>
<title>Basic Form Handling PHP</title>
</head>
<body>
<?php
$result1 = $db->query("select * from chapter where chapter_id='1'");
while($row1 = mysqli_fetch_assoc($result1)){ echo "<form action='samp.php' method='POST'>";
echo " <input class='input' type='hidden' name='cid' value='{$row1['chapter_id']}'/>";
echo " Title: <br>";
echo " <input type='text' name='ctitle'><br>";
echo " Body: <br>";
echo " <input type='text' name='cbody'><br>";
echo " <input type='submit' name='submit' value='PRESS ME'>";
echo " </form>";
}
?>
</body>
</html>
connect.php的代码
<?php
$db = new mysqli('localhost', 'root', '2830775', 'unity');
?>
代码为samp.php
<?php
include 'connect.php';
?>
<?php
$id = $_POST['cid'];
$title = $_POST['ctitle'];
$body = $_POST['cbody'];
$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id");
$result2 = $db->query("SELECT * FROM chapter where chapter_id='$id'");
?>
<?php $row1 = mysqli_fetch_assoc($result2);
echo $row1['chapter_body'];
?>
答案 0 :(得分:1)
您需要将连接文件包含在same.php
<?php
include 'connect.php';// incliude your connection file
$id = $_POST['cid'];
$title = $_POST['ctitle'];
$body = $_POST['cbody'];
$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id = '$id'");//missing Singal quote in end
$result2 = $db->query("SELECT * FROM chapter where chapter_id='$id'");
?>
<?php
$row1 = mysqli_fetch_assoc($result2);
echo $row1['chapter_body'];
?>
答案 1 :(得分:0)
将$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id")
更新为$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id'")
你忘了在那里结束单引号。