0
$sql = "insert into dbo.".$st_tab." (bike_id,st_id";
        if($st_tab === "st1_rec_id"||$st_tab === "st2_rec_id"||$st_tab ==="st3_rec_id")
        {
            $sql .= ",ln_id";
        }
        $sql .= ",times,date) values (?,?";
        if($st_tab === "st1_rec_id"||$st_tab === "st2_rec_id"||$st_tab ==="st3_rec_id")
        {
            $sql .= ",?";
        }
        $sql .= ",?,?)";
        echo $sql;
        if($st_tab === "st1_rec_id"||$st_tab === "st2_rec_id"||$st_tab ==="st3_rec_id")
        {
            $var = array($bike_id,$station,$line,$time,$date);
        }
        else
        {
            $var = array($bike_id,$station,$station,$time,$date);
        }
        $insert = sqlsrv_query($conn,$sql,$var);
        if($insert === false)
        {
            die(print_r(sqlsrv_errors(),true));
        }
        else
        {
            foreach($var as $x => $a)
            {
                echo $x." : ".$a." ";
            }
            echo "<br> 1 Record Added";
        }

due that code I got an query error

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'chs_st2_rec'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'chs_st2_rec'. ) )

I can't identify what's error and where it's.

3
  • i've try to print out assembled SQL string after "$sql .= ",?,?)";" i've echo $sql but it's didn't show. thanks Commented Aug 7, 2015 at 9:18
  • Figure out what's actually going on. If you can't print out the SQL string even, then this isn't the right code excerpt. (Notably there's no chs_st2_rec to be seen; yet that's what the error message is about). If this isn't actually the relevant code, you won't get it answered. Please invest a bit more time in adding details (See How to ask, and Writing the perfect question, or MCVE…) Commented Aug 7, 2015 at 9:28
  • ah i've got it it's my fault the code error before this code and i get this done i've edit ** $sql = "select '$st_char' from '$st_tab' where bike_id like '$bike_id'"; ** into **$sql = "select $st_char from $st_tab where bike_id like '$bike_id'"; ** and that's work :) . thanks Commented Aug 7, 2015 at 9:48

1 Answer 1

1

figured it out

the code error before all of these line

reason is i used single quotes with variable

$sql = "select '$st_char'
            from '$st_tab'
            where bike_id like '$bike_id'";

so i remove single quotes out and it's work

thanks everyone :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.