我已经提交了一个表单,一旦提交就应该将数据插入到数据库中。如果我在查询字符串中使用变量名,如INSERT INTO表(名称,注释)VALUES($ name,$ comment),它可以工作,但如果我使用pdo预处理语句它不起作用,那就是我目前的代码: / p>
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();
文件get已上传并移至我所述的目录,但没有数据插入数据库。有什么想法吗?
答案 0 :(得分: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();
答案 1 :(得分:-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']
));