我用PHP制作代码用于上传图片,但我有一个问题,当我上传图片时,数据库中没有记录,但图片上传到我想要的路径。
这是我的代码:
<form action="upload.php" method="post" enctype="multipart/form-data" id="upload_form">
<input id="file" type="file" name="file[]" />
<input type="submit" id="upload" name="submit" value="Dodaj" />
</form>
include 'init.php';
$uploaded = [];
$allowed = ['jpg', 'png'];
$succeeded = [];
$failed = [];
if(!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
if($_FILES['file']['error'][$key] == 0){
$temp = $_FILES['file']['tmp_name'][$key];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$file = md5_file($temp) . time() . '.' . $ext;
if(in_array($ext, $allowed) === true && move_uploaded_file($temp, "uploads/{$file}") === true){
$succeeded[] = array('name' => $name, 'file' => $file);
$path = 'uploads';
$sql = "INSERT INTO users (img_path, img_name, img_type) VALUES ('$path', '$file', '$ext') WHERE id = '$session_user_id'";
$result = mysql_query($sql);
}else{
$failed[] = array('name' => $name);
}
}
}
init文件:
session_start();
mysql_connect('localhost','root','');
mysql_select_db('croglas');
mysql_query('SET CHARACTER SET utf8');
include 'functions.php';
if(logged_in() === true){
$session_user_id = $_SESSION['id'];
$user_data = user_data($session_user_id, 'id', 'username', 'password', 'email', 'address', 'zip', 'phone_number', 'city', 'type', 'points', 'img_path', 'img_name', 'img_type');
}
答案 0 :(得分:5)
这可能会返回代码忽略的错误:
INSERT INTO users (img_path, img_name, img_type) VALUES ('$path', '$file', '$ext') WHERE id = '$session_user_id'
INSERT
条款没有WHERE
条款。完全省略该条款:
INSERT INTO users (img_path, img_name, img_type) VALUES ('$path', '$file', '$ext')
此外,您应该在执行SQL语句后检查SQL错误。
修改或者......这需要是UPDATE
声明吗?令我感到奇怪的是,一个名为users
的表将保存图像记录。您是否更新 现有用户记录以包含新值? 那会有一个WHERE
子句。像这样:
UPDATE users SET img_page='$path', img_name='$file', img_type='$ext' WHERE id = '$session_user_id'
答案 1 :(得分:0)
您的查询错误,您在AN INSERT语句中无法where
。
应该是这样的,
INSERT INTO users (img_path, img_name, img_type) VALUES ('$path', '$file', '$ext')
另外,一个提示:使用mysqli
。