为什么这个PHP脚本会两次插入数据?

时间:2012-03-04 16:53:55

标签: php mysql

问题解决了,造成了一个愚蠢的错误,包括打印和屏幕版本......


这个脚本插入数据两次,我尝试了一些东西,但我没有让它工作。有什么建议吗?

<?php
    include("config.inc.php");
$continue = '';
$sessiontekst = md5(date("d-m-Y"));
$sessionname = "Secure";
if(!isset($_SESSION[$sessionname]) or $_SESSION[$sessionname] != $sessiontekst)
{
$continue = 'Je bent niet ingelogd';
}
if($continue == '') {

以上是检查登录的部分。这里是处理表单

中的数据的脚本
    // startup
    $result_final = "";
    $counter = 0;

    // Arrays
    $opmerking = $_POST['opmerkingen'];
    $datum = $_POST['datum'];
    $docent = $_POST['docent'];
    $vak = $_POST['vak'];
    $stroming = $_POST['stroming'];
    $lokaal = $_POST['lokaal'];
    $tijd = $_POST['tijd'];


    while ($counter < count($datum))
    {
    if ($datum[$counter] == '')
    { echo 'Geen datum ingevoerd<br>';}
    elseif ($tijd[$counter] == '')
    { echo 'Geen tijd ingevoerd<br>';}
    else {
mysql_query( "INSERT INTO toetsrooster (`docent`, `vak`, `lokaal`, `datum`, `tijd`,  `richting`, `opmerkingen`) VALUES ('".addslashes($docent[$counter])."', '".addslashes($vak[$counter])."', '".addslashes($lokaal[$counter])."', '".addslashes($datum[$counter])."', '".addslashes($tijd[$counter])."',  '".addslashes($stroming[$counter])."', '".addslashes($opmerking[$counter])."')" );
$result_final .= "Toets ".($counter+1)." toegevoegd<br />";
    }
    $counter++;
    mysql_error();
    }

显示脚本输出的部分,它表示它添加了一次数据,但它会将数据广告两次。

    // Print Result
echo <<<__HTML_END

<html>
<head>
    <title>Toetsen toegevoegd</title>
</head>
<body>
    $result_final
</body>
</html>

__HTML_END;
}

再次检查一下登录

else {
    echo $continue;
}
?>

表格的代码:

<?
$continue = '';
$sessiontekst = md5(date("d-m-Y"));
$sessionname = "Secure";
if(!isset($_SESSION[$sessionname]) or $_SESSION[$sessionname] != $sessiontekst)
{
$continue = 'Je bent niet ingelogd';
}
if($continue == '') {
include("config.inc.php");
include("lijstenbouwer.inc.php");
//Standaard aantal
$velden = 6;
$veldenoutput = "";
$counter = 1;
//Kijken of er een URL variabele is voor ?p=velden
if( $_GET['velden'] )
    $velden = (int)($_GET['velden']);
//Velden bouwen
while( $counter <= $velden )
    {
$veldenoutput .=
<<<__HTML_END
<tr>
<td>
<select name='stroming[]'>
            $stroming
        </select></td><td><select name='vak[]'>
            $vak
        </select></td><td>
        <select name='docent[]'>
            $docent
        </select></td><td><select name='lokaal[]'>
            $lokaal
        </select></td><td>

    <input type="text" class="datepicker" id="$counter" name="datum[]" placeholder="Klik op de kalender"></td><td><input type="text" name="tijd[]" placeholder="12:00 - 13:00"></td><td><input type="text" name="opmerkingen[]" placeholder="Opmerkingen"></td>
</tr>
        <script>
    $('.datepicker').each(function(){
    $(this).datepicker({
    showOn: "button",
            buttonImage: "images/calendar.png",
            buttonImageOnly: true,
            dateFormat: 'dd-mm-yy',
            dayNames : ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
            dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
            firstDay: 1,
            monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December']
            });
});
    </script>
__HTML_END;
$counter++;
    }
//Output
echo <<<__HTML_END

<table width="100%">
<form enctype='multipart/form-data' action='index.php?p=verwerking/toetoevoegen' method='post' name='toevoegen'>
<tr>
<td>Stroming</td><td>Vak</td><td>Docent</td><td>Lokaal</td><td>Datum</td><td>Tijd</td><td>Opmerkingen</td>
</tr>
$veldenoutput
</table><input class="verzenden" name="verzenden" type="submit" value="Verzend"></form>
__HTML_END;
}
else {
    echo $continue;
}
?>

输入此数据时:The data,var_dump为:

array(8) { ["stroming"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["vak"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["docent"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["lokaal"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["datum"]=> array(2) { [0]=> string(10) "01-03-2012" [1]=> string(10) "02-03-2012" } ["tijd"]=> array(2) { [0]=> string(6) "Time 1" [1]=> string(6) "Time 2" } ["opmerkingen"]=> array(2) { [0]=> string(9) "Comment 1" [1]=> string(9) "Comment 2" } ["verzenden"]=> string(7) "Verzend" }

1 个答案:

答案 0 :(得分:2)

您是否可以为查询执行两次的示例提供$_POST['datum']的值?显而易见的答案是,while ($counter < count($datum))的循环体被执行两次。如果是count($datum) > 1,那就是这种情况。

您还应该使用mysql_real_escape_string转义或使用prepared statements代替addslashes

我运行它来在本地测试你的代码。如果您只是执行此缩短版本,则可以尝试发生错误。

<?php

$result_final = "";
$counter = 0;

$opmerking = array('Comment 1', 'Comment 2');
$datum =  array('01-03-2012', '02-03-2012');
$docent =  array('1', '1');
$vak =  array('1', '1');
$stroming =  array('1', '1');
$lokaal =  array('1', '1');
$tijd =  array('Time 1', 'Time 2');

while ($counter < count($datum))
{
    if ($datum[$counter] == '')
    {
        echo 'Geen datum ingevoerd<br>';
    }
    elseif ($tijd[$counter] == '')
    {
        echo 'Geen tijd ingevoerd<br>';
    }
    else
    {
        var_dump( "INSERT INTO toetsrooster (`docent`, `vak`, `lokaal`, `datum`, `tijd`,  `richting`, `opmerkingen`) VALUES ('".addslashes($docent[$counter])."', '".addslashes($vak[$counter])."', '".addslashes($lokaal[$counter])."', '".addslashes($datum[$counter])."', '".addslashes($tijd[$counter])."',  '".addslashes($stroming[$counter])."', '".addslashes($opmerking[$counter])."')" );
        $result_final .= "Toets ".($counter+1)." toegevoegd<br />";
    }
    $counter++;
    mysql_error();
}

echo <<<__HTML_END

<html>
<head>
    <title>Toetsen toegevoegd</title>
</head>
<body>
    $result_final
</body>
</html>

__HTML_END;

?>