Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem using || and &&

    I have a script to check to see if pamount and pdate are set before those variable are inserted into the db. My problem is that if the pamount is left blank and the pdate is not it still inserts the pdate. I have tried to change the script to || instead of && but no help. Please let me know where I went wrong.

    Thanks

    PHP Code:

    <?
    //session_start();

    include "conn.php";
    $id=$_POST['id'];
    $type=$_POST['type'];
    $amount=$_POST['amount'];
    $pdate=$_POST['pdate'];
    $famount=$_POST['famount'];
    $fdate=$_POST['fdate'];
    /*
    echo "<pre>";
    print_r ($_POST);
    echo "</pre>";
    */
    if($_POST['famount']>0){
    $final"Select payid,id,famount,fdate from pay_tbl where id = '$id' and fdate!=0000-00-00 and type= '$type'";

    $query=mysql_query($final);
    }
    if(
    mysql_num_rows($query) >0) {

    echo 
    "<h3>";
    echo 
    "Error! Final Payment has been Posted! Please Check Date and Delete previous Final Payment !";
    echo 
    "</h3>";

    elseif ((
    $_POST['amount']) && ($_POST['famount'])) {
    echo 
    "<h4>";
    print 
    "Error you can't post a regular payment and final payment at the same time! Use back button to correct this problem!";
    echo 
    "</h4>";




    elseif(isset(
    $_POST['amount']) && ($_POST['pdate'])) {
    $query "INSERT INTO pay_tbl (id,type,amount,pdate) values('$id','$type','$amount','$pdate')";
    $result mysql_query($query) or die("Could not insert");
    header("location:community.php");



    elseif (isset(
    $_POST['famount']) && ($_POST['fdate'])){
    $query "INSERT INTO pay_tbl (id,type,famount,fdate) values('$id','$type','$famount','$fdate')";
    $result mysql_query($query) or die("Could not insert");
    header("location:community.php");
    } else {
    echo 
    "Error! To Post Payments you must have a Date and Amount! Use Back Button to Correct";
    }





    ?>

  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Some form fields will still submit a value, even if that value is an empty string. So to carry out accurate conditionals, you have to check for non-empty values. Also, If your going to the trouble of converting post vars to short vars, may as well use em.

    How does this work for you?

    PHP Code:
    include "conn.php";

    if(isset(
    $_POST)) {

        
    //long to short
        
    $id = (!empty($_POST['id'])) ? $_POST['id'] : false;
        
    $type = (!empty($_POST['type'])) ? $_POST['type'] : false;
        
    $amount = (!empty($_POST['amount'])) ? $_POST['amount'] : false;
        
    $pdate = (!empty($_POST['pdate'])) ? $_POST['pdate'] : false;
        
    $famount = (!empty($_POST['famount'])) ? $_POST['famount'] : false;
        
    $fdate = (!empty($_POST['fdate'])) ? $_POST['fdate'] : false;
        
    $final_payment 0;
        
        if( 
    $famount && $famount ){
            
            
    $final "Select payid,id,famount,fdate from pay_tbl where id = '$id' and fdate != 0000-00-00 and type= '$type'";
            
    $query mysql_query($final);
            
    $final_payment mysql_num_rows($query);
            
        }
        
        if( 
    $final_payment ) {
            
            echo 
    "<h3>";
            echo 
    "Error! Final Payment has been Posted! Please Check Date and Delete previous Final Payment !";
            echo 
    "</h3>";
            
        } elseif ( 
    $amount && $famount ) {
            
            echo 
    "<h4>";
            print 
    "Error you can't post a regular payment and final payment at the same time! Use back button to correct this problem!";
            echo 
    "</h4>";
            
        } elseif ( 
    $amount && $pdate ) {
            
            
    $sql "INSERT INTO pay_tbl (id,type,amount,pdate) values('$id','$type','$amount','$pdate')";
            
    $query mysql_query($sql) or die("Could not insert");
            
    header("location:community.php");
            exit;
            
        } elseif ( 
    $famount && $fdate ){
            
            
    $sql "INSERT INTO pay_tbl (id,type,famount,fdate) values('$id','$type','$famount','$fdate')";
            
    $query mysql_query($sql) or die("Could not insert");
            
    header("location:community.php");
            exit;
            
        } else {
            
            echo 
    "Error! To Post Payments you must have a Date and Amount! Use Back Button to Correct";
            
        }
        
    }
    //end post processing 
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #3
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot that work great. Question please explain to me what you meant by "converting post vars to short vars".


    Thanks

  • #4
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    long = $_POST['amount'];
    short = $amount

    //long to short
    $amount = $_POST['amount'];
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •