dongyan1974 2017-01-19 06:01
浏览 75
已采纳

我的代码显示没有errmsg,但没有将任何数据插入数据库

So I am trying to make a simple e-commerce site. Once I submit the form (btn-submit), I am not able to insert any data to my database. Only the address and contact number verification works.

Here is my code:

    if ( isset($_POST['btn-submit']) ) {

    // clean user inputs 
    $oadd = trim($_POST['oadd']);
    $oadd = strip_tags($oadd);
    $oadd = htmlspecialchars($oadd);

    $contact = trim($_POST['contact']);
    $contact = strip_tags($contact);
    $contact = htmlspecialchars($contact);


    // address validation
    if (empty($oadd)) {
        $error = true;
        $oaddError = "Please enter a valid address.";
    } else if (strlen($oadd) < 5) {
        $error = true;
        $oaddError = "Please enter a valid address.";
    }

    // contact number validation
    if (empty($contact)) {
        $error = true;
        $contactError = "Please enter your contact number.";
    } else if (strlen($contact) < 7) {
        $error = true;
        $contactError = "Contact number must have atleast 7 digits.";
    } else if (!preg_match("/^[0-9 ]+$/",$lname)) {
        $error = true;
        $lnameError = "Please enter a valid contact number.";
    }

    // if there's no error, continue to place order
    if( !$error ) {
        $query = 'INSERT INTO cust_order(Order_Date, Order_Status, Order_Total , Address, Contact_No) VALUES (CURDATE(), "in process" , (SELECT SUM(p.Product_Price) FROM cart c, product p WHERE c.Prod_ID = p.Product_ID and c. User_ID = "'.$userRow['User_ID'].'"),"'.$oadd.'","'. $contact.'")';
        $res = mysql_query($query);

        if ($res) {
            $errTyp = "success";
            $errMSG = "Your order has been placed. To view the details, go to your order history";
            unset($oadd);
            unset($contact);
        } else {
            $errTyp = "danger";
            $errMSG = "Something went wrong. Please try again later.";   
        }   

    }

}

What could possibly be wrong with my code? I did similar queries in the other pages but this is the only one not working. Any help would be greatly appreciated! Thanks in advance!

  • 写回答

2条回答 默认 最新

  • douyicao2199 2017-01-19 06:06
    关注

    Try to understand the code flow:

    if( !$error ) {
        // This will only works when **$error is false and the not of false is true**, otherwise this block does not execute
    }
    

    So this code works only when there is no validation error occurs in your code and $error contains false

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号