有人可以帮我吗?我已经工作了大约3天。 这是关于更新记录。当我更新数据时,除“文件”外,所有数据均已更新。数据库中的“文件”变为空。
这是代码;
编辑/更新表格:
<label style="color:#e91e63">Attachement</label>
<div class="input-group input-group-md">
<span class="input-group-addon">
<i class="material-icons">file_upload</i>
</span>
<div class="form-line">
<input type="file" name="files" id="files" required>
</div>
</div>
<!-- Edited Date -->
<input type="hidden" name="id" id="id" />
<input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success waves-effect" />
<script>
$(document).ready(function(){
$('#edit').click(function(){
$('#insert').val("Insert");
$('#insert_form')[0].reset();
});
$(document).on('click', '.edit_data', function(){
var id = $(this).attr("id");
var extension = $('#files').val().split('.').pop().toLowerCase();
if(extension != '') {
if(jQuery.inArray(extension, ['gif','png','jpg','jpeg', 'pdf']) == -1) {
alert("Invalid File");
$('#files').val('');
return false;
}
}
$.ajax({
url:"script/fetch.php",
method:"POST",
data:{id:id},
dataType:"json",
success:function(data){
$('#dated').val(data.dated);
$('#ctrl_no').val(data.ctrl_no);
$('#title').val(data.title);
$('#category').val(data.category);
$('#file').val(data.file);
$('#fname').val(data.fname);
$('#adate').val(data.adate);
$('#createdby').val(data.createdby);
$('#id').val(data.id);
$('#insert').val("Update");
$('#update_Modal').modal('show');
}
});
});
$('#insert_form').on("submit", function(event){
event.preventDefault();
$.ajax({
url:"script/insert.php",
method:"POST",
data:$('#insert_form').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#insert_form')[0].reset();
$('#update_Modal').modal('hide');
$('#refresh').html(data);
}
});
});
});
</script>
从数据库中获取数据:
<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "record");
if(isset($_POST["id"]))
{
$query = "SELECT * FROM dashboard WHERE id = '".$_POST["id"]."'";
$result = mysqli_query($connect, $query);
$row = mysqli_fetch_array($result);
echo json_encode($row);
}
?>
更新数据。
<?php
$connect = mysqli_connect("localhost", "root", "", "record");
if(!empty($_POST))
{
$output = '';
$message = '';
$ctrl_no = mysqli_real_escape_string($connect, $_POST["ctrl_no"]);
$title = mysqli_real_escape_string($connect, $_POST["title"]);
$category = mysqli_real_escape_string($connect, $_POST["category"]);
$fname = mysqli_real_escape_string($connect, $_POST["fname"]);
$adate = mysqli_real_escape_string($connect, $_POST["adate"]);
$createdby = mysqli_real_escape_string($connect, $_POST["createdby"]);
//file upload
$file = '';
if($_FILES["files"]["name"] = '')
{
$file = upload_file();
}
else
{
$file = $_POST["file"];
}
if($_POST["id"] != '')
{
$query = "
UPDATE `dashboard`
SET
`ctrl_no`='$ctrl_no',
`title`='$title',
`category`='$category',
`file`='$file',
`fname`='$fname',
`adate` = '$adate',
`createdby` = '$createdby'
WHERE `id`='".$_POST["id"]."'";
$message = 'Data Updated.';
}
if(mysqli_query($connect, $query))
{
$output .= "<div class='alert alert-success alert-dismissible'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<strong>Success!</strong> $message
</div>";
$select_query = "SELECT * FROM `dashboard` ORDER BY `id` DESC";
$result = mysqli_query($connect, $select_query);
$output .= '
<table id="dataTable" style="width:100%" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th width="6%"><b>Date</b></th>
<th width="8%"><b>Control No.</b></th>
<th width="37%"><b>Title / Particular</b></th>
<th width="17%"><b>Category</b></th>
<th width="10%"><b>From /<br />End-user</b></th>
<th width="10%"><b>File</b></th>
<th width="7%"><b>Action</b></th>
</tr>
</thead>
<tbody>
';
while($row = mysqli_fetch_array($result))
{
$output .= '';
}
$output .= '</tbody></table>';
}
echo $output;
}
function upload_file()
{
if(isset($_FILES["files"]))
{
$extension = explode('.', $_FILES['files']['name']);
$new_name = rand() . '.' . $extension[1];
$destination = '../file/' . $new_name;
move_uploaded_file($_FILES['files']['tmp_name'], $destination);
return $new_name;
}
}
?>
<!-- Alert Success -->
<script>
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 5000); //timeout
</script>
唯一的问题是我无法更新文件。有任何想法吗?非常感谢您的帮助。谢谢。
答案 0 :(得分:0)
我认为,您正在通过输入字段分配文件名。如果有人没有通过输入字段分配值,则使用了 upload_file()方法。因此请尝试更改使用 $ _ POST [“ file”] = 来检查是否已分配名称,如果已分配,则在您的 upload_file()中定义使用的给定其他使用的名称。
<?php
$connect = mysqli_connect("localhost", "root", "", "record");
if(!empty($_POST))
{
$output = '';
$message = '';
$ctrl_no = mysqli_real_escape_string($connect, $_POST["ctrl_no"]);
$title = mysqli_real_escape_string($connect, $_POST["title"]);
$category = mysqli_real_escape_string($connect, $_POST["category"]);
$fname = mysqli_real_escape_string($connect, $_POST["fname"]);
$adate = mysqli_real_escape_string($connect, $_POST["adate"]);
$createdby = mysqli_real_escape_string($connect, $_POST["createdby"]);
//file upload
$file = '';
//make the change here in if
if($_POST["file"] = '')
{
$file = upload_file();
}
else
{
$file = $_POST["file"];
}
if($_POST["id"] != '')
{
$query = "
UPDATE `dashboard`
SET
`ctrl_no`='$ctrl_no',
`title`='$title',
`category`='$category',
`file`='$file',
`fname`='$fname',
`adate` = '$adate',
`createdby` = '$createdby'
WHERE `id`='".$_POST["id"]."'";
$message = 'Data Updated.';
}
if(mysqli_query($connect, $query))
{
$output .= "<div class='alert alert-success alert-dismissible'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<strong>Success!</strong> $message
</div>";
$select_query = "SELECT * FROM `dashboard` ORDER BY `id` DESC";
$result = mysqli_query($connect, $select_query);
$output .= '
<table id="dataTable" style="width:100%" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th width="6%"><b>Date</b></th>
<th width="8%"><b>Control No.</b></th>
<th width="37%"><b>Title / Particular</b></th>
<th width="17%"><b>Category</b></th>
<th width="10%"><b>From /<br />End-user</b></th>
<th width="10%"><b>File</b></th>
<th width="7%"><b>Action</b></th>
</tr>
</thead>
<tbody>
';
while($row = mysqli_fetch_array($result))
{
$output .= '';
}
$output .= '</tbody></table>';
}
echo $output;
}
function upload_file()
{
if(isset($_FILES["files"]))
{
$extension = explode('.', $_FILES['files']['name']);
$new_name = rand() . '.' . $extension[1];
$destination = '../file/' . $new_name;
move_uploaded_file($_FILES['files']['tmp_name'], $destination);
return $new_name;
}
}
?>
<!-- Alert Success -->
<script>
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 5000); //timeout
</script>
答案 1 :(得分:0)
确定要将文件发送到服务器端吗?如果不是,则您希望以此方式传递文件。(这是示例,我确信您将了解如何将其适合您的代码)。关键元素是formData:)
var formData = new FormData($("#formid")[0]);
$.ajax({
url:'url',
type: 'POST',
data: formData,
processData: false,
contentType: false,
async: false,
success:function(response){
if(response == '100'){
swal({
title: "Blog Added",
text: "Blog Added Successfully",
type: "success",
confirmButtonText: "OK",
showCancelButton: false,
}, function(){
/*location.reload();*/
window.location.href = 'redirect link';
});
}else{
toastr.error(response);
}
}
});