一次将多行插入MySQL

时间:2013-04-14 22:57:02

标签: php mysql row

我试图一次提交一个充满信息的表格,但找不到答案。向DB提交一组信息没有问题,但我需要同时侮辱1到50个相同的值。这是一个例子:

我们正在记录单圈时间,因此我们需要能够在Web应用程序上填写表格,其中包含大量名称和活动时间。因此,我们需要一次添加大量SQL条目。

我尝试过只复制并粘贴表单信息,但这会导致phpmyadmin中出现一组空白数据。

以下是代码:

<?php


$host = "localhost";
$databasename = "pe_results";
$databaseusername = "root";
$databasepassword = "";

$conn = mysql_connect("$host", "$databaseusername", "$databasepassword"); 
mysql_select_db("$databasename", $conn); 

        if (isset($_POST['Name'])) { 
        $Name = $_POST['Name'];
        }
        if (isset($_POST['Short'])) { 
        $Short = $_POST['Short'];
        }
        if (isset($_POST['Med'])) { 
        $Med = $_POST['Med'];
        }
        if (isset($_POST['Long'])) { 
        $Long = $_POST['Long'];
        }
        if (isset($_POST['VLong'])) { 
        $VLong = $_POST['VLong'];
        }
        if (isset($_POST['Extreme'])) { 
        $Extreme = $_POST['Extreme'];
        }
        if (isset($_POST['LJump'])) { 
        $LJump = $_POST['LJump'];
        }
        if (isset($_POST['HJump'])) { 
        $HJump = $_POST['HJump'];
        }
        if (isset($_POST['Shotputt'])) { 
        $Shotputt = $_POST['Shotputt'];
        }
        if (isset($_POST['Discuss'])) { 
        $Discuss = $_POST['Discuss'];
        }
        if (isset($_POST['Javelin'])) { 
        $Javelin = $_POST['Javelin'];
        }
        if (isset($_POST['Date'])) { 
        $Date = $_POST['Date'];
        }
        if (isset($_POST['Year'])) { 
        $Year = $_POST['Year'];
        }



           $i = count($Name);

for ($i=0;$i<10;$i++) {
$n = $Name[$i];
$s = $Short[$i];
$me = $Med[$i];
$lng = $Long[$i];
$slng = $VLong[$i];
$ext = $Extreme[$i];
$ljump = $LJump[$i];
$hjump = $HJump[$i];
$shot = $Shotputt[$i];
$disc = $Discuss[$i];
$jav = $Javelin[$i];
$date = $Date[$i];
$year = $Year[$i];

//and so on with more variable...

$sql="INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `Vlong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
            VALUES ('$n', '$s', '$me', '$lng', '$slng', '$ext', '$ljump', '$hjump', '$shot', '$disc', '$jav', '$date', '$year')";

}


    $result = mysql_query($sql) or die(mysql_error ());;
        if($result){
echo"<br/>Everythings been saved";
echo "<BR>";
echo "<a href='index.php'>Back to the main page</a>";
}

else {

echo $result = mysql_query($sql,$conn) or die (mysql_error ());
}

// close connection 
mysql_close($conn);
?>

同时在

下面找到HTML
<?php
// Create connection
$con=mysqli_connect("127.0.0.1","root","","pe_results");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Uh oh, tell chris that " . mysqli_connect_error() . "NO DATA WILL BE SAVED";
  }
?>
    <a href="results.php"><div id="1" >Results</div></a>
    <a href="record.php"><div id="2">Record</div></a>
    <a href="overview.php"><div id="3">Overview</div></a>
    <div id="4">Blank</div>
   <form name="input_primary" action="process.php" method="post">

    <font color="#FFFFFF"><strong>Date:</strong></font><input name="Date" type="date" />

   <font color="#FFFFFF"><strong>Year:</strong></font><select name="Year">
        <option value="7">Year 7</option>
        <option value="8">Year 8</option>
        <option value="9">Year 9</option>
        <option value="10">Year 10</option>
        <option value="11">Year 11</option>
        <option value="12">Year 12</option>
        <option value="13">Year 13</option>
       </select>
       <input type="submit" value="Save results!" name="submit" />


<table width="200" border="1px solid black" id="maintab">
  <tr>
    <th scope="col">Name</th>
    <th scope="col">100m</th>
    <th scope="col">200m</th>
    <th scope="col">400m</th>
    <th scope="col">800m</th>
    <th scope="col">1500m</th>
    <th scope="col">Long Jump</th>
    <th scope="col">High Jump</th>
    <th scope="col">Shotputt</th>
    <th scope="col">Discus</th>
    <th scope="col">Javelin</th>
  </tr>
  <tr>
    <td>
        <input name="Name" type="text" />
    </td>
    <td> 
        <input name="Short" type="text" size="10px" />
    </td>
    <td>
         <input name="Med" type="text" size="10px" />
    </td>
    <td> 
        <input name="Long" type="text" size="10px" />
    </td>
    <td>
         <input name="VLong" type="text" size="10px" />
    </td>
    <td>
         <input name="Extreme" type="text" size="10px" />
    </td>
    <td>
         <input name="LJump" type="text" size="10px" />
    </td>
    <td>
         <input name="HJump" type="text" size="10px" />
    </td>
    <td>
         <input name="Shotputt" type="text" size="10px" />
    </td>
    <td>
         <input name="Discuss" type="text" size="10px" />
    </td>
    <td>
         <input name="Javelin" type="text" size="10px" />
    </td>
  </tr>

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你要做的是用[]命名字段 所以

<select name="Year">

变为

<select name="Year[]">

然后,您将能够遍历提交的值,例如:

foreach($_POST['Year'] as $k=>$v){
 echo $v." - This is the value";
 echo $k." - This is the key";
}

