【问题标题】:Comparing a value inside database with input value将数据库中的值与输入值进行比较
【发布时间】:2015-03-28 16:07:06
【问题描述】:

我的 if 语句 无法识别 文本框名称(“ans”)。我应该改变什么?我应该把它放在哪里?我也想知道我的 if 语句是否正确;我想将数据库中的值输入值(“ans”)进行比较。提前致谢。

<?php
    $con = mysql_connect("localhost", "root", "avtt123");
    mysql_select_db("arvintarrega",$con);

    $sql = "SELECT * from identification";
    $myData = mysql_query($sql,$con);   

    echo '<form method = "POST">';

while($iden = mysql_fetch_array($myData))
{   
    echo '<center>' . $iden['question_number'] . '. ' . $iden['statement'] . ' <input type = "text" name = "ans"></input></center></br>';

    if($iden['correct_answer'] == $_REQUEST['ans'])
       {
        echo "Correct";
       }
    else
       {
        echo "Wrong";
       } 
}   
    echo '</form>';
?>
<form method = "POST">
<input type = "submit" name = "submit" value = "Submit" class="btn btn-warning">
</form> 

【问题讨论】:

  • 我们可以看看你的表格吗?
  • 将错误报告添加到文件顶部,紧跟在您的打开 PHP 标记之后,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,以查看它是否产生任何结果。还将or die(mysql_error()) 添加到mysql_query()。还可以在内部使用isset() 并与if($iden['correct_answer'] == $_REQUEST['ans']) 结合使用。您也没有提交按钮。
  • 你也使用了错误的变量while($iden = mysql_fetch_array($myData))
  • @D4V1D 表单在 PHP 代码中
  • @Noobist 老实说,我以前从未编写过测验代码。但是,您可以查看 Stack stackoverflow.com/q/12136151 上的这个问答,它可以为您提供一些想法以及这个 stackoverflow.com/q/10356623 - 这些是在谷歌搜索“quiz mysql php”后找到的

标签: php sql forms if-statement


【解决方案1】:

尝试以下对我有用的方法,我正在使用mysqli_ 连接和查询,因此请使用您自己的凭据更改xxx

<?php
$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';

$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
  die('Connection failed [' . $Link->connect_error . ']');
}

$sql = "SELECT * from identification LIMIT 1";

$myData = mysqli_query($Link, $sql);   

    echo '<form method = "POST">';

while($iden = mysqli_fetch_array($myData))
{
    echo '<center>' . $iden['question_number'] . '. ' . $iden['statement'] . ' <input type = "text" name = "ans"></center><br/>';

 if(isset($_POST['submit']))

{

    if(isset($_POST['ans']) && $iden['correct_answer'] == $_POST['ans']){

      echo "Correct<br><br>";
       }
    else
       {
        echo "Wrong<br><br>";
       } 


    } // if(isset($_POST['submit'])) end brace

} // while loop end brace

echo '<input type = "submit" name = "submit" value = "Submit" class="btn btn-warning">';

     echo '</form>';
?>

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 2023-04-07
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多