S4NDM4N
S4NDM4N

Reputation: 924

Need assistance to find the syntax error in my query

I'm writing a program to manipulating time entries by users. I wrote the below PHP to retrieve user details from the server when the variables are push from a JavaScript.

My problem is the SQL is throwing a fatal error which ways it near the variable :uid.

Here is the complete error I'm getting,

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':uid' at line 1' in ***\TimSheetSystem\bin\Functions\getUsrId.php on line 25

Here is my javascript,

function getUser2(){

var selUser = document.getElementById("uid").value;

if(window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("userSelect").innerHTML=xmlhttp.responseText;
    }
};
xmlhttp.open("POST","../functions/getUsrId.php?uid=getUsr2&selUser="+selUser,true);
xmlhttp.send();

}

And here is the PHP with the query,

$getSelUsr = "SELECT * FROM userlogin WHERE uId = :uid";
$getSelUsrQuery = $dbConnect -> query($getSelUsr);
$getSelUsrQuery -> bindParam(':uid', $_REQUEST["selUser"]);
$getSelUsrQuery -> execute();
$getSelUsrRow = $getSelUsrQuery -> fetch(PDO::FETCH_ASSOC);

echo "<option id= ".$getSelUsrRow["uId"].">".$getSelUsrRow["fName"]." ".$getSelUsrRow["lName"]."</option>";

I looked and checked all the semi's and did try to push the data directly with out the JavaScript but still I'be getting the same error over and over.

Can some one spot what I'm doing wrong.

EDIT1 This is page calls the getUser2 function via a timed function caller.

The main page,

<?php if ($_SESSION["sT"] == "Done"){ ?>
 <body onload="tEditReload2()">
 <div id="divCenter-timeEdit" class="box">
  <label id="sbId" hidden><?php echo $_SESSION["uRole"]?></label>
   <div class="logo-timeEdit">
        <img src="../../images/logo.png" width="142" height="33">
    </div>
    <div id="mainDiv" style="height: 38px;">
        <label for="dPicker">Date:</label>
        <input type="text" id="dPicker" style="margin-left: .5%;" size="10" value="<?php echo $getStermRow["sDate"]; ?>">
        <label for="userSelect" style="margin-left: 2%">Select User:</label>
        <select id="userSelect" style="width:160px; margin-left: .5%;" onchange="usrId(this.id);"></select>
        <input type="text" id="uid" size="1" value="<?php echo $getStermRow["sUid"]; ?>" hidden>
 <input type="button" class="getData" value="Submit" onclick="getData();">
    </div>
    <div id="resultTable"><span id="noDataMsg"></span> </div>2
</div>
</body>

Timed function caller,

function tEditReload2() {
    getUser2();
    setTimeout("getData();",100);
}

Upvotes: 0

Views: 71

Answers (2)

Michael
Michael

Reputation: 673

try to change:

$getSelUsrQuery = $dbConnect -> query($getSelUsr);

to

$getSelUsrQuery = $dbConnect ->prepare($getSelUsr);

Have you dumped the $uid? Is it a valid value?

Upvotes: 3

Sharad Kale
Sharad Kale

Reputation: 971

Try bindValue insted bindParam

$getSelUsrQuery -> bindValue(':uid', $_REQUEST["selUser"]);

Upvotes: 1

Related Questions