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月7日和安培;名称=丹%20Volunteer&安培;所属=企业&安培; checkno = 1701&安培;量= 20025.00000000&安培;总= 20250.00000000&安培;笔记=安培; date_deposit =安培; rowno = 2.00000000&安培; date_line = 2014年10月7日和安培;名称=哈珀%20Lee&安培;所属=企业%20B&安培; checkno = 1702&安培;量= 225
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循环。任何想法
答案 0 :(得分:0)
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.
}}