1

我制作了一个表单,一旦提交就应该将数据插入数据库。如果我在查询字符串中使用变量名称,例如 INSERT INTO 表(名称,注释)VALUES($name,$comment),它可以工作,但是如果我使用 pdo 准备好的语句,它就不起作用,这是我目前拥有的代码:

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/" . $_FILES["file"]["name"]);
$stmt = $conn->prepare('INSERT INTO instructors (name, bio, picture) VALUES (:name, :bio, :picture)');
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':bio', $_POST['bio']);
$stmt->bindParam(':picture', $_POST['file']['name']);
$stmt->execute();

文件上传并移动到我所说的目录,但没有数据插入数据库。任何想法为什么?

4

2 回答 2

0

尝试

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/" . $_FILES["file"]["name"]);
$stmt = $conn->prepare('INSERT INTO instructors (name, bio, picture) VALUES (:name, :bio, :picture)');
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':bio', $_POST['bio']);
$stmt->bindParam(':picture', $_POST['filename']);// Only 1 set of []s in $_POST[]
$stmt->execute();
于 2012-11-23T17:44:50.407 回答
-1

我相信你应该这样做:

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/" . $_FILES["file"]["name"]);
$stmt = $conn->prepare('INSERT INTO instructors (name, bio, picture) VALUES (:name, :bio, :picture)');

$stmt->execute(array(
    ':name'=> $_POST['name'],
    ':bio'=> $_POST['bio'],
    ':picture'=> $_POST['file']['name']
));
于 2012-11-23T14:59:36.653 回答