MySQL从PHP插入INTO://输入多行


MySQL INSERT INTO from PHP://INPUT multiple rows

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

rowno=1.00000000&date_line=2014-10-07&name=Dan%20志愿者&附属关系=企业&checkno=1701&金额=20025.00000000&总计=20250.00000000&notes=&date_deposite=&rowno=200000000&date_line=2014-10-07&name=哈珀%20Lee&附属关系=企业%20B&checkno=1702&数量=225

然后这个代码来处理

<?php
file_get_contents("php://input");
$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/wonder:bread;dbname=loaf', 'root', '');
  }catch(PDOException $ex){
      die(json_encode(
          array('outcome' => false, 'message' => 'Unable to connect.')
          )
      );
  }
};
try {
  if (array_key_exists('name', $_POST)) {
    $stmt = $db->prepare('INSERT INTO entries (name, affiliation) VALUES (:name, :affiliation)');
    $stmt->execute(array(':name' => htmlspecialchars($_POST['name']), ':affiliation' => htmlspecialchars($_POST['affiliation'])));
    $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;
?>

这篇http文章有两条记录(行/计数总是不同),但只插入最后一行的数据。需要所有行。

我要试试这个。。。。我认为发生的情况是,您只是按原样处理返回邮件,所以第一个rowno被跳过(而第二个rowno覆盖了第一个)。如果您以字符串的形式收到该帖子,则需要将其按preg_match()explode()进行拆分,以便使用try对其进行循环。

在你的字符串上试试这个类。此类将根据行将字符串拆分为数组。然后,您需要获取生成的数组$insert,然后在sql循环中处理每个数组。。。这有道理吗?

class ProcessPost
        {
            public  static  function Split($value = '',$splitVal = 'rowno=')
                {
                    if(!empty($value)) {
                            // Explode by row values
                            $rows   =   explode($splitVal,$value);
                            $rows   =   array_filter($rows);
                            if(is_array($rows) && !empty($rows)) {
                                    foreach($rows as  $_row => $querystring) {
                                            parse_str($splitVal.$querystring,$_array[]);
                                        }
                                    foreach($_array as $row_key => $row_val) {
                                            if(empty($row_val))
                                                unset($_array[$row_key]);
                                        }
                                    return $_array;
                                }
                        }
                }
        }
    $test   =   '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&checkno=1702&amount=225';
    $insert =   ProcessPost::Split($test);