我正在尝试通过pdo插入mysql表。当我运行代码时,我得到错误:column not found:1054 unknown column'testing'in'field list'when using testing as the first input for the first row in the html code below`
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>
1条答案
按热度按时间cgfeq70w1#
看起来您没有正确地转义这些值。我将使用pdo的方法来准备insert的值,然后将这些值作为数组执行查询。下面的代码将在pdo中将输入值作为字符串转义。
通常建议您在查询中使用用户输入之前先对其进行转义和清理,否则可能会发生sql注入。pdo的prepare方法对此很有帮助。