我被一个问题困住了。我不知道如何修复我的代码,让它在mysql pdo中插入多行输入检查器功能。不幸的是,我尝试了很多代码,但都不起作用。
我的代码如下:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = inputchecker_($_POST["name"][$info]);
$number = input_checker($_POST["number"][$info]);
$gender = input_checker($_POST["gender"][$info]);
foreach ($info as $key=>$val) {
$sql = "INSERT INTO memo (name, number, gender)
VALUES ('$name', '$number', '$gender')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
function input_checker($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name 1: <input type="text" name="name[]"><br>
Name 2: <input type="text" name="name[]"><br>
Name 3: <input type="text" name="name[]"><br>
Number 1: <input type="text" name="number[]"><br>
Number 2: <input type="text" name="number[]"><br>
Gender 1: <input type="text" name="gender[]"><br>
Gender 2: <input type="text" name="gender[]"><br>
<input type="submit" name="submit" value="Submit">
</form>
1条答案
按热度按时间mzmfm0qo1#
注意:应该使用prepare()方法并传递参数以防止sql注入。
值(?,?),(?,?,?),(?,?,?)样式
或值(:param1,:param2,:param3),(:param4,:param5,:param6),(:param7,:param8,:param9)样式