$query_num = 5 //插入数量
for($i=1$i<=$query_num$i++){
$value .= "('25','1')"
}
//mysql insert有插入多条语法,拼接sql语句,table_name表名
$sql = "insert into table_name (memid,online) values ".$value
//执行,插入$query_num条数据
mysql_query($sql)
<?phpif(isset($_POST)){
$sql = 'INSERT INTO uu (uu_name,uu_phone) VALUES '
foreach($_POST['uu_name'] as $k => $v){
$sql .= "('$v','".$_POST['uu_number'][$k]."'),"
}
$sql = substr($sql,0,-1)
//执行这条sql语句就可以了
echo $sql
}
?>
数组表单后面不需要加数字标签,会自动编码。
见以下代码:
<!DOCTYPE html><html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<form method="post">
<input type="text" name="age[]">
<textarea name="name[]"></textarea>
<hr>
<input type="text" name="age[]">
<textarea name="name[]"></textarea>
<hr>
<input type="text" name="age[]">
<textarea name="name[]"></textarea>
<hr>
<input type="text" name="age[]">
<textarea name="name[]"></textarea>
<hr>
<input type="text" name="age[]">
<textarea name="name[]"></textarea>
<hr>
<button type="submit">提交</button>
</form>
<?php
if(!empty($_POST)){
$pdo = new PDO("mysql:host=localhostdbname=t1","root","")
$stmt = $pdo->prepare("insert into t2(age,name)values(:age,:name)")
$ages = $_POST['age']
$names = $_POST['name']
foreach($ages as $key => $age){
if(trim($age) || trim($names[$key])){
$stmt->execute(array(':age'=>$age,':name'=>$names[$key]))
}
}
}
?>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)