Php 连接失败:SQLSTATE[42000]:语法错误或访问冲突:1064
我已经看到了很多与我类似的问题,但我的问题不同:我的错误不是因为在SQL中使用了Php 连接失败:SQLSTATE[42000]:语法错误或访问冲突:1064,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我已经看到了很多与我类似的问题,但我的问题不同:我的错误不是因为在SQL中使用了“关键字” 运行代码时,如果取消WHERE子句或使用硬编码值,如“WHERE customerNumber=356”,则工作正常,但如果尝试使用变量$custom\n的值,则会抛出错误 <?php $custom_n = $_POST["emp"]; $servername = "localhost"; $username = "root
“关键字”
运行代码时,如果取消WHERE
子句或使用硬编码值,如“WHERE customerNumber=356”
,则工作正常,但如果尝试使用变量$custom\n
的值,则会抛出错误
<?php
$custom_n = $_POST["emp"];
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn -> prepare("SELECT phone, salesRepEmployeeNumber, creditLimit FROM customers WHERE customerNumber = $custom_n");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
如果我对custom_n做一个echo,然后得到我想要的:353字符串
这是全部错误:
连接失败:SQLSTATE[42000]:语法错误或访问冲突:
1064您的SQL语法有错误;检查手册
对应于要使用的正确语法的MariaDB服务器版本
在第1行的“”附近
正如Magnus Eriksson所建议的,如果我进行
var\u转储($custom\n)代码>我得到这个输出:string(12)“
您的代码似乎很好,因此我假设您的字符串逃逸了查询。下次尽量使用事先准备好的语句
您的代码似乎很好,因此我假设您的字符串逃逸了查询。下次尝试使用准备好的语句。首选的方式是绑定。您可以像这样绑定param。你可以参考
首选的方式是绑定。您可以像这样绑定param。你可以参考
正如Magnus Eriksson所说,我的POST方法存在问题。。。即使看起来还可以,我的变量custom有一个奇怪的变量dump输出string(12)“
。我记得我以前使用过相同的代码并且正在工作,所以我只是将代码修改为完全相同:在我有发送“POST变量”表单的页面中,我使用了以下代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$custom_n = array();
try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn -> prepare("SELECT checkNumber, paymentDate, amount, customerNumber FROM payments ORDER BY paymentDate DESC");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
if ($k == 'customerNumber') {
array_push($custom_n, $v);
}
}
$tableCodes = "<table id='buttons'>";
foreach ($custom_n as $c) {
$tableCodes .= "
<tr>
<td>
<form action='customers.php' method='post' target='POPUPW' onsubmit=\"POPUPW = window.open('about:blank','POPUPW', 'width=800px,height=600px');\">
<button type='submit' name='code' value='$c'>Details</button>
</form>
</td>
</tr>
";
}
$tableCodes .= "</table>";
echo $tableCodes;
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
正如Magnus Eriksson所建议的,我的POST方法有问题。。。即使看起来还可以,我的变量custom有一个奇怪的变量dump输出string(12)“
。我记得我以前使用过相同的代码并且正在工作,所以我只是将代码修改为完全相同:在我有发送“POST变量”表单的页面中,我使用了以下代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$custom_n = array();
try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn -> prepare("SELECT checkNumber, paymentDate, amount, customerNumber FROM payments ORDER BY paymentDate DESC");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
if ($k == 'customerNumber') {
array_push($custom_n, $v);
}
}
$tableCodes = "<table id='buttons'>";
foreach ($custom_n as $c) {
$tableCodes .= "
<tr>
<td>
<form action='customers.php' method='post' target='POPUPW' onsubmit=\"POPUPW = window.open('about:blank','POPUPW', 'width=800px,height=600px');\">
<button type='submit' name='code' value='$c'>Details</button>
</form>
</td>
</tr>
";
}
$tableCodes .= "</table>";
echo $tableCodes;
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
您对SQL注入非常开放,应该使用参数化的Prepared语句,而不是像那样将未经处理的用户数据注入查询中。您已经设置了基本设置。在查询中,更改为:customerNumber=:number
,并将执行更改为:$stmt->execute([':number'=>$custom\n])对不起,你能解释得更清楚些吗?这是我第一次使用php,在其他页面中,我使用了相同的语法,它正在工作。。。你能给我一个你所说的参数化准备语句的例子吗?用“.$custom\n.”替换E customerNumber=$custom\n“,或者访问这个链接,你会看到一些例子。它也会像$stmt=$conn->prepare(“从customerNumber=”的客户那里选择电话、salesRepEmployeeNumber、creditLimit”(港币);代码>。您还可以使用param绑定,因为您对SQL注入非常开放,应该使用参数化的Prepared语句,而不是像这样将未被调用的用户数据注入查询中。您已经设置了基本设置。在查询中,更改为:customerNumber=:number
,并将执行更改为:$stmt->execute([':number'=>$custom\n])对不起,你能解释得更清楚些吗?这是我第一次使用php,在其他页面中,我使用了相同的语法,它正在工作。。。你能给我一个你所说的参数化准备语句的例子吗?用“.$custom\n.”替换E customerNumber=$custom\n“,或者访问这个链接,你会看到一些例子。它也会像$stmt=$conn->prepare(“从customerNumber=”的客户那里选择电话、salesRepEmployeeNumber、creditLimit”(港币);代码>。您还绑定了param,本来要发布答案,但赢得了比赛:)嗨,这并不能解决问题,我的意思是我没有错误,但查询没有返回数据,然后没有要返回的数据,或者您有错误,您只是不显示它。@FabioMagarelli-Doecho';变量转储(自定义);回声'编码>并使用问题的完整输出更新问题。可能会有一些额外的空白或类似的地方会把事情搞得一团糟。这些通常在执行回显时不可见。哦,等等,我得到的var_转储:string(12)”“
将发布答案,但赢得了比赛:)嗨,这没有解决问题,我的意思是,我没有错误,但没有从查询返回数据。如果没有数据要返回,或者您有错误,您只是不显示它。@FabioMagarelli-Doecho';变量转储(自定义);回声'编码>并使用问题的完整输出更新问题。可能会有一些额外的空白或类似的地方会把事情搞得一团糟。这些通常在执行回显时不可见。哦,等等,使用我得到的var_转储:string(12)”“
hi,这不能解决问题,我的意思是我没有错误,但是没有从query@MagnusEriksson我已经更新了我的答案。请让我知道建议删除第一个答案@SudharshanNair它不应该在这里我得到一个错误:致命错误:未捕获错误:调用/Applications/XAMPP/xamppfiles/htdocs/ASS3/customers.php中未定义的方法PDO::bindpram()。51堆栈跟踪:#0{main}第51行的/Applications/XAMPP/xamppfiles/htdocs/ASS3/customers.php中抛出的bindParam()
应该在语句$stmt
上,而不是连接$conn
。所以它应该是:$stmt->bindParam(':customernumber',$custom_n,PDO::PARAM_INT)代码>嗨,这不能解决问题,我的意思是我没有错误,但是没有从query@MagnusEriksson我已经更新了我的答案。请让我知道建议删除第一个答案@SudharshanNair它不应该在这里我得到一个错误:致命错误:未捕获错误:调用/application中未定义的方法PDO::bindParam()
<?php
$servername = "localhost";
$username = "root";
$password = "";
$custom_n = array();
try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn -> prepare("SELECT checkNumber, paymentDate, amount, customerNumber FROM payments ORDER BY paymentDate DESC");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
if ($k == 'customerNumber') {
array_push($custom_n, $v);
}
}
$tableCodes = "<table id='buttons'>";
foreach ($custom_n as $c) {
$tableCodes .= "
<tr>
<td>
<form action='customers.php' method='post' target='POPUPW' onsubmit=\"POPUPW = window.open('about:blank','POPUPW', 'width=800px,height=600px');\">
<button type='submit' name='code' value='$c'>Details</button>
</form>
</td>
</tr>
";
}
$tableCodes .= "</table>";
echo $tableCodes;
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn -> prepare("SELECT checkNumber, paymentDate, amount, customerNumber FROM payments ORDER BY paymentDate DESC");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
$customQuery = $conn -> prepare("SELECT customerNumber FROM payments ORDER BY paymentDate DESC");
$customQuery -> execute();
$custom = $customQuery->fetchAll(PDO::FETCH_COLUMN);
$tableCodes = "<table id='buttons'>";
foreach ($custom as $c) {
$tableCodes .= "
<tr>
<td>
<form action='customers.php' method='post' target='POPUPW' onsubmit=\"POPUPW = window.open('about:blank','POPUPW', 'width=800px,height=600px');\">
<button type='submit' name='emp' value='$c'>Details</button>
</form>
</td>
</tr>
";
}
$tableCodes .= "</table>";
echo $tableCodes;
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>