[英]Updating data from submission form into MySQL database
I am trying to update data from a submission form into the MySQL database using PHP. 我正在尝试使用PHP将提交表单中的数据更新到MySQL数据库中。 When I click on the button to update the value in the database, it becomes empty. 当我单击按钮以更新数据库中的值时,它变为空。
Additionally, I also receive the following two error messages : 此外,我还收到以下两个错误消息:
Notice: Undefined variable: id in C:\\xampp\\htdocs\\test1\\edit.php on line 64 注意:未定义的变量:第64行的C:\\ xampp \\ htdocs \\ test1 \\ edit.php中的id
Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\test1\\edit.php on line 66 警告:第66行的C:\\ xampp \\ htdocs \\ test1 \\ edit.php中为foreach()提供了无效的参数
The following is my PHP Code: 以下是我的PHP代码:
<?php
$name = '';
if (isset($_GET['editQ'])) {
$ok = true;
$id = $_GET['editQ'];
if ($ok) {
// add database code here
$db = mysqli_connect('localhost', 'root', '', 'test2015');
$sql = sprintf("UPDATE question SET question_body='%s'
WHERE question_id=%s",
mysqli_real_escape_string($db, $name),$id);
mysqli_query($db, $sql);
echo '<p>User updated.</p>';
mysqli_close($db);
}
} else {
$db = mysqli_connect('localhost', 'root', '', 'test2015');
$sql = sprintf('SELECT * FROM question WHERE question_id=%s', $id);
$result = mysqli_query($db, $sql);
foreach ($result as $row) {
$name = $row['question_body'];
}
mysqli_close($db);
}
?>
<form name="editQ" method="POST" action="edit.php" >
<td>Please Edit the Question</td>
<input type="text" name="<?php echo ($q)?>" value="<?php
echo htmlspecialchars($name);?>" />
<input type="submit" name="submit" value="Edit">
</form>
Any help/advice would be much appreciated. 任何帮助/建议将不胜感激。 Thanks in advance. 提前致谢。
您的表单正在发送POST,并且您正在尝试使用GET获取值。
$id
is not being set before the SQL statement is being called. 在调用SQL语句之前未设置$id
。 It's in the else
section of the if
statement isset($_GET['editQ'])
which defines $id
; 在if
语句isset($_GET['editQ'])
的else
部分中,该语句定义$id
;
Also instead of foreach
use the following with a mysqli result 另外,代替foreach
将以下内容与mysqli结果一起使用
while($row = $result->fetch_assoc())
{
// STUFF
}
You don't initialize $id if it is not set in $_GET['editQ']. 如果未在$ _GET ['editQ']中设置$ id,则不进行初始化。 If however in that case you still need it, I guess you know some default id for that instance. 但是,如果在这种情况下仍然需要它,我想您知道该实例的一些默认ID。 Define it then at the top of the script in case it is later not overwritten by $_GET value. 然后在脚本顶部定义它,以防以后不被$ _GET值覆盖。
Try pulling the $id
assignment outside the if
statement: 尝试将$id
分配拉到if
语句之外:
<?php
$name = '';
if (isset($_GET['editQ'])) {
$ok = true;
$id = $_GET['editQ'];
if ($ok) {
// add database code here
$db = mysqli_connect('localhost', 'root', '', 'test2015');
$sql = sprintf("UPDATE question SET question_body='%s'
WHERE question_id=%s",
mysqli_real_escape_string($db, $name),
$id);
mysqli_query($db, $sql);
echo '<p>User updated.</p>';
mysqli_close($db);
}
else {
$db = mysqli_connect('localhost', 'root', '', 'test2015');
$sql = sprintf('SELECT * FROM question WHERE question_id=%s', $id);
$result = mysqli_query($db, $sql);
foreach ($result as $row) {
$name = $row['question_body'];
}
mysqli_close($db);
}
}
?>
$id
from $editQ
.
编辑:上面的代码清除了未设置的变量错误,但用户仍在努力将$id
与$editQ
区分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.