我有一个上传图片的按钮,每次上传图片时,图片都没有保存在数据库中,并且没有给出错误,
这是我的代码:
<td>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" required>
<button type="submit" name="files" class="btn btn-primary btn-xs">
Submit
</button>
<?php
if(isset($_POST['files']))
{
$userid = $row['stall_id'];
$a = $_FILES['file']['name'];
$ab = $_FILES['file']['tmp_name'];
$location = "".$a;
move_uploaded_file($ab, "../pictures/".$location);
$sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
$sql2->execute(array($location,$userid));
if($sql){
echo '
<script>
window.location = "stalls.php"
</script>';
}
}
?>
</td>
答案 0 :(得分:1)
我想出了自己,
<td>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" required>
<input type="hidden" name="stall_id" value="<?php echo $value['stall_id']?>">
<button type="submit" name="files" class="btn btn-primary btn-xs">
Submit
</button>
<?php
if(isset($_POST['files']))
{
$userid = $_POST['stall_id'];
$a = $_FILES['file']['name'];
$ab = $_FILES['file']['tmp_name'];
$location = "".$a;
move_uploaded_file($ab, "../pictures/".$location);
$sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
$sql2->execute(array($location,$userid));
if($sql2){
echo '
<script>
window.location = "stalls.php"
</script>';
}
}
?>
</td>
答案 1 :(得分:0)
</form>
代码。检查所有包含数据的变量。使用var_dump()
,print_r()
或echo()
方法查看。
<td>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" required>
<button type="submit" name="files" class="btn btn-primary btn-xs">
Submit
</button>
</form>
</td>
<?php
if(isset($_POST['files']))
{
$userid = $row['stall_id'];
$a = $_FILES['file']['name'];
$ab = $_FILES['file']['tmp_name'];
$location = "".$a;
move_uploaded_file($ab, "../pictures/".$location);
if( $sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?"))
{
$sql2->bindParam(1, $location, PDO::PARAM_STR);
$sql2->bindParam(2, $userid, PDO::PARAM_INT);
$update=$sql2->execute();
if($update){
echo '
<script>
window.location = "stalls.php"
</script>';
}
}else{
echo 'sql prepare failed';
}
}
?>
答案 2 :(得分:-1)
从
改变$sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
$sql2->execute(array($location,$userid));
到
$sql2 = $conn->prepare("UPDATE stall SET file = ? WHERE stall_id = ?");
$sql2->bindParam(1, $location, PDO::PARAM_STR);
$sql2->bindParam(2, $userid, PDO::PARAM_INT);
$sql2->execute();