繁体   English   中英

PDO INSERT INTO准备好的阵列

[英]PDO INSERT INTO prepared array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [rowno] => 1.00000000
                    [date_line] => 2014-10-08
                    [name] => Dan Volunteer
                    [affiliation] => trying
                    [checkno] => 1
                    [amount] => 0.01000000
                    [total] => 1.01000000
                    [notes] => almost
                    [date_deposit] => 2014-10-10
                )

            [1] => Array
                (
                    [rowno] => 2.00000000
                    [date_line] => 2014-10-09
                    [name] => Forest G
                    [affiliation] => Shrimp
                    [checkno] => 101
                    [amount] => 1.00000000
                )

        )

)

我有来自HTTP POST的确切数据:

rowno = 1.00000000&date_line = 2014-10-07&name = Dan%20Volunteer&affiliation = Enterprise&checkno = 1701&amount = 20025.00000000&total = 20250.00000000&notes =&date_deposit =&rowno = 2.00000000&date_line = 2014-10-07&name = Harper%20Lee&affiliation = Enterprise%20B&mount = 1225

HTTP POST的格式为ProcessPost类

<?php

class ProcessPost
        {
            public  static  function Split($value = '')
                {
                    if(!empty($value)) {
                            // Explode by row values
                            $rows   =   explode("rowno=",$value);
                            $rows   =   array_filter($rows);

                            if(is_array($rows) && !empty($rows)) {
                                    foreach($rows as  $_row => $querystring) {
                                            parse_str("rowno=".$querystring,$_array[]);
                                        }

                                    foreach($_array as $row_key => $row_val) {
                                            if(empty($row_val))
                                                unset($_array[$row_key]);
                                        }

                                    return $_array;
                                }
                        }
                }
        }

    $test   =   file_get_contents("php://input");

    $insert =   ProcessPost::Split($test);


$db = null;
if (isset($_SERVER['SERVER_SOFTWARE']) &&
strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
  // Connect from App Engine.
  try{
     $db = new pdo('mysql:unix_socket=/cloudsql/cinnamon:toast;dbname=crunch', 'root', '');
  }catch(PDOException $ex){
      die(json_encode(
          array('outcome' => false, 'message' => 'Unable to connect.')
          )
      );
  }
};

$array = array($insert);
foreach( $array as $key=>$value ) {
try {
  if (array_key_exists('name', $_POST)) {
    $stmt = $db->prepare('INSERT INTO entries (rowno, date_line, name, affiliation, checkno, amount, total, notes) VALUES (:rowno, :date_line, :name, :affiliation, :checkno, :amount, :total, :notes)');
    $stmt->execute(array(':rowno' => . $value .), ':date_line' => htmlspecialchars($_POST['date_line']), ':name' => htmlspecialchars($_POST['name']), ':affiliation' => htmlspecialchars($_POST['affiliation']), ':checkno' => htmlspecialchars($_POST['checkno']), ':amount' => htmlspecialchars($_POST['amount']), ':total' => htmlspecialchars($_POST['total']), ':notes' => htmlspecialchars($_POST['notes'])));
    $affected_rows = $stmt->rowCount();
    // Log $affected_rows.
  }

} catch (PDOException $ex) {
  // Log error.
}}
$db = null;
?>
<?php

header("Content-type: application/vnd.fdf");
// read and store the data however you want
// reply with some FDF data
echo <<<RESPONSE
%FDF-1.2
1 0 obj
<< /FDF <<
/Status (Wham bam! File sent.)
>>
>>
endobj
trailer
<< /Root 1 0 R >>
%%EOF
RESPONSE;
?>

我迷失了如何格式化foreach以便sql循环。 有任何想法吗

您应该使用$ value而不是$ _POST:

foreach( $array as $key=>$value ) {
try {
  if (array_key_exists('name', $value)) {
    $stmt = $db->prepare('INSERT INTO entries (rowno, date_line, name, affiliation, checkno, amount, total, notes) VALUES (:rowno, :date_line, :name, :affiliation, :checkno, :amount, :total, :notes)');
    $stmt->execute(array(':rowno' => $value['rowno']), ':date_line' => htmlspecialchars($value['date_line']), ':name' => htmlspecialchars($value['name']), ':affiliation' => htmlspecialchars($value['affiliation']), ':checkno' => htmlspecialchars($value['checkno']), ':amount' => htmlspecialchars($value['amount']), ':total' => htmlspecialchars($value['total']), ':notes' => htmlspecialchars($value['notes'])));
    $affected_rows = $stmt->rowCount();
    // Log $affected_rows.
  }

} catch (PDOException $ex) {
  // Log error.
}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM