doulu4233 2014-10-07 01:13
浏览 69
已采纳

来自PHP的MySQL INSERT INTO:// INPUT多行

I have data (exact) from this 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&checkno=1702&amount=225

then this code to process

<?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;
?>

This http post has two records (row/recount count always varies), but only data from the last row is being inserted. Need all rows.

  • 写回答

1条回答 默认 最新

  • douzhe3516 2014-10-07 02:46
    关注

    I'm going to stab at this one....I think what is happening is that you are just processing the return post as is, so the first rowno is being skipped over (rather the second rowno is overwriting the first). If you receive that post back as a string, you need to split it by preg_match() or explode() so that you can loop over it with your try.

    Try this class on your string. This class will split the string into arrays based on rows. Then you need to take the resulting array $insert then process each array in your an sql loop...does that make sense?

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用