如何将多个数组放入一个数据库表中

时间:2014-10-15 21:03:32

标签: 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
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

2 个答案:

答案 0 :(得分:4)

嗯,我会在黑暗中拍摄。

PDO与PreparedStatements,MultipleIteratorArrayIterator一起使用:

$dbh = new PDO("mysql:host=localhost;dbname=YOUR_DATABASE;", "root", "");
$sth = $dbh->prepare("INSERT INTO customerphones(phone, type, notes) VALUES(:phone, :type, :note)");
$m = new MultipleIterator();
$m->attachIterator(new ArrayIterator($phonenums), 'phones');
$m->attachIterator(new ArrayIterator($types), 'types');
$m->attachIterator(new ArrayIterator($notes), 'notes');
foreach($m as $row){
    $sth->bindParam(":phone", $row[0]);
    $sth->bindParam(":type", $row[1]);
    $sth->bindParam(":note", $row[2]);
    $sth->execute();
}

我假设你使用的是本地MySQL服务器,而你服务器的root帐户没有密码保护。

这样的工作原理如下:

  • 使用一些参数创建新的PDO连接;
  • 准备一份包含插入内容的占位符的声明;
  • 创建一个Iterator来统一数组;
  • 将所有数组附加到迭代器;
  • 完成迭代器的所有迭代:每次迭代都会返回一个包含电话号码,类型和注释的数组;
  • 将当前迭代的所有元素绑定到语句的占位符,然后执行它。

但请发布你用来连接数据库的内容,然后我会重构我的答案。

答案 1 :(得分:0)

使用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++;
}