gpt4 book ai didi

即使文件上传/字段元素被禁用,PHP 脚本也会插入

转载 作者:行者123 更新时间:2023-11-29 23:07:08 24 4
gpt4 key购买 nike

嗨,我似乎无法确定出了什么问题。我有一个包含元素的表单。你能检查一下脚本吗?我只知道它缺少一些东西,但像我这样的菜鸟就是不知道。基本上,当选择“智能货币”单选按钮时,BPI 将被禁用,反之亦然。它应该将数据输入插入数据库中。如果所选的 radio btn 是 BPI,则它可以正常工作,但如果选择了智能货币并且用户输入数据并且禁用 BPI/文件上传,则它不会在数据库中插入任何内容。您能告诉我该怎么做吗?我认为脚本有点放置错误,因为查询位于文件上传脚本下方。我认为当没有任何内容可上传/该选项被禁用时它不会插入任何内容。我猜文件上传脚本会干扰。

PHP:

if(isset($_FILES['filename'])){
$errors = array();
$file_name = $_FILES['filename']['name'];
$file_size =$_FILES['filename']['size'];
$file_tmp =$_FILES['filename']['tmp_name'];
$file_type=$_FILES['filename']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));


$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}

// if no error...
if (empty($errors)==true) {

// upload the file...
move_uploaded_file($file_tmp,"uploads/".$file_name);

$servername = "localhost";
$username = "root";
$password = " ";
$dbname = "admin";

// create new record in the database
include ("dbinfo.php");

mysql_query("INSERT INTO payment_form (Tracking, date, mode, ContactNo, totalsent, datesent, filename) VALUES ('$transactionNo', NOW(), '$rad', '$contactNo', '$totalSent', '$dateSent', '$file_name')") ;

header('Location: paymentform_success.php');
}else{
print_r($errors);
}
}

表格:

<form name="form" method="POST" enctype="multipart/form-data">
<table width="416" height="245" border="1" align="center">
<tr>
<td colspan="2">Transaction No: <input type="text" name="transaction_no" id="transaction_no" /> </td>
</tr>
<tr>
<td colspan="2" align="center">Please select the mode of payment</td>
</tr>
<tr>
<td width="183" align="center"><input name="rad" type="radio" onclick="enableField(this)" value="Smart Money">
Smart Money</td>
<td width="201" align="center"><input name="rad" type="radio" onclick="enableField(this)" value="BPI"> BPI Bank Deposit</td>
</tr>
<tr>
<td align="center"><input name="contactno" type="text" disabled="disabled" id="contactno"></td>
<td align="center"><input name="filename" type="file" id="filename" disabled="disabled"/></td>
</tr>
<tr>
<td>Total amount sent:</td>
<td>&nbsp;<input type="text" name="totalsent" id="totalsent" /></td>
</tr>
<tr>
<td>Date sent:</td>
<td>&nbsp;<input type="text" name="datesent" id="datesent" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" id="submit" value="Submit" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form" />

</form>

JS 禁用/启用

<script type="text/javascript">
function enableField(obj){
var form=obj.form;
var txtNames=['contactno','filename'], f;
var rads=document.getElementsByName(obj.name), r, i=0;
while(r=rads[i++]){
f=form[txtNames[i-1]];
if(r.checked){
f.removeAttribute('disabled');
f.focus();
}
else{
f.value='';
f.setAttribute('disabled','disabled')
}
}
}
</script>

最佳答案

if(isset($_FILES['filename'])) block 之外执行 INSERT

if (isset($_POST['submit'])) {
$errors = array();
if (isset($_FILES['filename'])) {
$file_name = $_FILES['filename']['name'];
$file_size =$_FILES['filename']['size'];
$file_tmp =$_FILES['filename']['tmp_name'];
$file_type=$_FILES['filename']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));

$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}

// if no error...
if (empty($errors)==true) {

// upload the file...
move_uploaded_file($file_tmp,"uploads/".$file_name);

}else{
print_r($errors);
}
} else {
$file_name = '';
}

if (empty($errors)) {
$servername = "localhost";
$username = "root";
$password = " ";
$dbname = "admin";

// create new record in the database
include ("dbinfo.php");

$transactionNo = $_POST['transaction_no'];
$rad = $_POST['rad'];
$contactNo = $_POST['contactno'];
$totalSent = $_POST['totalsent'];
$dateSent = $_POST['datesent'];

mysql_query("INSERT INTO payment_form (Tracking, date, mode, ContactNo, totalsent, datesent, filename) VALUES ('$transactionNo', NOW(), '$rad', '$contactNo', '$totalSent', '$dateSent', '$file_name')") ;

header('Location: paymentform_success.php');
}
}

关于即使文件上传/字段元素被禁用,PHP 脚本也会插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28251458/

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