-1

网站。

这感觉像是一个常见问题,但对 Google 来说却是一个难题!

正如代码中的注释中所解释的,插入该值以前不是问题,但是当我在INSERT其下方添加其他查询时停止工作。

$user = $_POST['name'];
$night = $_POST['club'];
$query = mysql_query("SELECT day FROM nights WHERE name = '$night'");
$email = $_POST['email'];

while ($row = mysql_fetch_assoc($query)) {

    $date = getFullDateString($row['day']);
    $date2 = getDateString($row['day']);

}

// this one previously worked but now enters into the database with $user as "Array"
mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$user', '$night', '$date') ") or die(mysql_error());

$guest1 = $_POST['name1'];
$guest2 = $_POST['name2'];

// these were added later and work fine but seem to have had an effect on the query above
mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$guest1', '$night', '$date') ") or die(mysql_error());
mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$guest2', '$night', '$date') ") or die(mysql_error());

foreach ($_POST as $key){
    if (is_array($key)){
        foreach ($key as $key2 => $value){
            mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$value', '$night', '$date') ") or die(mysql_error());

        }
    }
}
4

3 回答 3

1

在您的网站中,您有:

<input class='input' type='text' name='name["+currentArrayNum+"]' value='' />

生成表单。name[...]参数最终在 php 中,因为$_POST['name']它将是一个数组。如果您将其强制为字符串,则该字符串将为Array. 因此$user,使用 , 对它进行迭代并foreach处理每个名称。

另外,请阅读sql注入!

于 2012-06-13T09:58:05.293 回答
1

您应该将访客 HTML 输入重命名为,guests[]而不是name再次使用。您有命名冲突。

您需要在您的 javascript 代码中解决此问题:

var name = $("<p><input class='input' type='text' name='guests[]' value='' /></p>");

在您的 HTML 代码中:

<input class="input" type="text" name="guests[]" />

之后,您的 PHP 代码应将 guest 变量作为数组处理:

$guests = $_POST['guests'];

foreach ($guests as $guest)
{
    mysql_query("INSERT INTO guestlists (guest, night, date) VALUES('$guest', '$night', '$date') ") 
    or die(mysql_error());
}

请注意,您不需要 go guests[1],guests[2]guests[3]

于 2012-06-13T10:11:47.783 回答
0

您的页面上有一些 Javascript 正在插入如下所示的表单字段:

<input class="input" type="text" name="name[5]" value="">

name="name[5]"部分是导致您出现问题的原因 -$_POST['name']提交时它会变成一个数组。您需要重构该代码。

于 2012-06-13T09:58:07.273 回答