修改

(string)$insert;
if(is_array($_POST['Year'])){
    foreach($_POST['Year'] as $k=>$v){  
         $insert .= "(".$_POST['Name'][$k].", ".$_POST['Short'][$k].", ".$_POST['Med'][$k].", ".$_POST['Long'][$k].", ".$_POST['VLong'][$k].", ".$_POST['Extreme'][$k].", ".$_POST['LJump'][$k].", ".$_POST['HJump'][$k].", ".$_POST['Shotputt'][$k].", ".$_POST['Discuss'][$k].", ".$_POST['Javelin'][$k].", ".$_POST['Date'][$k].", ".$_POST['Year'][$k]."),"; 
    }
     $insert = substr_replace($insert ,0,-1);
}else{
    $insert .= "($_POST['Name'], $_POST['Short'], $_POST['Med'], $_POST['Long'], $_POST['VLong'], $_POST['Extreme'], $_POST['LJump'], $_POST['HJump'], $_POST['Shotputt'], $_POST['Discuss'], $_POST['Javelin'], $_POST['Date'], $_POST['Year'])"; 
}
$sql="INSERT INTO results_main 
(`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
VALUES 
".$insert;

这应该可以将所有内容都放入一个查询中,而不是为每一行执行多个查询

答案 1 :(得分:0)

在html表单中为所有名称添加方括号[](有意义的地方)。括号[]表示元素被视为动态数组。

html-form 中,执行以下操作:

<form name="input_primary" action="process.php" method="post">

    <font color="#FFFFFF"><strong>Date:</strong></font><input name="Date" type="date" />
   <font color="#FFFFFF"><strong>Year:</strong></font><select name="Year">
        <option value="7">Year 7</option>
        <option value="8">Year 8</option>
        <option value="9">Year 9</option>
        <option value="10">Year 10</option>
        <option value="11">Year 11</option>
        <option value="12">Year 12</option>
        <option value="13">Year 13</option>
       </select>
       <input type="submit" value="Save results!" name="submit" />


<table width="200" border="1px solid black" id="maintab">
  <tr>
    <th scope="col">Name</th>
    <th scope="col">100m</th>
    <th scope="col">200m</th>
    <th scope="col">400m</th>
    <th scope="col">800m</th>
    <th scope="col">1500m</th>
    <th scope="col">Long Jump</th>
    <th scope="col">High Jump</th>
    <th scope="col">Shotputt</th>
    <th scope="col">Discus</th>
    <th scope="col">Javelin</th>
  </tr>
<?php
//Repeat element inside loop 10 times
for($i=0;$i<10;$i++) {
?>
  <tr>
    <td>
        <input name="Name[]" type="text" />
    </td>
    <td> 
        <input name="Short[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Med[]" type="text" size="10px" />
    </td>
    <td> 
        <input name="Long[]" type="text" size="10px" />
    </td>
    <td>
         <input name="VLong[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Extreme[]" type="text" size="10px" />
    </td>
    <td>
         <input name="LJump[]" type="text" size="10px" />
    </td>
    <td>
         <input name="HJump[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Shotputt[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Discuss[]" type="text" size="10px" />
    </td>
    <td>
         <input name="Javelin[]" type="text" size="10px" />
    </td>
  </tr>
<?php
}
?>
</table>
</form>

在您的 PHP 代码中执行以下操作:

//Create initial query for sql-insert.
$sql="INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `Vlong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`) VALUES ";


$date = $Date; //Do like this if date-element is only occur once (at top) (then no need for brackets)
$year = $Year; //Do like this if year-element is only occur once (at top) (then no need for brackets)

$cnt = count($Name);    
for ($i=0;$i<$cnt;$i++) {
    //Use mysql_escape_string to escape strings (when needed) BEFORE adding to the $sql.
    $n = $Name[$i];
    $s = $Short[$i];
    $me = $Med[$i];
    $lng = $Long[$i];
    $slng = $VLong[$i];
    $ext = $Extreme[$i];
    $ljump = $LJump[$i];
    $hjump = $HJump[$i];
    $shot = $Shotputt[$i];
    $disc = $Discuss[$i];
    $jav = $Javelin[$i];

    //Insert each row separated with a comma
    $sql .= "('$n', '$s', '$me', '$lng', '$slng', '$ext', '$ljump', '$hjump', '$shot', '$disc', '$jav', '$date', '$year'),";
}

$useQuery = substr($sql, 0, -1); //Delete last comma
$result = mysql_query($useQuery); //Do the actual insert

当然,请阅读PDO而不是使用mysql_query等。请尽快执行此操作,因为这样的mysql_ *函数已弃用,将来会被删除。 PDO是一个更好的选择,因为它是oop,它更安全,更灵活。从这里开始...... http://www.php.net/manual/en/intro.pdo.php

答案 2 :(得分:0)

将数据存储在多个阵列中是不安全的,因为它很容易出错并导致错误。而是将数据存储到关联数组中,如下所示:

$people = array();
$people[] = array('Name' => $name, 'Short' => $short ....);

上面的代码创建了数组和数组,内部数组是关联数组,因此您可以使用以下语法来获取第一个人的名字:

$人[0] [ '名称'];

一切都在一起,如果没有为某些人设定某些价值,它就不会渗透到其他人的记录中。

我看到你正在使用POST变量。上面的关联数组可以通过使用json_encode和json_decode轻松传递ajax(如果这就是你的使用,只是一个选项)。当您通过多种语言传递数据结构时,这有助于保留数据结构。

最后,为了插入大量记录,我建议将PDO语句作为最简单,最干净的方法之一。在这里查看一个很棒的教程http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

希望这有帮助,祝你好运!