【问题标题】:How do I post data in php and mysql to insert multiple times (rows) using a for loop如何在 php 和 mysql 中发布数据以使用 for 循环插入多次(行)
【发布时间】:2013-05-26 19:32:15
【问题描述】:

我将如何创建一个脚本,根据用户想要的次数多次发布数据。我在想我可以使用 for 循环来做到这一点,但不知道从哪里开始。目标是将此数据行 1、3 或 5 次发布到 mySQL 表中。有人知道从哪里开始吗?

以下是我正在使用的...

   // check if the form was submitted
if( isset($_POST['save_stuff']) ) {

  // create the website object
  $stuff = new stuff($_POST['stuff']);

  // prepare an SQL query
  $stmt = $dbh->prepare("INSERT INTO `".$example."` (`title`, `stuf`, `due`, `details`, `category`, `user`) VALUES (:title, :stuff, :due, :details, :category, :user)");

$id = mysql_real_escape_string($_GET['id']);


  // run the SQL query
  if(  $stmt->execute(array(
        'title' => $stuff->title,
        'stuff' => $id,
        'due' => $stuff->due,
        'details' => $stuff->details,
        'category' => $stuff->category,
        'user' => $stuff->user
      ))
  ) {

    // if successful then go back to home page
    header("Location: ../stuff/?id=".$id."");
  } else {

    // display an error if it failed
    echo "<p>failed to add stuff</p>";
  }

【问题讨论】:

  • 在将数字传递给 execute() 之前,您无需转义数字。 mysql_real_escape_string 也没有任何意义,只要 PDO 提供了自己的一个转义函数
  • 但是这个问题呢?
  • execute() 多次?
  • 是的,如何执行数组在一个实例中多次提交?
  • 什么是“执行数组”?您需要使用不同的参数多次调用execute() 方法

标签: php mysql sql if-statement for-loop


【解决方案1】:
<?php 
// check if the form was submitted
if( isset($_POST['save_stuff']) ) {

    // create the website object
    $stuff = new stuff($_POST['stuff']);

    // prepare an SQL query
    $stmt = $dbh->prepare("INSERT INTO `".$example."` (`title`, `stuf`, `due`, `details`, `category`, `user`) VALUES (:title, :stuff, :due, :details, :category, :user)");

    // Unnecesary since PDO takes care of it anyway trough execute
    //$id = mysql_real_escape_string($_GET['id']);

    // number of times to run
    $timestorun = 10;
    for($i = 1; $i <= $timestorun; $i++){
        // Notice it will insert the same stuff multiple times! 
        // run the SQL query
        if(  $stmt->execute(array(
        'title' => $stuff->title,
        'stuff' => $id,
        'due' => $stuff->due,
        'details' => $stuff->details,
        'category' => $stuff->category,
        'user' => $stuff->user
        ))
        ) {
            $insertworkedArr[] = 1;
            $allIds .= $id . ",";
        } else {
            $insertworkedArr[] = 0;
        }
    }
    if(min($insertworkedArr) == 1){
        // if successful then go back to home page
        // $allIds contains all IDs that are inserted, accessible 
        // as an array trough an explode($allIds,",") at the next page
        header("Location: ../stuff/?id=".$id."&wasTheLastOneButAlso".$allIDs."");
    } else {
        // display an error if it failed
        echo "<p>failed to add stuff</p>";
    }
}
?>

【讨论】:

  • 它的功能就像假设多次插入但它出现错误“无法添加内容”而不是重定向到标题。
  • 抱歉,现在它已更改为 if(min($insertworkedArr) == 1) 而不是 0。告诉我它是否适合您。
  • 工作完美!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多