我的要求是我想存储与其phone_no
对应的联系人姓名,但是在我使用foreach
循环后爆炸变量
foreach($phone_no11 as $phone_nos)
foreach($contact_name11 as $contact_names)
然后,对于每个姓名,所有电话号码都存储,所以如果我的phone_no
是999,888,777而contact_name
是Watch,Sun,Sky,那么它存储在数据库中
观看999观看888观看777太阳999太阳888太阳777天空999天空888 天空777
请帮帮我
<?php
if(isset($_GET['phone_no'])&& isset($_GET['contact_name'])) {
include("connection.php");
$phone_no1 = $_GET['phone_no'];
$phone_no11 = (explode(",",$phone_no1));
$contact_name1 = $_GET['contact_name'];
$contact_name11 = (explode(",",$contact_name1)); // Insert data that retrieves from "temp_members_db" into table "registered_members"
foreach($phone_no11 as $phone_nos)
foreach($contact_name11 as $contact_names)
$sql = mysql_query("INSERT INTO phone_directory (contact_name,contact_number) VALUES ('$contact_names','$phone_nos')");
}
?>
答案 0 :(得分:1)
像这样的东西。
if(isset($_GET['phone_no'])&& isset($_GET['contact_name'])) {
include("connection.php");
$phone_no1 = $_GET['phone_no'];
$phone_no11 = (explode(",",$phone_no1));
$contact_name1 = $_GET['contact_name'];
$contact_name11 = (explode(",",$contact_name1)); // Insert data that retrieves from "temp_members_db" into table "registered_members"
$total_records = count($phone_no11);
for($i=0;$i<$total_records;$i++)
{
$sql = mysql_query("INSERT INTO phone_directory (contact_name,contact_number) VALUES ('$contact_name11[$i]','$phone_no11[$i]')");
}
}
首先计算一下你有多少个电话号码或联系号码(total_records),然后通过循环插入它们。
如果你有很多记录,那么将mysql查询放入循环中并不是一个好习惯。批处理查询的工作原理很完美。
像
这样的东西 if(isset($_GET['phone_no'])&& isset($_GET['contact_name'])) {
include("connection.php");
$phone_no1 = $_GET['phone_no'];
$phone_no11 = (explode(",",$phone_no1));
$contact_name1 = $_GET['contact_name'];
$contact_name11 = (explode(",",$contact_name1)); // Insert data that retrieves from "temp_members_db" into table "registered_members"
$total_records = count($phone_no11);
$records = '';
for($i=0;$i<$total_records;$i++)
{
$records .= ",('$contact_name11[$i]','$phone_no11[$i]')";
}
$records = sunstr($records,1);
$sql = mysql_query("INSERT INTO phone_directory (contact_name,contact_number) VALUES $records;
}
这将首先进行批量查询,然后将其运行出循环。