Php 如何将多个数组放入一个数据库表中
0我有三个阵列。。。例如:Php 如何将多个数组放入一个数据库表中,php,mysql,arrays,Php,Mysql,Arrays,0我有三个阵列。。。例如: phonenums [0] 555-5555 [1] 555-4444 [2] 555-3333 types [0] cell [1] home [2] work notes [0] a note [1] the babysitters name [2] call before 6pm 它们来自具有动态添加的输入的表单,因此行数是任意的 我想使用PHP将这些数组放入MySQL中的一个表中 Table name: customerphones id custome
phonenums
[0] 555-5555
[1] 555-4444
[2] 555-3333
types
[0] cell
[1] home
[2] work
notes
[0] a note
[1] the babysitters name
[2] call before 6pm
它们来自具有动态添加的输入的表单,因此行数是任意的
我想使用PHP将这些数组放入MySQL中的一个表中
Table name: customerphones
id
customer_id
phone
type
notes
我可以很好地将任何一个数组放入数据库,但是,当涉及到将所有三个数组放入数据库表以相互协调时,例如:每一行[0]都位于数据库表的一行中……我被卡住了!我一直在不同的循环中重写,每次都会出错
如果代码有助于进一步解释我的情况,我可以发布代码。我只是在这里寻找一个概念,为我指明正确的方向
我应该以某种方式组合这些数组,还是将它们放入一个循环中?我不知道
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这是我根据要求提出的解决方案。我敢肯定这根本不实际……而且可能有更好的方法。但它得到了我想要的结果
// ~~~ Get variables from the form submitted
$phonenums = $_POST["phone"];
$types = $_POST["type"];
$notes = $_POST["notes"];
// ~~~ Queries for Inserting Customer Phone Numbers
$querynum = "INSERT INTO customerphones";
$querynum .= "(customer_id, phone)";
$querynum .= "VALUES (?, ?)";
$stmtnum = mysqli_prepare($db, $querynum);
$queryty = "UPDATE customerphones SET ";
$queryty .= "type = ? ";
$queryty .= "WHERE customer_id = ? AND phone = ?";
$stmtty = mysqli_prepare($db, $queryty);
$queryno = "UPDATE customerphones SET ";
$queryno .= "notes = ? ";
$queryno .= "WHERE customer_id = ? AND phone = ?";
$stmtno = mysqli_prepare($db, $queryno);
// Loops for executing the queries to insert phone numbers
// (I scraped this together b/c I couldn't get other methods to work...Change this later)
$n = 0;
foreach($phonenums as $rowph) {
mysqli_stmt_bind_param($stmtnum, 'is', $custid, $rowph);
mysqli_execute($stmtnum);
$rct = 0;
foreach($types as $rowty) {
if($rct == 0) {
$x = $types[$n];
mysqli_stmt_bind_param($stmtty, 'sis', $x, $custid, $rowph);
mysqli_execute($stmtty);
$rct++;
}
} // End Update Phone Type
$rct = 0;
foreach($notes as $rowno) {
if($rct == 0) {
$x = $notes[$n];
mysqli_stmt_bind_param($stmtno, 'sis', $x, $custid, $rowph);
mysqli_execute($stmtno);
$rct++;
}
} // End Update Phone Notes
$n++;
} // End foreach loops
好吧,我要在黑暗中试一试 将PDO与PreparedStatements、MultipleIterator和ArrayIterator一起使用: 我假设您使用的是本地MySQL服务器,并且服务器的根帐户没有密码保护 其工作原理如下: 使用一些参数创建新的PDO连接; 为插入准备一个带有占位符的语句; 创建一个迭代器来合并数组; 将所有数组附加到迭代器; 遍历迭代器的所有迭代:每次迭代都返回一个带有电话号码、类型和注释的数组; 将当前迭代的所有元素绑定到语句的占位符,然后执行它。 但是请发布您用于连接数据库的内容,然后我将重构我的答案。使用mysqli:
$host = 'your_host';
$user = 'your_user';
$pass = 'your_pass';
$db = 'your_database';
$mysqli = new mysqli($host, $user, $pass, $db);
// Check connection mysql
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$sql = 'INSERT INTO customerphones(phone, type, notes) VALUES(?,?,?)';
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('sss', $phone, $type, $note);
$index = 0;
while(COUNT($phonenums) - 1 >= $index){
$phone = $phonenums[$index];
$type = $type[$index];
$note= $note[$index];
$stmt->execute();
$index++;
}
正确的方法适用于这里。为不同的数据位创建一个表(如果它们是不同的),或者为一个事实类型表(如果它们非常通用)。等等,等等,等等。。。我刚刚看到你用我列出的内容更新了问题。。。为什么不将每一行单独插入呢?再多填一点问题:您是如何连接到MySQL的?还有其他数据要插入吗?最重要的是,@tadman我的数据库已经正常化了。但是谢谢你提供的信息。@Shomz。。我从未更新/编辑过我的问题..?哦,好吧,我想这就是我需要的。所以我只是将它们合并到一个数组中,我已经考虑过了,但不知道如何真正使其工作。我要试试这个。谢谢,谢谢!我已经把这件事搞砸了一段时间,并且让它起作用了。你的答案有几个地方不对劲,但它帮助我找到了一些可行的方法。所以,谢谢你:没有使用bind_param,而只是用零转义插入任意数据是完全鲁莽的。最初的问题是使用参数化语句,因此这是一个大规模的回归。结果数组将有三个位置:一个是数字,一个是类型,一个是注释。因此,您的foreach循环是不正确的。它不会返回二维数组而不是从字符串返回字符,而且,即使会返回,也会得到一个包含所有电话号码、所有类型或所有注释的数组。总之,插入件不正确@Omgabee,既然你成功了,你能和我们分享代码吗?是的,我会分享的。对不起,我真的意识到了这一点,并记录了一些我勉强拼凑起来的东西。我将使用此代码进行编辑。谢谢,我想这也许也能回答我的问题,但我会先试试Pantamtuy的答案,因为我对PHP比较陌生,这对我来说更有意义。感谢you@omgabee这只是一种不同的方法,但如果你是新手,就放轻松,按照你所理解的去做。但我必须指出:不要将查询放在循环中,它会扰乱您的连接,还会将您的数据泄漏给黑客,所以不要在生产代码中这样做。但是MySQLi也有准备好的语句,所以你可以重构Pantamtuy的代码来使用它,让你更安全。是的,我实际上使用的是准备好的语句!事实上,我正在为它量身定做。谢谢谢谢你的帮助,如果我有代表,我会投赞成票:
$host = 'your_host';
$user = 'your_user';
$pass = 'your_pass';
$db = 'your_database';
$mysqli = new mysqli($host, $user, $pass, $db);
// Check connection mysql
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$sql = 'INSERT INTO customerphones(phone, type, notes) VALUES(?,?,?)';
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('sss', $phone, $type, $note);
$index = 0;
while(COUNT($phonenums) - 1 >= $index){
$phone = $phonenums[$index];
$type = $type[$index];
$note= $note[$index];
$stmt->execute();
$index++;
}