Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-27-2007, 02:19 PM   PM User | #1
mwm
Regular Coder

 
Join Date: Oct 2005
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
mwm has a little shameless behaviour in the past
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";
}





?>
mwm is offline   Reply With Quote
Old 08-27-2007, 03:27 PM   PM User | #2
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
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.
meth is offline   Reply With Quote
Old 08-27-2007, 03:44 PM   PM User | #3
mwm
Regular Coder

 
Join Date: Oct 2005
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
mwm has a little shameless behaviour in the past
Thanks alot that work great. Question please explain to me what you meant by "converting post vars to short vars".


Thanks
mwm is offline   Reply With Quote
Old 08-27-2007, 03:47 PM   PM User | #4
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
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.
meth is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:27 AM.


Advertisement
Log in to turn off these ads.