<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PDO</title>
</head>
<body>
<form method="post" action="server.php">
<input type="text" name="classId[]">
<textarea name="className[]"></textarea> <hr>
<input type="text" name="classId[]">
<textarea name="className[]"></textarea> <hr>
<input type="text" name="classId[]">
<textarea name="className[]"></textarea> <hr>
<input type="text" name="classId[]">
<textarea name="className[]"></textarea> <hr>
<input type="text" name="classId[]">
<textarea name="className[]"></textarea> <hr>
<button type="submit">提交</button>
</form>
</body>
</html>
<?php
$pdo = new PDO("mysql:host=localhost;dbname=db_test","root","123");
$stmt = $pdo->prepare("insert into Classes (classId,className) values
(:classId,:className)");
$classIds = $_POST['classId'];
$classNames = $_POST['className'];
foreach($classIds as $key => $classId){
if(trim($classId) || trim($classNames[$key])){
$stmt->execute(array(':classId'=>$classId,':className'=>$classNames[$key]));
}
}
?>
评论0
最新资源