0

This is the js\jquery function which is requesting

function AssignWork(){
    var projectId = $jq(".dmProjName").val(),
        empId = $jq(".nameEmp").val(),
        assignWork = $jq(".workDescription").val(),
        workDate = $jq(".workDate").val();
    var go_path = "Employee_Switch_Person.php?action=assignWork&vars=4&var1="+empId+"&var2="+projectId+"&var3="+assignWork+"&var4="+workDate;
    $jq.get(go_path,{},function(data){
        if(data ==1){
            alert("Successfully Assigned!");
            showAssignWork(0);
        }
    });
}

this is php

function assignWork($empId,$projectId,$assignWork,$workDate){
    //echo $workDate;
    global $con;
    date_default_timezone_set("Asia/Karachi");
    //echo "date format".date('Y-m-d H:i:s');
    //echo $empId.",".$projectId.",".$assignWork.",".$workDate;
    $sql = "INSERT INTO `tblempassignwork` (`EmpId`, `AssignWork`, `AssignById`, `ProjectId`, `WorkDate`, `AssignDateTime`)
          VALUES($empId,'".$assignWork."',".$_COOKIE["userID"].",".$projectId.",'".$workDate."','".date('Y-m-d H:i:s')."')";
    $result = mysql_query($sql,$con) or die(mysql_error());
    echo $result;
}

problem is that

 assignWork = $jq(".workDescription").val()

can contain a string with double quote, single quote, hash or any special character. if i use single quote or hash then it is showing

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 's and makes some examples',3,77,'2015-05-08','2015-05-08 09:51:17')' at line 2

cause i have type single quote in string. so how can i skip special character when passing through get request.

Mohammad Faizan khan
  • 1,213
  • 3
  • 17
  • 32

3 Answers3

0

In the jQuery, you can replace all the non-alphanumeric characters with -

assignWork=assignWork.trim().replace(/[^a-z0-9]+/gi, '-');

Or you can replace it with whitespace or what ever suites your program the best :)

Manikiran
  • 2,618
  • 1
  • 23
  • 39
0

If you want to exclude special characters

solution is already in stackoverflow javascript regexp remove all special characters

var outString = sourceString.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
Community
  • 1
  • 1
gaurav bhavsar
  • 2,033
  • 2
  • 22
  • 36
0

Change this:

assignWork = $jq(".workDescription").val(),

To this:

assignWork = escape($jq(".workDescription").val()),

IMHO isn't safe to store te unscaped string in your database, and when you read it back in javascript, you cand just "unescape" it.

JavaScript unescape() Function

otherwise, if you want to, just decode it in the PHP using:

$assignWork = urldecode($assignWork);

Javascript's “unescape” in PHP

Community
  • 1
  • 1
KodornaRocks
  • 425
  • 3
  • 14