Php 数据未进入数据库
我不知道为什么,但在我的图像上传脚本中,没有任何数据被输入数据库。这是我一直在使用的同一个脚本,但我最近添加了Php 数据未进入数据库,php,mysql,mysqli,Php,Mysql,Mysqli,我不知道为什么,但在我的图像上传脚本中,没有任何数据被输入数据库。这是我一直在使用的同一个脚本,但我最近添加了if(isset))语句,以查看是否选中了某些复选框。图像正在上载到服务器,但数据库表仍然为空。有什么线索吗?我没有收到任何错误 if(isset($_POST['submit'])) { $count = count($_FILES['img_file']['name']); for($i = 0; $i < $count; ++$i){ $i
if(isset))
语句,以查看是否选中了某些复选框。图像正在上载到服务器,但数据库表仍然为空。有什么线索吗?我没有收到任何错误
if(isset($_POST['submit'])) {
$count = count($_FILES['img_file']['name']);
for($i = 0; $i < $count; ++$i){
$img_name = $_POST['img_name'];
$img_name = str_replace(' ', '_', $img_name);
$img_album = $_POST['img_album'];
$img_album = str_replace(' ', '_', $img_album);
$img_photographer = $_POST['img_photographer'];
$img_location = $_POST['img_location'];
if(isset($_POST['horror'])) { $horror = "1"; } else { $horror = "0"; }
if(isset($_POST['occult'])) { $occult = "1"; } else { $occult = "0"; }
if(isset($_POST['goth'])) { $goth = "1"; } else { $goth = "0"; }
if(isset($_POST['industrial'])) { $industrial = "1"; } else { $industrial = "0"; }
if(isset($_POST['fashion'])) { $fashion = "1"; } else { $fashion = "0"; }
if(isset($_POST['fetish'])) { $fetish = "1"; } else { $fetish = "0"; }
if(isset($_POST['avante-garde'])) { $avanteGarde = "1"; } else { $avanteGarde = "0"; }
if(isset($_POST['cosplay'])) { $cosplay = "1"; } else { $cosplay = "0"; }
if(isset($_POST['nude'])) { $nude = "1"; } else { $nude = "0"; }
$file_name = $_FILES["img_file"]["name"][$i];
$file_ext = end((explode(".", $file_name)));
$target = $_SERVER['DOCUMENT_ROOT']."/gallery/";
$img_rename = $img_name . '_' . $i . '.' . $file_ext;
$target = $target . $img_rename;
if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){
mysqli_query($conn, "INSERT INTO gallery_img (img_name, img_album, img_photographer, img_location, horror, occult, goth, industrial, fashion, fetish, avante-garde, cosplay, nude, file_location) VALUES ('$img_name', '$img_album', '$img_photographer', '$img_location', '$horror', '$occult', '$goth', '$industrial', '$fashion', '$fetish', '$avanteGarde', '$cosplay', '$nude', '$img_rename')") ;
echo '<div class="alert alert-success margin-top">Image "'.$file_name.'" successfully uploaded and renamed to '.$img_rename.'.</div>';
}else {
echo '<div class="alert alert-danger margin-top">Sorry, there was a problem uploading your images.</div>';
}
}
}
if(isset($_POST['submit']){
$count=count($_文件['img_文件']['name']);
对于($i=0;$i<$count;++$i){
$img_name=$_POST['img_name'];
$img_name=str_replace(“,”,$img_name);
$img_相册=$_POST['img_相册'];
$img_相册=str_替换(“,”,$img_相册);
$img_摄影师=$_POST['img_摄影师'];
$img_location=$_POST['img_location'];
if(isset($_POST['horror']){$horror=“1”}else{$horror=“0”}
if(isset($_POST['oclult']){$oclult=“1”}else{$oclult=“0”}
if(isset($_POST['goth']){$goth=“1”}else{$goth=“0”}
if(isset($_POST['industrial']){$industrial=“1”}else{$industrial=“0”}
if(isset($_POST['fashion']){$fashion=“1”}else{$fashion=“0”}
if(isset($_POST['fetish']){$fetish=“1”}else{$fetish=“0”}
if(isset($_POST['avante-garde']){$avanteGarde=“1”}else{$avanteGarde=“0”}
if(isset($_POST['cosplay']){$cosplay=“1”}else{$cosplay=“0”}
if(isset($_POST['nude']){$nude=“1”}else{$nude=“0”}
$file\u name=$\u FILES[“img\u file”][“name”][$i];
$file_ext=end((分解(“.”,$file_name));
$target=$\u服务器['DOCUMENT\u ROOT'].“/gallery/”;
$img_rename=$img_name.'.$i...$file_ext;
$target=$target.$img\u重命名;
如果(移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\{
mysqli_query($conn,“插入画廊img(img_名称、img_专辑、img_摄影师、img_位置、恐怖、神秘、哥特、工业、时尚、恋物癖、先锋派、角色扮演、裸体、文件位置)值(“$img_name”、“$img_album”、“$img_摄影师”、“$img_位置”、“$horroid”、“$goth”、“$industrial”、“$fashion”、“$fetish”、“$avantegard”、“$cosplay”、“$裸体”、“$img_rename”);
echo“Image”.$file_name.“已成功上载并重命名为“$img_rename.”;
}否则{
echo“抱歉,上载图像时出现问题。”;
}
}
}
您显然没有检查查询中的错误
注意到你的一个专栏的连字符了吗?似乎其他人没有向右滚动(足够)看到它并通知你
avante-garde
减去avante
并认为你想做数学。它应该像对其他一些列那样使用下划线重命名,或者用记号将其括起来garde
- 注意:我真的希望这不是你的错别字,毕竟你使用了下划线
$query = mysqli_query($conn, "INSERT INTO gallery_img (...) VALUES (...)");
然后
脚注: 您可能希望研究使用三元运算符,而不是使用一些/所有那些
if{…}else{…}
语句,另外它的代码要短得多
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
当然,您可以将empty
替换为isset
有一点值得注意,它可能(也)阻止您的查询执行,这就是查询的位置
if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){
mysqli_query($conn, "INSERT INTO gallery_img (...) VALUES (...)");
echo '<div class="...">Success...</div>';
}else {
echo '<div class="...">Error...</div>';
}
你有以下条件。如果你的上传失败,查询也会失败
if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){
mysqli_query($conn, "INSERT INTO gallery_img (...) VALUES (...)");
echo '<div class="...">Success...</div>';
}else {
echo '<div class="...">Error...</div>';
}
if(移动上传的文件($文件['img\u文件]['tmp\u名称][$i],$target)){
mysqli_查询($conn,“插入到库中_img(…)值(…)”);
呼应‘成功……’;
}否则{
回音“错误…”;
}
上载失败的几个可能原因,可能是以下原因之一:
- 文件太大
- 没有为要写入的文件夹设置权限
- 文件输入的打字错误
- 其他
mysqli\u错误($conn)
johncode和@nogad的正确说法是,你不知道你的插入是否有效。这一行->mysqli\u查询($conn,“insert-INTO…”
你应该使用参数化查询,你可以接受SQL注入。另外,$\u文件['img\u文件']['tmp\u名称]
和$\u文件['img\u文件]['name]
是唯一的数组吗?啊。我没有意识到它会被解释成这样。这就是为什么我的其他人都是先锋派的原因。我在那里理解了它,但没有注意到你指出的地方。现在工作得很有魅力。感谢其他的提示。这个脚本还在开发中,所以我肯定要做一些修改这是你的建议。非常感谢。@JesseElser非常欢迎你Jesse,很高兴能帮上忙;cheers@JesseElser关于连字符的补充说明。这也适用于变量
if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){
mysqli_query($conn, "INSERT INTO gallery_img (...) VALUES (...)");
echo '<div class="...">Success...</div>';
}else {
echo '<div class="...">Error...</div>';
}