我收到错误,我无法解决.. PHP中的SQL错误

时间:2015-01-13 03:57:24

标签: php mysql sql ajax

我收到错误,我无法解决..

response error:
Database query failedYou have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near '' as Loreta Punzalan'}, 
gender  = {'Female'}, dob = {'01/23/20' at line 2 

JS AJAX

$("button.update_btn").click(function(event){
        //alert("test");
        var clickedEditid = this.id.split('-');
        var dbId = clickedEditid[1];
        var mydata = '&recordToBeUpdated='+ dbId;
        var mydatas = $('#instructorUps').serialize();
            var datas = mydatas+mydata;
        //alert(datas);
        $.ajax({
            type:"POST",
            url:"<?php echo get_stylesheet_directory_uri(); ?>/includes/response_instructor_update.php?>",
            dataType:"text",
            data: datas,
            success: function(response){
                alert(response);
            // $('#ins_'+dbId).fadeIn();
                //$('#instructorModal').append(response);
                location.reload();
                },
            error:function(xhr, ajaxOption, thrownError){
                alert(thrownError);
                }

            });
        });

Response_instructor_update.php

<?php
// Response for AJAX

require_once("connection.php");
require_once("functions.php");
if(isset($_POST['fullnameup'])){

        $idupdate     =   mysql_prep($_POST["recordToBeUpdated"]);
        $fullname     =   mysql_prep($_POST["fullnameup"]);
        $username     =   mysql_prep($_POST["usernameup"]);
        $gender       =   mysql_prep($_POST["genderup"]);
        $dob          =   mysql_prep($_POST["dobup"]);
        $nationality  =   mysql_prep($_POST["nationalityup"]);
        $mobile       =   mysql_prep($_POST["mobileup"]);
        $emailadd     =   mysql_prep($_POST["emailaddup"]);
        $address      =   mysql_prep($_POST["addressup"]);
        $zipcode      =   mysql_prep($_POST["zipcodeup"]);
$query  = "UPDATE instructor SET    
            instructor    = {'$fullname'},
            gender        = {'$gender'},
            dob           = {'$dob'},
            nationality   = {'$nationality'},
            mobile       = {$mobile},
            emailadd     = {'$emailadd'},
            address      = {'$address'},
            zipcode      = {$zipcode},
            username     = {'$username'}
            WHERE instructorid= {$idupdate}";   
    $updatedInstructor = mysql_query($query,$connection);
    confirm_query($updatedInstructor);

    if (mysql_affected_rows() == 1){

        echo "TRUE";
        mysql_close($connection);
        }else{
            die("Database query failed". mysql_error());

            } 
    }
    ?>

1 个答案:

答案 0 :(得分:0)

我认为你有正确的想法。但是,您有轻微的语法问题。

我相信你的变量没有评估字符串,MySQL期待字符串。请尝试以下方法:

$query  = "UPDATE instructor SET    
        instructor    = '{$fullname}',
        gender        = '{$gender}',
        dob           = '{$dob}',
        nationality   = '{$nationality}',
        mobile       = {$mobile},
        emailadd     = '{$emailadd}',
        address      = '{$address}',
        zipcode      = {$zipcode},
        username     = '{$username}',
        WHERE instructorid= {$idupdate}"; 

请注意,单引号(&#39;)是大括号({})的OUTSIDE。您必须在MySQL期望字符串的任何位置添加引号(列类型为text / varchar / etc ..)。

- 更新 -

正如斯维里所说,他们实际上被视为字符串。这里的问题是php的插值有微小的变化:你可以在双引号(&#34;)字符串中使用{$variable}或简单地$variable。对于您的帖子,您有{'$variable'}使用$variable方法进行插值,只需将大括号视为字符串的一部分,当您有意替换PHP时。