php pdo语法错误或访问冲突:1064

时间:2015-03-01 22:24:53

标签: php mysql pdo

我在尝试更新MySql数据库中的表值时遇到了php文件的问题。我已经尝试了不同的方法来克服这个问题,但即使在查看堆栈溢出和谷歌之后也没有成功!

我有一个模式从php填充的表中获取信息,然后我尝试使用php PDO更新course_title的值但是我不断收到此错误:

'消息:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第2行'WHERE course_code = NULL'附近使用正确的语法

如果有人能够对我做错了什么发光,我会非常感激!

php文件:

<?php

    if(!isset( $_POST['course_title'], $_POST['course_code']))
    {
$message = $_POST['course_title'] ." ". $_POST['course_code'];
    }

include "db_conx.php";

try
{
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = $db_conx->prepare("UPDATE course_details SET course_title = :course_title,  
    WHERE course_code = :course_code");

$course_title = $_POST['course_title'];
$course_code = $_POST['course_code'];

$sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
$sql->bindParam(':course_code', $course_code, PDO::PARAM_STR);


/*** execute the prepared statement ***/
$sql->execute();

/*** success message ***/
 $message ='record updated';
 } 
 catch(Exception $e)
 {
$message = 'Message: ' .$e->getMessage();
}

 ?>
<html>
<head>
<title>Update Course</title>
</head>
 <body>
<p><?php echo $message; ?>
</body>
    </html>

模态:

    <div id="editModel" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
 <div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Update Record: </h4>
    </div>
    <div class="modal-body">
        <p class="text-primary">Please make any changes and click 'update' to save or 'cancel' to return</p>
        <form id="updateForm">
            <div class="form-group">
                <label for="course_code" class="control-label">Course Code:</label>
                <input type="text" class="form-control" id="course_code" readonly="">
            </div>
            <div class="form-group">
                <label for="course_title" class="control-label">Course Title:</label>
                <input type="text" class="form-control" id="course_title">
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button name="cancel" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button name= "update" value="update" type="submit" class="btn btn-primary" onclick="myCall();">Update</button>
    </div>
</div>

非常感谢:)

**更新 - 由于@ Rizier123已解决了初始语法问题,但我现在遇到的问题是'未定义索引:course_title''未定义索引:course_code'错误此代码:

    $course_title = $_POST['course_title'];
    $course_code = $_POST['course_code']; 

任何想法为什么会这样?我已将输入值与PDO准备好的语句相匹配

1 个答案:

答案 0 :(得分:0)

尝试使用name属性而不是id:

<input type="text" class="form-control" id="course_code_id" name="course_code" readonly="">

id属性应该用于css中的样式,并且应该使用name属性以便在发布表单时使用PHP获取值。

同样在你的php脚本中你应该检查是否设置了这些值,比如@ chris85指向。

相关问题