如何在php和mysql中发布数据,使用for循环多次插入(行)

如何在php和mysql中发布数据,使用for循环多次插入(行),php,mysql,sql,if-statement,for-loop,Php,Mysql,Sql,If Statement,For Loop,如何创建一个脚本,根据用户需要的次数多次发布数据。我在想我可以使用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']); //

如何创建一个脚本,根据用户需要的次数多次发布数据。我在想我可以使用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>";
  }
//检查表单是否已提交
如果(isset($\u POST['save\u stuff'])){
//创建网站对象
$stuff=新内容($_POST['stuff']);
//准备SQL查询
$stmt=$dbh->prepare(“插入到“.”$example.”(`title`、`stuf`、`due`、`details`、`category`、`user`)值(:title、:stuff、:due、:details、:category、:user)”);
$id=mysql\u real\u escape\u字符串($\u GET['id']);
//运行SQL查询
如果($stmt->执行(数组(
'title'=>$stuff->title,
'stuff'=>$id,
“到期日”=>$stuff->到期日,
'details'=>$stuff->details,
“类别”=>$stuff->category,
'user'=>$stuff->user
))
) {
//如果成功,则返回主页
标题(“位置:../stuff/?id=“.$id.”);
}否则{
//如果失败,则显示错误
echo“添加内容失败”

; }


在将数字传递给
execute()
之前,不需要对其进行转义,只要PDO提供自己的一个转义函数,则mysql\u real\u escape\u string也没有意义,但问题是什么?
execute()
多次?是的,如何在一个实例中执行数组以提交多次?是什么“执行数组”?您需要使用不同的argumentsIt函数多次调用
execute()
方法,比如假设多次插入,但出现错误“添加内容失败”,而不是重定向到标头。很抱歉,现在它改为if(min($insertworkedArr)==1)而不是0。告诉我它是否为您提供了解决方案。工作完美!谢谢!
<?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>";
    }
}
?>