我一直收到此错误
致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第6行的C:\ wamp \ www \ notaryaccounting \ contact.php中使用“第1行”附近的正确语法
我无法弄明白。在contact.php中,如果我将$ _POST ['parentVal']替换为数字1,则脚本可以正常工作。所以它与从jquery脚本传递parentVal变量有关。
当我使用$ _GET ['parentVal']进行设置时,我使用开发人员工具,我可以看到变量在那里,所以脚本应该可以工作,但事实并非如此。
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function(){
$('#parent').change(function(){ //on change event
var parentVal = $('#parent').val(); //<----- get the value from the parent select
$.ajax({
url : 'contact.php', //the url you are sending datas to which will again send the result
type : 'POST', //type of request, GET or POST
data : { parentValue: parentVal}, //Data you are sending
success : function(data){$('#child').html(data)}, // On success, it will populate the 2nd select
error : function(){alert('an error has occured')} //error message
})
})
})
</script>
</head>
<body>
Customer:
<select name="customer" id="parent">
<option>-Select a Customer-</option>
<?php
include("connect.php");
$pid = $_SESSION['profile']['id'];
foreach($db->query("SELECT * FROM customers WHERE pid = '$pid'") as $row) {
echo "<option value=" . $row['id'] . ">" . $row['name'] . "</option>";
}
?>
</select>
Contact:
<select name="contact"id="child"/>
<option>-Select a Contact-</option>
</select>
</body>
</html>
<?php
include("connect.php");
$custid = $_POST['parentVal'];
foreach($db->query('SELECT * FROM contact WHERE custid =' . $custid ) as $row) {
$results.=("<option value=" . $row['id'] . ">" . $row['name'] . "</option>");
}
echo $results;
答案 0 :(得分:1)
您使用parentValue
名称发送数据,并在服务器中使用parentVal
。更改名称。
$_POST['parentValue'];
不
$_POST['parentVal']
此外,您还有SQL语法错误:
变化
$db->query('SELECT * FROM contact WHERE custid =' . $custid
要:
$db->query("SELECT * FROM contact WHERE custid='$custid'")