gpt4 book ai didi

php - 如何插入多个具有自己类型的数据?

转载 作者:行者123 更新时间:2023-11-29 15:06:16 25 4
gpt4 key购买 nike

<input name="names[]" type="text"/><select><option value="male">male</option><option value="female">female</option></select>  
<input name="names[]" type="text"/><select><option value="male">male</option><option value="female">female</option></select>
<input name="names[]" type="text"/><select><option value="male">male</option><option value="female">female</option></select>





if (!empty($_POST['names'])) {
foreach ($_POST['names'] AS $names) {
// add to the database
if (!empty($names)){
$rs=mysql_query("SELECT names FROM table WHERE names = '$names' LIMIT 1");
if(mysql_num_rows($rs) == 0) {
mysql_query("INSERT INTO table (names,gender) VALUES ('$names','$gender')");
}
}
}
}

这确实插入了名字,但是我不知道如何使性别适用于每个名字。

最佳答案

您需要为 SELECT 元素命名:

<input name="names[]" type="text"/><select name="genders[]"><option value="male">male</option><option value="female">female</option></select>

现在可以使用$_POST['names']的key来获取对应的性别:

foreach ($_POST['names'] AS $key => $names) {
// add to the database
if (trim($names) != '' && !empty($_POST['genders'][$key]) && in_array($_POST['genders'][$key], array('male', 'female'))) {
$gender = $_POST['genders'][$key];
$rs = mysql_query("SELECT names FROM table WHERE names = '".mysql_real_escape_string($names).' LIMIT 1");
if (mysql_num_rows($rs) == 0) {
mysql_query("INSERT INTO table (names,gender) VALUES ('".mysql_real_escape_string($names)."','$gender')");
}
}
}

您还应该使用mysql_real_escape_string净化用户发送的数据。

关于php - 如何插入多个具有自己类型的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1966406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com