简体   繁体   中英

Getting syntax error for sqlsrv_query

I am new to SQL . For my PHP application I am using HTML, PHP and SQL Below code for add the insert query.

$insert_query = "INSERT INTO policy_request (user_name, user_mobile, user_email, user_car_model, user_policy_expiry_date, request_from, policy_created_on) VALUES (?,?,?,?,?,?,?)";
$params = array('".$user_name."', $user_mobile, '".$user_email."', '".$user_car_model."', '".$policy_date."', '".$request_url."', '".$created_on."');
if(!sqlsrv_query($conn,$insert_query,$params)){
    //echo "inside";exit; 
    die(print_r(sqlsrv_errors())); exit;
}else{
    echo "rows inserted";
    exit;
}

I am getting error like this

 [0] => Array
    (
        [0] => IMSSP
        [SQLSTATE] => IMSSP
        [1] => -14
        [code] => -14
        [2] => An invalid parameter was passed to sqlsrv_query.
        [message] => An invalid parameter was passed to sqlsrv_query.
    )

I dont know where I made mistake. FYI db connection established.

By the look of your parameters array, you appear to be trying to pass the parameters as one long string, with the values separated by commas. This is incorrect.

Firstly, your attempt to do so is incorrect. You have a few missing quotes - missing first and last " in the string, and missing quotes and concatenation around $user_mobile .

Secondly, you are only sending one parameter (the string), when the query is expecting seven.

$params should be an array with the same amount of values as you have placeholders ( ? ) in your query. In this case you have seven placeholders, therefore you should have seven values in the $params array. Like so:

$params = array($user_name, $user_mobile, $user_email, $user_car_model, $policy_date, $request_url, $created_on); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM