...

View Full Version : problem using || and &&



mwm
08-27-2007, 02:19 PM
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




<?
//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";
}





?>

meth
08-27-2007, 03:27 PM
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?



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 > 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);
$final_payment = mysql_num_rows($query);

}

if( $final_payment > 0 ) {

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

mwm
08-27-2007, 03:44 PM
Thanks alot that work great. Question please explain to me what you meant by "converting post vars to short vars".


Thanks

meth
08-27-2007, 03:47 PM
long = $_POST['amount'];
short = $amount

//long to short
$amount = $_POST['amount'];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum