php - 我的脚本中不断出现有关 verable 的错误

标签 php jquery mysql

我不断收到此错误

fatal error :未捕获异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突: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;

最佳答案

您正在使用 parentValue 名称发送数据,并在服务器中使用 parentVal。更改名称。

$_POST['parentValue'];

不是

$_POST['parentVal']

此外,您还有 SQL 语法错误:

改变

$db->query('SELECT * FROM contact WHERE custid =' . $custid

致:

$db->query("SELECT * FROM contact WHERE custid='$custid'")

关于php - 我的脚本中不断出现有关 verable 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133682/

相关文章:

php - Laravel:正确覆盖 SoftDeletes 特性

php - 在 PHP 的邮件命令中使用未经过滤的发布数据是否有任何危险?

php - 我在使用 simplexml 解析 facebook 提要时遇到问题

jquery - 淡出并删除 WordPress 中的一个部分

javascript - Backbone.js - 一个 View 应该如何更新多个 HTML 元素?

javascript - 使用 float 动画前置和附加

php - 从 php select 创建多维数组

mysql - 在MYSQL中选择底部结果

mysql - 计算时间差

php - MySQL 按日期时间字段排序,如果存在相同日期,则使用 Rand()