无法使用php更新MySQL中的列

时间:2015-06-09 04:49:22

标签: php mysql

我正在尝试更新服务器中存在的MySql数据库列。我没有弄到我在查询中犯的错误,列没有得到更新。任何人都可以对我的错误有所了解。我尝试过所有可行的方法,但我无法成功。

我已尝试过以下所有查询:

$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. "'";

在上述所有查询中,我得到的响应为1,但我的列没有使用该值进行更新。

下面是我的php脚本:

<?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();
?>

我正确地传递了值,我也试过打印这些值。欢迎所有建议。提前致谢。

2 个答案:

答案 0 :(得分:4)

'开始后您有一个额外的空格 - Subject =' " .$obtainedSubject. "'"。试试 -

Subject ='" .$obtainedSubject. "'"

查询将是 -

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

答案 1 :(得分:1)

如下修改

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

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