I have 2 form with array
我有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">
那么它会是这样的
This is addschedule.php
这是addschedule.php
<?php
include_once("connect.php");
if(!empty($_POST['jam'])){
foreach($_POST['jam'] as $jmt){
echo $jmt."<br>";
echo $_POST['nampro'];
}
}
?>
i want to insert that 'jam' and 'nampro' into mysql
我想将'jam'和'nampro'插入mysql
$result=$mysqli->query('INSERT INTO SCHEDULE VALUES ('$jam','$nampro');
but how can i get 'nampro' Or anyone has a better way to do this ? Im new in PHP so can anyone help?
但我怎么能得到'nampro'或者任何人有更好的方法来做到这一点?我是PHP新手,所以任何人都可以帮忙吗?
2 个解决方案
#1
4
As i can see both array is of same length, so you can do following:
我可以看到两个数组的长度相同,所以你可以这样做:
$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));
Explanation:
说明:
$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));
will give you output:
会给你输出:
INSERT INTO SCHEDULE VALUES ("test", 123),("google", 112),("facebook", 110),("yahoo", 100)
插入日程安排价值(“测试”,123),(“谷歌”,112),(“facebook”,110),(“雅虎”,100)
#2
1
try this code
试试这段代码
<?php
include_once("connect.php");
if(!empty($_POST['jam'])){
$namproPost = $_POST['nampro'];
foreach($_POST['jam'] as $key=>$jmt){
// echo $jmt."<br>";
$nampro = $namproPost[$key];
$result=$mysqli->query('INSERT INTO SCHEDULE VALUES ($jmt,$nampro)');
}
}
?>
#1
4
As i can see both array is of same length, so you can do following:
我可以看到两个数组的长度相同,所以你可以这样做:
$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));
Explanation:
说明:
$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));
will give you output:
会给你输出:
INSERT INTO SCHEDULE VALUES ("test", 123),("google", 112),("facebook", 110),("yahoo", 100)
插入日程安排价值(“测试”,123),(“谷歌”,112),(“facebook”,110),(“雅虎”,100)
#2
1
try this code
试试这段代码
<?php
include_once("connect.php");
if(!empty($_POST['jam'])){
$namproPost = $_POST['nampro'];
foreach($_POST['jam'] as $key=>$jmt){
// echo $jmt."<br>";
$nampro = $namproPost[$key];
$result=$mysqli->query('INSERT INTO SCHEDULE VALUES ($jmt,$nampro)');
}
}
?>