正如标题所述,我试图将数据插入2个表中,如果表单通过了几个条件(用户登录后,用户之前没有对该产品投票,所有字段都已填写)。
当条件只是:
时,它正在将数据完美地插入到两个表中$sql = "SELECT productid FROM votes WHERE username='$username' LIMIT 1";
但是我意识到,如果用户对任何产品进行了投票,那么他们的用户名就会显示在表格中,如果没有对产品进行投票,他们就会失败。所以我刚补充说:
$sql = "SELECT productid FROM votes WHERE username='$username' AND productid='$id' LIMIT 1";
现在,如果我尝试向数据库提交数据,它总是返回'插入投票表中的错误'消息,但不返回mysql_error(),显然不会在投票表中插入新行,但奇怪的是它会更新产品表。
我无法弄清楚发生了什么,所以如果有人能帮我诊断问题,我真的很感激!这是代码:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if($_POST['slider_surface'] !== "0" && $_POST['slider_edgewear'] !== "0" && $_POST['slider_centering'] !== "0" && $_POST['slider_corners'] !== "0"){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$slider_surface = $_POST['slider_surface'];
$slider_edgewear = $_POST['slider_edgewear'];
$slider_centering = $_POST['slider_centering'];
$slider_corners = $_POST['slider_corners'];
$id = preg_replace('#[^a-z0-9]#i', '', $_GET['id']);
session_start();
$username = $_SESSION['username'];
//check if user has already voted
mysql_select_db('products');
$sql = "SELECT productid FROM votes WHERE username='$username' AND productid='$id' LIMIT 1";
$query = mysql_query( $sql, $conn );
$uname_check = mysql_num_rows($query);
if ($username){
if ($uname_check < 1) {
$sql = "INSERT INTO votes ".
"(username,productid,votesurface,voteedgewear,votecentering,votecorners,datetime) ".
"VALUES('$username','$id','$slider_surface','$slider_edgewear','$slider_centering','$slider_corners', now())";
$retval = mysql_query( $sql, $conn );
$id='';
// Make sure the _GET product ID is set, and sanitize it
$id = preg_replace('#[^a-z0-9]#i', '', $_GET['id']);
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM products WHERE id='$id'") or die(mysql_error());
$product = mysql_fetch_array( $data );
$newvotecount = $product['votecount'] + 1;
$newsum_surface = $product['sumsurface'] + $slider_surface;
$newsum_edgewear = $product['sumedgewear'] + $slider_edgewear;
$newsum_centering = $product['sumcentering'] + $slider_centering;
$newsum_corners = $product['sumcorners'] + $slider_corners;
$sql = "UPDATE products SET votecount='{$newvotecount}', sumsurface='{$newsum_surface}', sumedgewear='{$newsum_edgewear}', sumcentering='{$newsum_centering}', sumcorners='{$newsum_corners}' WHERE id='$id'";
$retval2 = mysql_query( $sql, $conn );
if(! $retval){
die('Error inserting into votes table: ' . mysql_error());
}
else if(! $retval2){
die('Error inserting into products table: ' . mysql_error());
}
$grading_error = 'success';
mysql_close($conn);
} else
$grading_error = 'duplicateuser';
} else
$grading_error = 'nouser';
}
else
$grading_error = 'emptyfields';}
?>
答案 0 :(得分:0)
将数据插入表时出现问题,因此INSERT
语句必定存在错误。
正如@ user2910809在之前的评论中所说,sql
评估为:
INSERT INTO votes (username,
productid,
votesurface,
voteedgewear,
votecentering,
votecorners,
datetime)
VALUES ('magmar',
'52',
'7',
'6',
'5',
'5',
now())
这句话在句法上是正确的。因此,如果出现错误,则必须使用插入的值。
正如@ user2910809对其评论所述,UNIQUE
列上有一个username
键,这就是抛出错误的原因。
因此解决方案是修改表的索引以允许多行具有相同的username
。
修改:根据评论中的建议,解决此问题的SQL是:ALTER TABLE votes DROP INDEX username;