避免更新中的php mysql中的重复数据?

时间:2016-07-24 09:44:48

标签: php mysqli

我有这个数据库,其中包含一个用于问题的表和另一个用于答案的表,我需要使用php来更新问题和答案但是当我查询选择问题和答案时我得到四个问题和四个答案或一个问题和答案在这里是我的表结构

-------------------------------------------------------   ---------------------
|  question                                            |  |     answers       |
-------------------------------------------------------   ---------------------

PHP server scripts are surrounded by delimiters, which?|  |   <&>...</&>
                                                       |  |  <?php>...</?>
                                                       |  |  <script>..</script>
                                                       |  |  <?php...?>

这是我的完整代码

   <?php include ("connection.php"); ?>
<?php
$msg = "";
if (isset($_POST['update'])) {
    if (is_numeric($_POST['question_id'])) {

        $question_id = mysqli_real_escape_string($link, htmlspecialchars($_POST['question_id']));
        $question_text = mysqli_real_escape_string($link, htmlspecialchars($_POST['question_text']));
        $test_id = mysqli_real_escape_string($link, htmlspecialchars($_POST['test_id']));

        if ($question_text == "" || $test_id == "" ) {
            $msg = "يرجى تعبيئة كافة الحقول";
        } else {
            $sql = mysqli_query($link, " UPDATE `question` SET `question_text`='".$question_text."',`test_id`= '".$test_id."' 
            WHERE  `question_id` = '".$question_id."'  ")or die(mysqli_error($link));

          /*   UPDATE `question` SET `question_text`='".."',`test_id`= '".."' WHERE  `question_id` =          */ 
        }
    } else {
        echo "Error !";
    }
}else {
    if (isset($_GET['question_id']) && is_numeric($_GET['question_id']) && $_GET['question_id'] > 0) {

        $question_id = $_GET['question_id'];

        $query = mysqli_query($link, "SELECT * FROM `question` WHERE `question_id` = '".$question_id."' ")
                or die(mysqli_error($link));
        while ($row = mysqli_fetch_assoc($query)) {
            $question_id = $row ['question_id']; 
            $question_text = $row ['question_text'];
            $test_id = $row['test_id'];


            $query2 = mysqli_query($link,"SELECT * FROM `answers` WHERE answers.question_id = '".$question_id."' ")    or die(mysqli_error($link));
            while($row2 = mysqli_fetch_assoc($query2)){
                $answer_text = $row2 ['answer_text'];   
                $correct = $row2 ['correct'];

            }




/*
SELECT question.question_id,question.question_text,question.test_id,test.test_name
FROM question,test
WHERE question.test_id = test.test_id
AND question.question_id= '".$question_id."'

*/
/*  SELECT  `answer_text`, `correct` FROM `answers` WHERE answers.question_id = 6 AND answers.correct = 1 */
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="RTL" lang="Ar">

    <head>


        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />

        <meta name="viewport" content="width=device-width, initial-scale=1.0">


        <meta http-equiv="refresh" content="30">



</head>

<body>

    <form method= "POST" action = "" >



            <p>
                <label><p>الحقل المخصص للسؤال</p></label>
               <textarea name = "question_text"><?php echo $question_text ; ?> </textarea>

            </p>

            <p>
                <label>رقم الاختبار</label>
                <td><input type="text" name="test_id" value="<?php echo  $test_id ; ?>" /></td>
            </p>

            <p>
                <label>الخيار الاول</label>
          <input type="text" name="" value="<?php echo  $answer_text ; ?>" />
            </p>
            <p>
                <label>الخيار الثانى</label>
                <td><input type="text" name="" value="<?php echo  $answer_text ; ?>" /></td>
            </p>
              <p>
                <label>الخيار الثالث</label>
                <td><input type="text" name="" value="<?php echo  $answer_text ; ?>" /></td>
            </p>  
            <p>
                <label>الخيار الرابع</label>
                <td><input type="text" name="" value="<?php echo  $answer_text ; ?>" /></td>
            </p>


            <p>

                <input type="submit" name="update" value="تحديث البيانات"  class="button save" />
            </p>

            <p>

                <input type="hidden" name = "question_id" value="<?php echo $_GET['question_id']; ?>" />
            </p>


    </form>
</body>
</html>
<?php
        }
    }
}


?>

1 个答案:

答案 0 :(得分:0)

问题出在你的周期中。代码不是很干净,但你打算每个问题打印4次相同的答案(最后在你的mysql数据库中找到),而不是所有的答案。你能试试吗,我不确定是否有拼写错误,因为我无法运行代码。

{{1}}