我的整个代码如下。$members
我有一个存储一些 ID的数组。我运行一个循环来搜索表中的这些 ID,检索它们各自的名称并将它们存储在另一个数组$members_name
中。然后我将两个数组都插入我的表中。数组的大小$members
在 1-6 之间。
<?php
session_start();
$members = $_POST['mem'];
$n=count($members);
foreach($members as $key=>$value){
$res = mysql_query("SELECT id,name,email FROM users WHERE id='".$value."'");
if ($res === false) {
echo mysql_error();
die;
}
$row = mysql_fetch_assoc($res);
if($row['id'])
{
$members_name[]=$row['name'];
$members_email[]=$row['email'];
}
}
$sess_uid = $_SESSION['id'];
$sess_email = $_SESSION['email'];
$sess_name = $_SESSION['name'];
$id=$_SESSION['eid'];
$name=$_SESSION['ename'];
$members=$_SESSION['emembers'];
if ($n==5){
$res=mysql_query("INSERT INTO eventregistration(event_id,event_name,event_members,mem_1_id,mem_1_name,mem_2_id,mem_2_name,mem_3_id,mem_3_name,mem_4_id,mem_4_name,mem_5_id,mem_5_name,mem_6_id,mem_6_name)
VALUES('".$id."','".$name."','".$members."','".$sess_uid."','".$sess_name."','".$members[0].",'".$members_name[0]."','".$members[1].",'".$members_name[1]."','".$members[2].",'".$members_name[2]."','".$members[3].",'".$members_name[3]."','".$members[4].",'".$members_name[4]."')");
}
else if ($n==4){
$res=mysql_query("INSERT INTO eventregistration(event_id,event_name,event_members,mem_1_id,mem_1_name,mem_2_id,mem_2_name,mem_3_id,mem_3_name,mem_4_id,mem_4_name,mem_5_id,mem_5_name)
VALUES('".$id."','".$name."','".$members."','".$sess_uid."','".$sess_name."','".$members[0].",'".$members_name[0]."','".$members[1].",'".$members_name[1]."','".$members[2].",'".$members_name[2]."','".$members[3].",'".$members_name[3]."')");
}
else if ($n==3){
$res=mysql_query("INSERT INTO eventregistration(event_id,event_name,event_members,mem_1_id,mem_1_name,mem_2_id,mem_2_name,mem_3_id,mem_3_name,mem_4_id,mem_4_name)
VALUES('".$id."','".$name."','".$members."','".$sess_uid."','".$sess_name."','".$members[0].",'".$members_name[0]."','".$members[1].",'".$members_name[1]."','".$members[2].",'".$members_name[2]."')");
}
else if ($n==2){
$res=mysql_query("INSERT INTO eventregistration(event_id,event_name,event_members,mem_1_id,mem_1_name,mem_2_id,mem_2_name,mem_3_id,mem_3_name)
VALUES('".$id."','".$name."','".$members."','".$sess_uid."','".$sess_name."','".$members[0].",'".$members_name[0]."','".$members[1].",'".$members_name[1]."')");
}
else if ($n==1){
$res=mysql_query("INSERT INTO eventregistration(event_id,event_name,event_members,mem_1_id,mem_1_name,mem_2_id,mem_2_name)
VALUES('".$id."','".$name."','".$members."','".$sess_uid."','".$sess_name."','".$members[0].",'".$members_name[0]."')");
}
else if ($n==0){
$res=mysql_query("INSERT INTO eventregistration(event_id,event_name,event_members,mem_1_id,mem_1_name)
VALUES('".$id."','".$name."','".$members."','".$sess_uid."','".$sess_name."')");
}
$url="events.php?slug=".$slug;
header('Location: ' . $url);
exit;
?>
我仍然是 PHP 的学习者。我知道我使用的是旧格式的 PHP。我仍然需要帮助来运行这个页面,而不是关于 PDO。