“字段输入”中的未知列

anauzrmj  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(304)

我正在尝试通过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>
cgfeq70w

cgfeq70w1#

看起来您没有正确地转义这些值。我将使用pdo的方法来准备insert的值,然后将这些值作为数组执行查询。下面的代码将在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注入。pdo的prepare方法对此很有帮助。

相关问题