gpt4 book ai didi

javascript - 当我只上传一次时,图像在表格中插入了两次

转载 作者:可可西里 更新时间:2023-11-01 07:57:54 25 4
gpt4 key购买 nike

我在这里上传两个代码,一个是 Index.php,另一个是 ImageUpload.php,当我上传图像时,文件被移动到上传文件夹中,图像名称两次存储在数据库中,我只想在数据库中存储一个数据我上传的每张图片都是数据库

images | int(11)    
name | varchar(200)
image | longtext



<?php
include("config.php");

if(isset($_POST) && !empty($_FILES['image']['name'])){
$valid_extensions = array('jpeg', 'jpg', 'png','pdf','doc','docx'); // valid extensions
$filepath = 'uploads/';
if(!is_dir($filepath)){

mkdir($filepath, 0755, true);
}

$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];

list($txt, $ext) = explode(".", $img); // alternative $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

$image_name = time().".".$ext;
$image_base64 =md5(rand());
$image = 'data:image/'.$image_name.';base64,'.$image_base64;

$query = "insert into images(name,image) values('".$image_name."','".$image."')";
mysqli_query($con,$query) or die(mysqli_error($con));

// Validate File extension
if(in_array($ext, $valid_extensions))
{
$filepath=$filepath.$image_name;
if(move_uploaded_file($tmp,$filepath)){
echo "<img width='100px' src='".$filepath."'>";
}else{
echo "Failed to upload image on server";
}
}else
{
echo 'Please upload valid image';
}
}else{
echo "Please select image to upload";
}

?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Ajax image upload without refreshing page with validation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
</head>
<body>

<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8">
<h2>PHP - Upload image on server with validation using jQuery Ajax</h2>
<div class="panel panel-info">
<div class="panel-heading">File upload using ajax in PHP</div>
<div class="panel-body">

<form action="imageUpload.php" enctype="multipart/form-data" class="form-horizontal" method="post">
<div class="preview"></div>
<input type="file" name="image" class="form-control" />
<br/>
<button class="btn btn-block btn-primary btn-upload" id="upload" name="upload">Upload</button>
</form>
</div>
</div>
</div>
</div>
</div>

<script type="text/javascript">
$(document).ready(function() {
$(".btn-upload").click(function(){
$(".form-horizontal").ajaxForm({target: '.preview'}).submit();
});
});
</script>


</body>
</html>

最佳答案

这是因为您的 ajax 请求运行了两次。只需在表单元素上添加 onsubmit="return false;" 即可。使得表单仅通过ajax代码提交。

<form action="imageUpload.php" enctype="multipart/form-data" class="form-horizontal" method="post" onsubmit="return false;">
<div class="preview"></div>
<input type="file" name="image" class="form-control" />
<br/>
<button class="btn btn-block btn-primary btn-upload" id="upload" name="upload">Upload</button>
</form>

关于javascript - 当我只上传一次时,图像在表格中插入了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com