【问题标题】:PHP Script insert even if file upload/field element is disabled即使文件上传/字段元素被禁用,PHP 脚本也会插入
【发布时间】:2015-01-31 12:14:09
【问题描述】:

您好,我似乎无法确定问题所在。我有一个带有元素的表格。你能检查一下脚本吗?我只知道它缺少一些东西,但像我这样的菜鸟就是不知道。基本上,选择智能货币单选按钮时,BPI被禁用,反之亦然。它应该在数据库中插入数据输入。如果选择的无线电 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>

【问题讨论】:

  • 只有在isset($_FILES['filename']) 时才进行数据库插入。如果文件上传被禁用,则不会设置该变量,因此不会进行数据库插入。
  • @Barmar 那么先生应该怎么做?
  • 如果他们不上传文件会发生什么?看起来需要它,因为您在 INSERT 中有一个 filename 字段。
  • @Barmar 当文件上传被禁用时,这意味着第一个选项被启用并且它接受'contactno' txtfield 中的输入。不过,它应该插入到 db 中,并且 db 字段中的文件名字段留空。

标签: php mysql insert sql-insert


【解决方案1】:

if(isset($_FILES['filename'])) 块之外执行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');
    }
}

【讨论】:

  • 终于插入了!很棒的先生。但是在我接受您的出色回答之前,我在页面左上方收到此通知,如何删除它?注意:未定义变量:transactionNo inC:\xampp\htdocs\MCC\paymentform2.php 在第 56 行 注意:未定义变量:rad in C:\xampp\htdocs\MCC\paymentform2.php 在第 56 行 注意:未定义变量:contactNo in C:\xampp\htdocs\MCC\paymentform2.php 在第 56 行 ... 第 56 行是插入查询行
  • 您需要设置INSERT 行中的所有变量。例如$transactionNo = $_POST['transaction_no'];
  • 哦。我应该在哪里添加该代码?在最顶端还是在哪里?
  • if之前可以,之后可以,没关系。
  • 这不是火箭科学,当你知道它们应该是什么时,在你使用它们之前设置变量。
猜你喜欢
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
相关资源
最近更新 更多