gpt4 book ai didi

php - 错误的原因可能是什么?

转载 作者:行者123 更新时间:2023-11-29 17:32:07 25 4
gpt4 key购买 nike

我正在编写代码来从数据库获取数据,但它不断标记错误:

Notice: Undefined variable: word in C:\xampp\htdocs\Zubby\admin\updateword.php on line 7

<?php 

if(isset($_POST['submit']))
$word = $_POST['word'];

include '../include/config.php' ;
$sql = "SELECT FROM word WHERE word = $word";

$fetch = mysqli_query($connect, $sql);
if ($fetch) {
echo $word;
}else{
echo "No such word exists in the database";
}

?>

<form name="word-Add" method="POST" action="">
<input type="text" name="word" placeholder=" Type the Word "><br><br><br><br>
<input type="submit" name="submit" id="submit" value="Search Word"><br><br><br><br>

</form>

最佳答案

尝试

<?php 

if(isset($_POST['submit']))
{
$word = $_POST['word'];

include '../include/config.php' ;
$sql = "SELECT * FROM word WHERE word = '".$word."'";

$fetch = mysqli_query($connect, $sql);
if ($fetch) {
echo $word;
}else{
echo "No such word exists in the database";
}
}

?>

<form name="word-Add" method="POST" action="">
<input type="text" name="word" placeholder=" Type the Word "><br><br><br><br>
<input type="submit" name="submit" id="submit" value="Search Word"><br><br><br><br>

</form>

请注意,您的代码非常不安全!切勿在查询中直接使用用户输入!更好地使用准备好的语句:http://php.net/manual/en/pdo.prepared-statements.php

关于php - 错误的原因可能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556407/

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