PHP的foreach array_combine

时间:2019-02-19 02:50:58

标签: php

我有以下代码:

// choices array
    $choices = [];
    $choices[1] = $_POST['choice1'];
    $choices[2] = $_POST['choice2'];
    $choices[3] = $_POST['choice3'];
	
	$titlu = [];
	$titlu[1] = $_POST['titlu1'];
    $titlu[2] = $_POST['titlu2'];
    $titlu[3] = $_POST['titlu3'];
    
    if($insert_row){

        foreach(array_combine($choices, $titlu) as $choice => $titlu)
		{
           if($choice != ''){
               if($correct_choice == $choice) {
                   $is_correct = 1;
               } else {
                   $is_correct = 0;
               }
               $query = "INSERT INTO `choices`(question_number, is_correct, choice, titlu)VALUES('$question_number', '$is_correct', '$choice', '$titlu')";

               $insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);

               if($insert_row) {
                   continue;
               } else {
                   die('Error : ('.$mysqli->errno.') ' . $mysqli->error);
               }
           } 
        }
        $msg = "Questions has been added";
    }
}

我的数据库表是:

"Choices" which contain:

---------------------------------------------------
id | question_number | is_correct | choice | titlu |
---------------------------------------------------

我的旧代码没有“ titlu”行,我添加了它,并为此使用了“ aray_combine”。 一切正常,行充满了信息,但是“ is_correct”行不再起作用。即使我选择显示“ 1”,它在所有行上也仅显示“ 0”。 希望你能理解我!我是初学者,但我已经准备好学习。 谢谢!

2 个答案:

答案 0 :(得分:1)

我不确定您从哪里得到{。{1}}值。.但是我进行了快速测试,它工作正常。

correct_choice

输出为

$choices = [];
$choices[1] = 'bar';
$choices[2] = 'foo';
$choices[3] = 'elo';

$correct_choice = 'foo'; //added


$titlu = [];
$titlu[1] =  'llo';
$titlu[2] ='plo';
$titlu[3] = 'clo';


    foreach(array_combine($choices, $titlu) as $choice => $titlu)
    {
       if($choice != ''){
           if($correct_choice == $choice) {
               $is_correct = 1;
           } else {
               $is_correct = 0;
           }
           $query = "INSERT INTO `choices`(question_number, is_correct, choice, titlu)VALUES('1', '$is_correct', '$choice', '$titlu')";

           echo $query . "<br/>";
       } 
    }
    $msg = "Questions has been added";

您的INSERT INTO `choices`(question_number, is_correct, choice, titlu)VALUES('1', '0', 'bar', 'llo') INSERT INTO `choices`(question_number, is_correct, choice, titlu)VALUES('1', '1', 'foo', 'plo') INSERT INTO `choices`(question_number, is_correct, choice, titlu)VALUES('1', '0', 'elo', 'clo')

的方式可能有问题

答案 1 :(得分:0)

这是我的HTML表单:

<form method="post" action="add.php">
	     	   <p>
			<label>Question Number</label>
			<input type="number" value="<?php echo $next; ?>" name="question_number" />
		   </p>
	     	   <p>
			<label>Question</label>
			<input type="text" name="question_text" />
		   </p>
	     	   <p>
			<label>Choice #1: </label>
			<input type="text" name="choice1" />
		   </p>
	     	   <p>
			<label>Choice #2: </label>
			<input type="text" name="choice2" />
		   </p>
	     	   <p>
			<label>Choice #3: </label>
			<input type="text" name="choice3" />
		   </p>
		   <p>
			<label>TITLU #1: </label>
			<input type="text" name="titlu1" />
		   </p>
		   <p>
			<label>TITLU #2: </label>
			<input type="text" name="titlu2" />
		   </p>
		   <p>
			<label>TITLU #3: </label>
			<input type="text" name="titlu3" />
		   </p>
	     	   <p>
			<label>Correct choice number </label>
			<input type="number" name="correct_choice" />
		   </p>
		   <p>
			<input type="submit" name="submit" value="Submit" />
		   </p>
	     </form>

我认为这是我的问题:

 if($choice != ''){
           if($correct_choice == $choice) {
               $is_correct = 1;
           } else {
               $is_correct = 0;
           }

在添加“ titlu”行及其值之前,先用“ $ value”填充,但是我用“ $ choice”更改了