-1

I am trying to update a column of my MySql database which is present in server. I am not getting what mistake am I doing in my query, column is not getting updated. Can any one please show some light on my mistake. I have tried doing this all possible ways, but I am not able to succeed.

I have tried all the following queries:

$sql = "UPDATE AndroidTable SET HasLike ='".$obtainedCount."' WHERE Subject =' " .$obtainedSubject. "'";

$sql = "UPDATE AndroidTable SET HasLike ='.$obtainedCount.' WHERE Subject =' " .$obtainedSubject. "'";

$sql = "UPDATE AndroidTable SET HasLike ='$obtainedCount' WHERE Subject =' " .$obtainedSubject. "'";

In all the above queries I get the response as 1, but my column does not get updated with that value.

Below is my php script:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "iFocusBlogs";

       $obtainedSubject = urldecode($_POST['enteredSubject']);
       $obtainedCount = urldecode($_POST['enteredCount']);  



       //print " ==== POST DATA =====
       //userName  : $userName
       //Password : $password
       //Status  : $status;


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "UPDATE AndroidTable SET HasLike ='.$obtainedCount.' WHERE Subject =' " .$obtainedSubject. "'";
//$obtainedCount

$result=mysqli_query($conn,$sql);



if ($conn->query($sql) === TRUE) {
    echo $result ;

} else {
    echo "Error: " . $sql . "<br>" . $conn->error();
}
mysqli_commit($conn);


$conn->close();
?>

I am passing the values properly, I have tried printing those values also. All suggestions are welcome. Thanks in advance.

James Z
  • 12,209
  • 10
  • 24
  • 44
keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • what is $sql output. Try this ' echo $sql;exit; ' And then try to run that directly in mysql client. see if you get any error – KA. Jun 09 '15 at 04:51
  • You are injectable with this code. Separate user input from SQL, use prepared statements. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 Also POST doesn't go through the URL so you should not have URL encoded data in there. – chris85 Jun 09 '15 at 04:59
  • @KiranAmbati Error solved. Printing SQL statement gave me my mistake. Thanks a lot. – keshav kowshik Jun 09 '15 at 04:59

2 Answers2

4

You have an extra space after the ' starts - Subject =' " .$obtainedSubject. "'". Try with -

Subject ='" .$obtainedSubject. "'"

The query will be -

$sql = "UPDATE AndroidTable SET HasLike ='$obtainedCount' WHERE Subject ='$obtainedSubject'";
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

Modify like below

From

$sql = "UPDATE AndroidTable SET HasLike ='.$obtainedCount.' WHERE Subject =' " .$obtainedSubject. "'";

To

$sql = "UPDATE AndroidTable SET HasLike ='$obtainedCount' WHERE Subject ='$obtainedSubject'";
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36