php - 发布的结果不会保存到 mysql 数据库,即使不存在记录

标签 php mysql if-statement while-loop

我正在做一个测验,它会检查表格以查看用户是否已经发布了问题的答案并且没有敲击提交按钮,我遇到的问题是它没有发布由于某种奇怪的原因将信息存入数据库。如果我取出 if 语句和第一个数据库查找并仅发布数据,它工作正常,但是当我首先检查第一个查询的结果时,它似乎不起作用,即使用户没有提交还没回答。

 <?php
 $quizID = $_GET['quiz'];
$userID = $_GET['user'];
$quizselectanswer = $_POST['quizselectanswer'];
$cf_created = date("y/m/d");
$questionID = $_POST['questionID'];

// Check to see if user answered question already
 $result = mysql_query("SELECT questionID,userID FROM itsnb_chronoforms_data_answerquiz WHERE     questionID='$questionID' AND userID='$userID' LIMIT 1") or die(mysql_error());

while($row = mysql_fetch_array($result))
{ 
if (empty($row))
{
    mysql_query("INSERT INTO itsnb_chronoforms_data_answerquiz (cf_created,     questionID,quizselectanswer,quizID, userID)
    VALUES ('$cf_created', '$questionID', '$quizselectanswer', '$quizID','$userID')") 
    or     
    die(mysql_error());
}else{

}
}
?>

我的数据库如下所示 itsnb_chronoforms_data_answerquiz cf_idcf_uidcf_createdcf_modified ,cf_ipaddress ,cf_user_id ,questionID,quizselectanswer,quizID ,用户 ID.

最佳答案

尝试这样可能会对你有帮助

// Check to see if user answered question already
 $result = mysql_query("SELECT * FROM itsnb_chronoforms_data_answerquiz WHERE questionID='$questionID' AND userID='$userID' LIMIT 1") or die(mysql_error());

$row = mysql_fetch_array($result);
if(!empty($row)) 
{
    while($row){
        //some statement
    }
}else{
    mysql_query('INSERT INTO itsnb_chronoforms_data_answerquiz (cf_created, questionID,quizselectanswer, quizID, userID)
    VALUES ("'.$cf_created.'", "'.$questionID.'", "'.$quizselectanswer.'", "'.$quizID.'","'.$userID.'")') or die(mysql_error());
}
?>

关于php - 发布的结果不会保存到 mysql 数据库,即使不存在记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13627146/

相关文章:

MySQL括号性能影响?

c - if/else if 语句中的条件格式是否必须在 C 中匹配?

if-statement - NetLogo 中嵌套 ifelse 的问题

php - 如何允许人们将我的 Facebook 应用程序订阅到他们的页面?

php - preg_replace() 在 MySQL 表中查找匹配项

php - 如何使用 PHP 动态插入 CSS 类?

mysql - 安装组件时创建mysql函数

c# - WPF 复选框检查 IsChecked

php - 将 PHP 对象插入到 MONGODB 中

php - 对象通过引用传递。 call_user_func 的参数不是。是什么赋予了?