双击需要插入数据库

时间:2014-01-08 12:52:56

标签: php html mysql mysqli

这可能是一个非常简单的解决方案,但我真的无法理解。如果我插入我的数据库,我必须按两次插入按钮才能工作..我的猜测是它与我在一个文件中使用2个表单或仅仅因为我在一个文件中完成所有操作。请帮帮我。

感谢

代码:

<?php
/*require "link.php";*/
?>
<html>
    <head>
        <!--<link rel="stylesheet" type="text/css" href="css.css">--> <!-- verwijzing naar je css -->
        <!--<script type="text/javascript" src="js.js"></script>-->
    </head>
    <header>
    </header>
    <article>
        <div id="cards">
            <?php 
            $host = "localhost";
            $user = "root";
            $pwd = "";
            $db_name = "flashcards";
            $link = mysqli_connect($host, $user, $pwd, $db_name)or die("cannot connect");
            $array = array();
            $IDarray = array();

            ini_set('display_errors', 1);
            error_reporting(E_ALL);

            $sql = mysqli_query($link, "SELECT * FROM Questions ORDER BY ID ASC ") or die(mysqli_error($link)); 
            echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><table border='1'>";
            while ($rows = mysqli_fetch_assoc($sql))
            {
                echo "<tr id='".$rows['ID']."'><td>".$rows['Question']."</td><td><input type='text' name='Answer[]' id='V".$rows['ID']."'></input></td></tr>";
                $array[] = $rows["Answer"];
                $IDarray[] = $rows["ID"];
            }  
            echo "</table><input type='submit' name='submit'></input></form>";

            $i = 0;
            $count = sizeof($IDarray);
            if(!empty($_POST['Answer']))
            { 
                foreach($_POST['Answer'] as $answer)
                {
                    if (isset($_POST['Answer'])) {
                        if ($answer == $array[$i])
                        {
                            echo "<script>document.getElementById('".$IDarray[$i]."').style.background='green'; document.getElementById('V".$IDarray[$i]."').value='".$array[$i]."'</script>";
                        }
                        elseif ($answer !== $array[$i])
                        {
                            echo "<script>document.getElementById('".$IDarray[$i]."').style.background='red'; document.getElementById('V".$IDarray[$i]."').value='".$answer."'</script>";
                            $count = $count-1;
                        }
                        $i ++;
                    }
                }echo $count." van de ".sizeof($IDarray)." goed";
                if ($count == sizeof($IDarray))
                {
                    header('Location: http://localhost:1336/php3/');
                }
            }

            echo "</br></br>insert";
            echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><table border='1'>";
            echo "<tr><td>vraag</td><td><input type='text' name='vraag'></input></td><td>antwoord</td><td><input type='text' name='antwoord'></input></td></tr>";
            echo "</table><input type='submit' name='submitinsert' value='insert'></input></form>";

            if ($_POST['vraag'] != "") {
                $vraag = $_POST['vraag'];
                $antwoord = $_POST['antwoord'];
                mysqli_query($link, "INSERT INTO questions (Question, Answer) VALUES (".$vraag.",".$antwoord.");") or die(mysqli_error($link)); 
            }
            ?> 
        </div>
    </article>
    <footer>
    </footer>
</html>

2 个答案:

答案 0 :(得分:3)

问题是您在与生成表单的脚本相同的脚本中处理表单提交。结合您第一次查询数据库的事实,生成一个包含您已经存储的内容的表单,然后添加用户可能发布的任何数据,你'永远不会看到您添加的数据在您提交表单的第一次出现时显示出来 将插入查询移到顶部(在生成表单之前),或者单独关注

让我告诉你我的意思:

//don't OR DIE
$sql = mysqli_query($link, "SELECT * FROM Questions ORDER BY ID ASC ") or die(mysqli_error($link));
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'><table border='1'>";
while ($rows = mysqli_fetch_assoc($sql))
{//build form here
}
/*
CODE HERE
*/
if ($_POST['vraag'] != "") {
    //insert here, after form is generated
}

因此,您查询的数据不能包含提交的表单数据 但是,代码还存在一些其他问题,例如or die:不要这样做。与您的编码风格保持一致(同一个脚本中的allman括号+ K&amp; R是混乱的)。正确地缩进你的代码并且这个:

if ($_POST['vraag'] != "") {
}

应该是:

if (isset($_POST['vraag'])) {
}

您正在将可能不存在的数组的键与空字符串进行比较,而检查该数组键是否存在。使用isset

我可以继续下去,但我现在就把它留在那里。还有一件事:再次 - &gt; separrate关注!表示层(输出:HTML等) 不应 包含数据库连接内容。这应该在其他地方完成 使用AJAX异步处理表单(因为提交的内容已添加到已存在的表中),或者至少使用单独的脚本。让一个脚本完成所有工作很快就会让你为一堆意大利面条代码哭泣

答案 1 :(得分:2)

它没有提交两次,实际上它在插入后没有加载数据,

尝试添加

        if ($_POST['vraag'] != "") {
            $vraag = $_POST['vraag'];
            $antwoord = $_POST['antwoord'];
            echo "are you sure?";
            mysqli_query($link, "INSERT INTO questions (Question, Answer) VALUES (".$vraag.",".$antwoord.");") or die(mysqli_error($link)); 
        }

之前

  $sql = mysqli_query($link, "SELECT * FROM Questions ORDER BY ID ASC ") or   
  die(mysqli_error($link)); 

这将在保存当前记录后选择您的记录。