我有2个数组形式
<form action="addschedule.PHP" method="post"> <?PHP $n = 0; $c = 0; echo "<Select name='jam[]'>"; do{ if($c>10){$n="";} echo "<option>".$n.$c.":00</option>"; echo "<option>".$n.$c.":30</option>"; $c++; }while($c<24); ?> <input type="text" name="nampro[]"></td> <input type="submit" id="submit" class="btn btn-success" value="submit"> </form> <input class="btn btn-primary" id="addrow" value="Add Row">
这是addschedule.PHP
<?PHP include_once("connect.PHP"); if(!empty($_POST['jam'])){ foreach($_POST['jam'] as $jmt){ echo $jmt."<br>"; echo $_POST['nampro']; } } ?>
我想将’jam’和’nampro’插入MysqL
$result=$MysqLi->query('INSERT INTO SCHEDULE VALUES ('$jam','$nampro');
我可以看到两个数组的长度相同,所以你可以这样做:
原文链接:https://www.f2er.com/php/134459.html$jam = $_POST['jam']; $namePro = $_POST['nampro']; $sql = array(); foreach( $jam as $key=>$val ) { $sql[] = '("'.$val.'",'.$namePro[$key].')'; } $result = $MysqLi->query('INSERT INTO SCHEDULE VALUES '.implode(',',$sql));
说明:
$jam = ['test','google','facebook','yahoo']; $namePro = [123,112,110,100]; $sql = array(); foreach( $jam as $key=>$val ) { $sql[] = '("'.$val.'",'.$namePro[$key].')'; } echo('INSERT INTO SCHEDULE VALUES '.implode(',$sql));
会给你输出:
INSERT INTO SCHEDULE VALUES (“test”,123),(“google”,112),(“facebook”,110),(“yahoo”,100)