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

时间:2017-01-19 06:01:23

标签: php mysql

所以我想创建一个简单的电子商务网站。一旦我提交表单(btn-submit),我就无法将任何数据插入到我的数据库中。只有地址和联系电话验证有效。

这是我的代码:

    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.";   
        }   

    }

}

我的代码可能有什么问题?我在其他页面中做了类似的查询,但这是唯一一个不起作用的查询。任何帮助将不胜感激!提前谢谢!

2 个答案:

答案 0 :(得分:0)

尝试理解代码流程:

<div class="cover">
<ul class="bxslider">
  <li><img src="http://searchengineland.com/figz/wp-content/seloads/2015/12/google-amp-fast-speed-travel-ss-1920.jpg" title="Funky roots" /></li>
  <li><img src="http://searchengineland.com/figz/wp-content/seloads/2015/12/google-amp-fast-speed-travel-ss-1920.jpg" title="The long and winding road" /></li>
  <li><img src="http://searchengineland.com/figz/wp-content/seloads/2015/12/google-amp-fast-speed-travel-ss-1920.jpg" /></li>
</ul>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bxslider/4.2.5/jquery.bxslider.min.js"></script>
<script>
$('.bxslider').bxSlider({
  auto: true,
  prevText:"prev",
  nextText:"next",
  pager:false
});
</script>

因此,只有当代码中没有验证错误并且if( !$error ) { // This will only works when **$error is false and the not of false is true**, otherwise this block does not execute } 包含$error

时,此代码才有效

答案 1 :(得分:0)

//$userRow is not define any where...
//to check error occur or not :
echo $error;
if(!$error)
{
    echo "IN IF";
    //also go with die..
    $res = mysql_query($query) or die();
}
else
{
    echo "IN ELSE";
}