gpt4 book ai didi

php - 'field entry' 中的未知列

转载 作者:行者123 更新时间:2023-11-29 10:13:53 26 4
gpt4 key购买 nike

我正在尝试通过 PDO 插入 mysql 表。当我运行代码时,出现错误: 未找到列:1054 当使用测试作为下面 Html 代码中第一行的第一个输入时,“字段列表”中的未知列“测试”。`

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO test (Name, Test) VALUES ($first, $Test)";

$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="page_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Test </strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>test</td>
<td>:</td>
<td><input name="Test" type="text" id="Test"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>

</tr>
</table>
</form>
</td>
</tr>
</table>

最佳答案

看来您没有正确转义这些值。我将使用 PDO 的方法来准备插入的值,然后使用这些值作为数组执行查询。以下代码将输入值转义为 PDO 中的字符串。

// replace your variables with question marks
$sql = "INSERT INTO test (Name, Test) VALUES (?,?)";
// prepare the sql to execute and get passed back a resource
$stmt = $conn->prepare($sql);
// pass the parameters as you would of in the query, in the array
// first variable matches first question mark and so forth
$stmt->execute(array($first,$Test));

始终建议您在查询中使用用户输入之前对其进行转义和清理,否则可能会发生 SQL 注入(inject)。 PDO 的 prepare方法对此有所帮助。

关于php - 'field entry' 中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50440347/

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