【发布时间】:2014-03-27 18:51:39
【问题描述】:
我在 PDO 语句中有以下代码块:
$stmt = $db->prepare("INSERT INTO first_page_data (title, first_name, surname, phone, email, add1, add2, add3, add4, add5) VALUES(?,?,?,?,?,?,?,?,?,?)");
$stmt->bindValue(1, $_POST['title'], PDO::PARAM_STR);
$stmt->bindValue(2, $_POST['first_name'], PDO::PARAM_STR);
$stmt->bindValue(3, $_POST['surname'], PDO::PARAM_STR);
$stmt->bindValue(4, $_POST['phone'], PDO::PARAM_INT);
$stmt->bindValue(5, $_POST['email'], PDO::PARAM_STR);
$stmt->bindValue(6, $_POST['add1'], PDO::PARAM_STR);
$stmt->bindValue(7, $_POST['add2'], PDO::PARAM_STR);
$stmt->bindValue(8, $_POST['add3'], PDO::PARAM_STR);
$stmt->bindValue(9, $_POST['add4'], PDO::PARAM_STR);
$stmt->bindValue(10, $_POST['add5'], PDO::PARAM_STR);
$stmt->execute();
$_SESSION['buyer_email'] = $_POST['email'];
可以使用数组和for each 循环将这些参数(title、first_name 等)放入bindValues 吗?我可以通过一个包含标题的数组来使准备语句工作,但似乎无法在 $_POST 值中获取变量名。它会节省不少代码,但我无法到达那里!
以下是我想在绑定值循环中使用的准备语句中使用的数组:
$first = array('title','first_name','surname','phone','email','add1','add2','add3','add4','add5');
【问题讨论】: