0

保存它的 PHP 代码。

$cols = $_POST['col'];

$EvenementID = mysql_real_escape_string($_POST['evenementid']);

foreach($cols as $col) {

$Ticketnaam = mysql_real_escape_string($col['ticket']);
$Aantal = mysql_real_escape_string($col['aantal']);
$Prijs = mysql_real_escape_string($col['prijs']);

$sql = "INSERT INTO tbl_Tickets (EvenementID, Ticketnaam, Aantal, Prijs) VALUES('".$EvenementID ."', '{$Ticketnaam}', '{$Aantal}', '{$Prijs}')";


}

代码

<input type="hidden" name="evenementid" value="<?php echo $evenementid; ?>" />

区块 1

<input type="text" name="col[0][ticket]" id="ticket" class="tekst-lang"/>
<input type="text" class="tekst-lang" name="col[0][aantal]" id="aantal"/>
<input type="text" class="tekst-lang" name="col[0][prijs]" id="prijs"/>

区块 2

<input type="text" name="col[1][ticket]" id="ticket" class="tekst-lang"/>
<input type="text" class="tekst-lang" name="col[1][aantal]" id="aantal"/>
<input type="text" class="tekst-lang" name="col[1][prijs]" id="prijs"/>

AJAX

var myData = $('#ticket-form').serialize();

                        $.ajax({
                            type: "POST",

                            //URL of the php file that will process the login
                            url: "includes/ticket.php",

                            dataType: 'json',

                            //Pass the data through
                            data: myData,

                            //Handle the response
                            success: function (data) {

                            switch(data.case){

                            case 1: 
                            $(".inlog-feedback").html(data.message).fadeIn('slow');
                            break;

                            case 2:      
                            $(".inlog-feedback").html(data.message).fadeIn('slow');
                            window.location = "index.php"; 
                            break;

                            case 3: 
                            $(".inlog-feedback").html(data.message).fadeIn('slow');
                            break;

                            default:
                            /* If none of the above */

                            }
                    }            
                        })  

                        //Stop the submit button from submitting the form 
                        return false;   
                        }  

我正在尝试将 2 个块保存到我的数据库中(所以我需要 2 条记录)。两个块具有相同的evenementID。问题是数据库中只有一条记录。

4

2 回答 2

0
$cols = $_POST['col'];

$EvenementID = mysql_real_escape_string($_POST['evenementid']);

foreach($cols as $col) {

$Ticketnaam = mysql_real_escape_string($col['ticket']);
$Aantal = mysql_real_escape_string($col['aantal']);
$Prijs = mysql_real_escape_string($col['prijs']);

$sql = "INSERT INTO tbl_Tickets (EvenementID, Ticketnaam, Aantal, Prijs) VALUES('".$EvenementID ."', '{$Ticketnaam}', '{$Aantal}', '{$Prijs}')";

$res = mysql_query($sql);//This will execute count($cols) number of times

}

您需要将 mysql_query($sql) 移动到循环内,否则它将仅执行 foreach 循环的最后一次迭代。

于 2013-08-26T17:05:43.587 回答
0
    if (isset($_POST)){    
$EvenementID = mysql_real_escape_string($_POST['evenementid']);

foreach($_POST as $col) {

$Ticketnaam_1 = mysql_real_escape_string($col[0]['ticket']);
$Aantal_1 = mysql_real_escape_string($col[0]['aantal']);
$Prijs_1 = mysql_real_escape_string($col[0]['prijs']);


$Ticketnaam_2 = mysql_real_escape_string($col[1]['ticket']);
$Aantal_2 = mysql_real_escape_string($col[1]['aantal']);
$Prijs_2 = mysql_real_escape_string($col[1]['prijs']);





$sql = "INSERT INTO tbl_Tickets (EvenementID, Ticketnaam, Aantal, Prijs) VALUES('".$EvenementID ."', '{$Ticketnaam_1}', '{$Aantal_1}', '{$Prijs_1}')";
$sql = "INSERT INTO tbl_Tickets (EvenementID, Ticketnaam, Aantal, Prijs) VALUES('".$EvenementID ."', '{$Ticketnaam_2}', '{$Aantal_2}', '{$Prijs_2}')";


}


}
于 2013-08-26T17:13:49.253 回答