我使用php向 table.ive插入变量$email、$starcount、$bookid固定值,现在只是为了测试文件。$res查询检查其中是否有带有该图书id和电子邮件的行。如果没有$sql查询,则$nex查询循环使用任何starcount列,其中book列=$bookE 219。
如果我更改文件顶部的电子邮件,它应该插入到新的信息数据库中,并取出新的和现有的星号,但它不会发布,它只是返回已经存在的星号。我不明白为什么它不起作用..。我使用数组返回到我的文件。
<?php
mysql_connect("localhost","root","");
mysql_select_db("FunReads");
$email = "sd";
$starcount = "2";
$bookid = "5";
$res = mysql_query("SELECT * FROM Review WHERE book_id='$bookid' AND user_email='$email'");
if (mysql_num_rows($res) != 0) {
$array[]= array("starcount" => "already entered");
} else {
$sql = mysql_query("INSERT INTO Review(book_id,starcount,user_email) values('.$bookid.','.$starcount.','.$email')");
$nex = mysql_query("SELECT * FROM Review WHERE book_id='$bookid'");
while($row = mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
//echo "[{"name":"n1","city":"NY"},{"name":"n2","city":"Paris"}, ...]?>
发布于 2016-04-18 05:27:41
在我看来,"Review“表中的"book_id”是主键,因为您多次尝试添加它,所以系统会显示错误重复键。检查错误并张贴。还请检查insert查询是否工作。
发布于 2016-04-18 05:38:21
您不应该手动传递主键值,它将帮助您。
<?php
mysql_connect("localhost","root","");
mysql_select_db("FunReads");
$starcount="2";
$email = "vinodh@gmail.com";
$res=mysql_query("SELECT * FROM Review WHERE email ='$email'");
if(mysql_num_rows($res)!=0){
$array[]= array("starcount" => "already entered");
}else{
$sql=mysql_query("INSERT INTO Review (starcount,email) values('.$starcount.','.$email')");
$nex=mysql_query("SELECT * FROM Review WHERE email='$email'");
while($row=mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
?>发布于 2016-04-18 11:57:05
我刚刚更新了你的代码,它对我很好。
<?php
mysql_connect("localhost","user","");
mysql_select_db("xyz");
$email = "hari@gmail.com";
$starcount = "2";
$bookid = "5";
$sql = "SELECT * FROM review WHERE book_id='$bookid' AND user_email='$email'";
$res = mysql_query($sql);
if (mysql_num_rows($res) != 0) {
$array[]= array("starcount" => "already entered");
} else {
$sql = "INSERT INTO review(book_id,starcount,user_email) values('$bookid','$starcount','$email')";
$sql = mysql_query($sql);
$nex = mysql_query("SELECT * FROM review WHERE book_id='$bookid'");
while($row = mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
示例输出:
{“星号”:“2”},{“星号”:“3”},{“星号”:“1”},{“星号”:“2”},{“星号”:“1”}
我更新了insert查询,请尝试更新并测试。
https://stackoverflow.com/questions/36685994
复制相似问题