gpt4 book ai didi

PHP Ajax更新/编辑记录

转载 作者:行者123 更新时间:2023-11-29 17:00:50 25 4
gpt4 key购买 nike

任何人都可以帮我解决这个问题吗?我已经工作了大约3天了。这是关于更新记录。当我更新数据时,除了"file"之外的所有数据都会更新。数据库中的"file"变空。

这是代码;

编辑/更新表格:

<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'>&times;</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>

唯一的问题是我无法更新文件。有任何想法吗?非常感谢您的帮助。谢谢。

最佳答案

我认为,您正在通过输入字段分配文件名。如果有人没有通过输入字段分配值,则您已经使用了方法upload_file()。所以尝试更改您的if使用 $_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'>&times;</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>

关于PHP Ajax更新/编辑记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271654/

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