这感觉像是一个常见的问题,但对谷歌来说这很难!
正如代码中的注释所解释的那样,插入该值以前不是问题,但是当我在其下面添加了额外的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());
}
}
}
答案 0 :(得分:1)
在您的网站上:
<input class='input' type='text' name='name["+currentArrayNum+"]' value='' />
生成表单。 name[...]
参数在php中以$_POST['name']
结尾,这将是一个数组。如果强制将其作为字符串,则字符串将为Array
。因此,请使用$user
并使用foreach
对其进行迭代,以处理每个名称。
另外,请阅读sql注入!
答案 1 :(得分: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());
}
请注意,您无需转guests[1]
,guests[2]
,guests[3]
等。
答案 2 :(得分:0)
您的网页上有一些Javascript正在插入如下所示的表单字段:
<input class="input" type="text" name="name[5]" value="">
name="name[5]"
部分是什么导致您的问题 - 它在提交时将$_POST['name']
转换为数组。你需要重构那段代码。