SQL插入不保存到数据库

时间:2015-05-08 18:29:24

标签: php mysql insert

我无法让这个简单的代码工作,它是产品页面评级脚本的一部分。

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

$vresult1 = mysqli_query($connn,"SELECT * FROM rating_log WHERE ip=$userIP AND product_id=$pid");

if ($vresult1->num_rows > 0) {
    // ERROR: user already voted
    $v_msg = '<div style="color:red;">You have already voted on this product!</div>';
} else {
    // enter vote
    mysqli_query($connn,"UPDATE wc_products SET rating=$votecnt1");
    mysqli_query($connn,"INSERT INTO rating_log (ip, product_id) VALUES ($userIP, $pid)");
    $v_msg = '<div style="color:green;">Thank you for voting! DEBUG['.$userIP.'-'.$pid.']</div>';
}
$conn->close();
} // end rating

所有这一切应该是添加一个记录用户ip和产品ID的新条目,然后更新产品db以注册投票。更新工作正常,但它不会记录用户。

1 个答案:

答案 0 :(得分:1)

试试这个:

$userIP = $mysqli->real_escape_string($userIP);
$pid = $mysqli->real_escape_string($pid);
 mysqli_query($connn,"INSERT INTO rating_log (ip, product_id) VALUES ('$userIP', '$pid')");

单引号应该用于VALUES()列表中的字符串值。有关详细信息,请阅读此帖子的答案 When to use single quotes, double quotes, and backticks in MySQL