Jump to content

Adding isset() causes database value to be ignored


ICYUH8

Recommended Posts

I'm trying to fix an old website that was developed several years ago which had been mothballed. Now I want to recesutate it and get it up and running again.

 

What I'm trying to do at this point is fix any Warnings, Notices, and of course Errors. Being a new guy to PHP it's been kind of challenging, but definitley rewarding fixing whatever I can (thank you google). But there is one problem I cannot figure out, hopefully someone here can help me.

 

I'm getting a bunch of notices on one page when it loads. I know where the problem is, and thought I knew how to fix it which is just by adding isset(). But adding it is not giving me the desired results.

 

Adding isset() removes the notices, but a database value that is set to 'N' is ignored and interpretted as 'Y'. 

FYI, the affected database values are 'Y' which forces a paid membership, or 'N' which makes it free and no payment required.

 

 

 

These are the notices when the page loads:

Notice: Undefined index: hou_title /home/mysite/public_html/config.php(420) : eval()'d code:63
Notice: Undefined index: hou_desc /home/mysite/public_html/config.php(420) : eval()'d code:73
Notice: Undefined index: hou_areax /home/mysite/public_html/config.php(420) : eval()'d code:80
Notice: Undefined index: hou_areay /home/mysite/public_html/config.php(420) : eval()'d code:81
Notice: Undefined index: hou_rooms /home/mysite/public_html/config.php(420) : eval()'d code:91
Notice: Undefined index: hou_travlrs /home/mysite/public_html/config.php(420) : eval()'d code:98

Please see my code below. I pasted the entire page, but the line that's causing me issues starts at 16.

<?

    include "init.php";

    $title2 = "- Add Your Home";

    $submitted=false;
     
    
    if(!$user) {
        $center = message(eval_template("register_or_login"));
    } else {
    
        #check paypal balance here
        
        if($settings['set_payment']=='Y') {    // this works the way it should, however the page gives Undefined index warnings
    //    if (isset($settings['set_payment'])=='Y') {      // this remove the notices, but will go to paypal even though set_payment in DB is set to 'N'
            $now=time();
            $r = query("SELECT * FROM {$prefix}payments WHERE pay_usr_id=$user[usr_id] AND pay_validupto>=$now")
                    or cdie("gps:".mysql_error());
                    
            if(numr($r) == 0) {
                $r = query("SELECT * FROM {$prefix}prices WHERE pri_locked='N' ORDER BY pri_days ASC")
                            or cdie("gps2:".mysql_error());
            
                if(numr($r)==0)
                    cdie("No payment plans in database, please visit later.");
                $pay = "<b>Please select a plan below:</b><br>
                    <table border=0 width=100%>
                    <tr bgcolor=#eeeeee>
                        <td width=70%>Memberships</td>
                        <td align=center># Days valid</td>
                        <td align=center>Price (USD)</td>
                    </tr>
                
                ";
                while($price = fetch($r)) {
                    $pay .= "
                    <tr>
                        <td>
                            <a href='$settings[set_site_url]/pay.php?pri_id=$price[pri_id]'>$price[pri_title]</a><br>
                            <span style='color:gray'>$price[pri_desc]
                        </td>
                        <td align=center>$price[pri_days]</td>
                        <td align=center>\$$price[pri_amount]</td>
                    </tr>
                    ";
                }
                $pay .= "</table>";
            
                $center = message("You either need to open a new membership account, or your balance is too low to add a listing!<br><br>$pay");
                
            }
            
        } else {
    
            $ret= "<select name=hou_cou_id id=hou_cou_id
                onchange=\"javascript:
                    //var cou_obj = document.getElementById('hou_cou_id');
                    if(this.value != '0') {
                        var ifra = document.getElementById('loadlist');
                        ifra.src = '$settings[set_site_url]/list_cities_array.php?cou_id='+this.value;
                        document.getElementById('clist').innerHTML = 'Loading cities list';
                    } else
                        document.getElementById('clist').innerHTML = '<i>No country selected to list cities of</i>';
                
                \"
            >";
            $ret .="<option value='0'>Select country</option>";
            
            $q = "SELECT * FROM {$prefix}countries WHERE cou_locked='N' ORDER BY cou_title ASC";
            $r = query($q)
                or ddie(mysql_error().":".$q);
            if(numr($r)==0) return "None";
            $arr = Array();
            $arr2 = Array();
            
            
            
            while($c = fetch($r)) {
                
                $arr []= "<option value='$c[cou_id]'>"._html($c['cou_title'])."</option>";
                
                $r2 = query("SELECT * FROM {$prefix}cities WHERE cit_cou_id=$c[cou_id] ORDER BY cit_title ASC");
                $temp = "<option value=c$c[cou_id]>"._html($c['cou_title'])."</option>";
                
                if(numr($r2) > 0) {
                    while($ct = fetch($r2)) {
                        //echo "$ct[cit_title] = ";
                        $ct['cit_title'] = enc_unicode($ct['cit_title']);
                        //echo "$ct[cit_title]<br>";
                        
                        $temp .= "<option value='i$ct[cit_id]'>    - $ct[cit_title]</option>\n";
                        $selected=true;
                    }
                }
                    
                $arr2 []= $temp;
            }
            
            $hou_cou_id = $ret . implode("\n",$arr) ."</select>";        
            $country_wise_cities= implode("\n",$arr2);
            
            $months =  Array("January","February","March","April","May","June","July","August",    "September","October","November","December");
            $cmonth = date("F");
            $dat_date1_mon = Array();
            foreach($months as $k=>$month) {
                $sel = $cmonth==$month ? ' selected' : '';
                if(isset($_POST['dat_date1_mon']))
                    if($k+1==intval($_POST['dat_date1_mon']))
                        $sel=' selected';
                $dat_date1_mon []= "<option value='".($k+1)."'$sel>$month</option>";
            }
            $dat_date1_mon = implode("\n",$dat_date1_mon);
    
            $dat_date2_mon = Array();
            foreach($months as $k=>$month) {
                $sel = $cmonth==$month ? ' selected' : '';
                if(isset($_POST['dat_date2_mon']))
                    if($k+1==intval($_POST['dat_date2_mon']))
                        $sel=' selected';
                $dat_date2_mon []= "<option value='".($k+1)."'$sel>$month</option>";
            }
            $dat_date2_mon = implode("\n",$dat_date2_mon);
    
            
            $dat_date1_day = Array();
            for($i=1; $i<31;$i++) {
                if(isset($_POST['dat_date1_day'])) {
                    if($i==intval($_POST['dat_date1_day']))
                        $sel=' selected';
                    else
                        $sel='';
                } else
                    $sel = $i==intval(date("d")) ? ' selected' : '';
                $dat_date1_day []= "<option value='$i'$sel>$i</option>";
            }
            $dat_date1_day = implode("\n",$dat_date1_day);
    
            
            $dat_date2_day = Array();
            for($i=1; $i<31;$i++) {
                if(isset($_POST['dat_date2_day'])) {
                    if($i==intval($_POST['dat_date2_day']))
                        $sel=' selected';
                    else
                        $sel='';
                } else
                    $sel = $i==intval(date("d")) ? ' selected' : '';
                $dat_date2_day []= "<option value='$i'$sel>$i</option>";
            }
            $dat_date2_day = implode("\n",$dat_date2_day);
    
    
    
            $cyear = intval(date("Y"));
            $dat_date1_year = Array();
            for($i=$cyear; $i<=$cyear+10; $i++) {
                $dat_date1_year []= "<option value='$i'>$i</option>";
            }
            $dat_date2_year = $dat_date1_year = implode("\n",$dat_date1_year);
    
    
    
            if(!isset($_GET['process'])) {
                $center = eval_template("addhome");
            } else {
                $error = '';
                check_magic_quotes();
                
                $dat_date1 = mktime (0,0,0,$_POST['dat_date1_mon'],$_POST['dat_date1_day'],$_POST['dat_date1_year']);
                $dat_date2 = mktime (0,0,0,$_POST['dat_date2_mon'],$_POST['dat_date2_day'],$_POST['dat_date2_year']);
    
                //echo "From ".date("F d, Y",$dat_date1) ." to ".date("F d, Y",$dat_date2) ."<hr>";
    
                
                if(intval($_POST['hou_cou_id']) ==0) $error = "No country selected";
                else if($dat_date1 == $dat_date2) $error = "Availability dates are the same";
                else if($dat_date1 > $dat_date2) $error = "Invalid availability dates. Date 'From' is after date 'To' that you've selected";
                else if(!isset($_POST['hou_cit_id'])) $error = "No city selected. The country you selected does have cities listed";
                else if(strlen($_POST['hou_title']) < 5) $error = "Title too short. 5 character minimum required";
                else if(strlen($_POST['hou_desc']) < 5) $error = "Description too short. 5 character minimum required";
                else if(intval($_POST['hou_areax']) < 1) $error = "Invalid house area measure X";
                else if(intval($_POST['hou_areay']) < 1) $error = "Invalid house area measure Y";
                else if(intval($_POST['hou_rooms']) < 1) $error = "Invalid rooms specification";
                                else if(intval($_POST['hou_travlrs']) < 1) $error = "Invalid number of travelers";
                
                if($error != '') {
                    $center = eval_template("addhome");
                
                } else {
                    #process form
                    
                    $array = $_POST;
                    $array['hou_date'] = time();
                    $array['hou_ip'] = _ip();
                    $array['hou_usr_id'] = $user['usr_id'];
                    $q = make_ins_query("houses", $array,"dat_date1_day,dat_date1_mon,dat_date1_year,dat_date2_day,dat_date2_mon,dat_date2_year,dest_list,destinations,locations");
    
                    $date = date("F d, Y", time());
                    $time = date("h:i:s A", time());
                    if(query($q)) {
                        $house_id = mysql_insert_id();
                        $q = "INSERT INTO `${prefix}destinations` (`des_reg_id`,`des_cou_id`,`des_cit_id`,`des_hou_id`,`des_usr_id`) VALUES ";
                                    
                        $destarr = explode("^",$_POST['locations']);
                        $qarr = Array();
                        $any=false;
                        foreach($destarr as $de) {
                            if(eregi("i",$de)) {
                                $city = city(intval(substr($de,1,strlen($de)-1)));
                                if($city) {
                                    $regi = region($city['cit_reg_id']);
                                    if(!$regi) continue;
                                    $coun = country($city['cit_cou_id']);
                                    if(!$coun) continue;
                                } else continue;
                            } else
                            if(eregi("c",$de)) {
                                $coun = country(intval(substr($de,1,strlen($de)-1)));
                                if($coun) {
                                    $regi = region($coun['cou_reg_id']);
                                    $city = Array('cit_id'=>0);
                                    if(!$regi) continue;
                                } else continue;
                            } else continue;                        
                            $qarr []= "($regi[reg_id],$coun[cou_id],$city[cit_id],$house_id,$user[usr_id])";
                            $any=true;
                        }
                        if($any) {
                            $q .= implode(",\n",$qarr);
                            query($q) or cdie(mysql_error()."<hr>DESIRED LOCATIONS INSERTION ERROR : $q");
                        }
                        $q = "INSERT INTO `{$prefix}dates` (dat_hou_id,dat_usr_id,dat_date1,dat_date2) VALUES($house_id,$user[usr_id],$dat_date1,$dat_date2)";
                        query($q)
                             or cdie(mysql_error()."<hr>DATE INSERTION ERROR $q");
                        
                        $body =
                        "Dear admin,
                        
                        A new listing has been submitted to the database for approval.
                        Submitter  : '"._html($user['usr_name'])."' also known as '"._html($user['usr_title'])."'
                        Time       : $time
                        Date       : $date
                        IP Address : $array[hou_ip]
                        
                        
                        
                        Approval link (where you can view details and approve the new listing):
                        
                        $settings[set_site_url]/admin.php
                        
                        
                        ----------------------------------------------------------------------------
                        This is an automated email sent to the administrator.";
                        
                        @mail("$settings[set_email]","New listing added for approval",$body,"From:\"$settings[set_from]\" <$settings[set_email]>");
                        
                        $center = message("Your listing has been submitted successfully and will appear after administrator approval.<br /> <a href='$settings[set_site_url]/index.php'>Click here to return to homepage</a>");
                        
                        
                        $submitted=true;
                    } else {
                        $center = message("Internal error : please report to administrator at <a href='mailto:$settings[set_email]'>$settings[set_email]</a>");
                    }
                    
                }
            }
        }
    }


    echo eval_template("header");
    echo eval_template("body");
    echo eval_template("footer");
    
    cdie();
    
?>

Thank you to whoever has the solution and can explain it to me.

 

 

Link to comment
Share on other sites

if(isset($variable)) is a check all on it's own.  it evaluates to true or false.  putting that on the left side of a conditional evaluation is pointless.  you may be able to work around the problem by simply adding in the isset check as an extra contition for the evaluation you are currently performing, if not wou will need to wrap the whole check inside a seperate if check.

 

try changing your line 16 to the following first and see how it goes :

 

if($settings['set_payment']=='Y' && isset($settings['set_payment'])) {

I think it should go that way round (in that if conditionals are parsed in a left to right manner) if you still get the warning try swaping the two sides of the && around and see what happens.  Failing that you will need to wrap the whole lot in in initial

if(isset($settings['set_payment'])){ //open if isset()
if($settings['set_payment'] == "Y"){ // your current if =="Y"
... //code for matching if=="Y"
} // close if == "Y"
... //code for not matching the if == "Y"
}//close the if isset()
Link to comment
Share on other sites

To clarify, isset returns a boolean value of true or false, so basically your condition is saying:

 

if the array index is set:

 

if (true=="Y")
 

or 

 

if the array index is not set:

if (false=="Y")
 

Yes, try changing your condition to what Muddy_Funster showed - however, you need to reverse it.

 

This will still give you the error, since you are evaluating the value first. 

 

 

if($settings['set_payment']=='Y' && isset($settings['set_payment'])) {
 

This will first check if the index is set, and will prevent the error, since the 2nd half of the condition won't be evaluated if the first half is false.

 

 

if(isset($settings['set_payment']) && $settings['set_payment']=='Y') {
Link to comment
Share on other sites

Thank you guys. Taking both of your ideas, the results were the same in both instances. The database value was correctly interpreted as 'Y', however I still have the same notices appearing.

 

Here is what I tried so you can see:

if($settings['set_payment']=='Y' && isset($settings['set_payment'])) {
if(isset($settings['set_payment']) && $settings['set_payment']=='Y') {

Now I'm studying how Muddy wants the things wrapped so I can try that. If I'm looking at it correctly, I'll need to move blocks of code around to accomplish it.

 

I'll post those results once I figure it out.

Link to comment
Share on other sites

As I said before, the isset needs to come first. There is no difference between that 2nd one and putting it in separate if statements, so you shouldn't need to do that. Did you apply this to ALL of your conditions? Look back at your warning messages: you are getting multiple warnings, each for different indexes. IOW you need to apply this to more than one place in your code.

Link to comment
Share on other sites

For example, here is another part of your code that tries to use array indexes in a condition without first seeing if they are set:

 

else if(strlen($_POST['hou_title']) < 5) $error = "Title too short. 5 character minimum required";
else if(strlen($_POST['hou_desc']) < 5) $error = "Description too short. 5 character minimum required";
else if(intval($_POST['hou_areax']) < 1) $error = "Invalid house area measure X";
else if(intval($_POST['hou_areay']) < 1) $error = "Invalid house area measure Y";
else if(intval($_POST['hou_rooms']) < 1) $error = "Invalid rooms specification";
You need to throw an isset into the mix on all of those. Basically anywhere in your code where you use a variable or array[index] in a condition you need to first check if it is set.

 

Look at the error messages, they tell you what vars/indexes are the offense. Then ctrl+f for them.

Link to comment
Share on other sites

Sorry, I guess I don't fully understand what you meant by having the isset come first. I thought the second statement in my last post was what you meant since it was at the beginning of the statement and thus it was first.  I'll check that again.

 

 

 

For example, here is another part of your code that tries to use array indexes in a condition without first seeing if they are set:
 

else if(strlen($_POST['hou_title']) < 5) $error = "Title too short. 5 character minimum required";
else if(strlen($_POST['hou_desc']) < 5) $error = "Description too short. 5 character minimum required";
else if(intval($_POST['hou_areax']) < 1) $error = "Invalid house area measure X";
else if(intval($_POST['hou_areay']) < 1) $error = "Invalid house area measure Y";
else if(intval($_POST['hou_rooms']) < 1) $error = "Invalid rooms specification";
You need to throw an isset into the mix on all of those. Basically anywhere in your code where you use a variable or array[index] in a condition you need to first check if it is set.

Look at the error messages, they tell you what vars/indexes are the offense. Then ctrl+f for them.

 

 

I was looking at this part too since the notices match these exactly. I will add them in and post the results.

 

Out of curiousity, is it possible to have just one isset cover all of them so they don't each have to be added individualy? It just seems like redundancy to me. But what do I know for a new guy. Understood if it is possible, but would require recoding much of the file.

 

Thanks again for your help with this.

Link to comment
Share on other sites

... - however, you need to reverse it.

 

This will still give you the error, since you are evaluating the value first. 

 

 

Cheers Josh, I couldn't remember if it was a left to right or a right to left evaluation on conditionals in PHP.

 

 

...Now I'm studying how Muddy wants the things wrapped so I can try that. If I'm looking at it correctly, I'll need to move blocks of code around to accomplish it.

 

I'll post those results once I figure it out.

 

You don't need to maove anything about, just add the extra lines of code arround (hence the "wrap") the existing code starting before and ending after what you have already.

Link to comment
Share on other sites

Sorry, I guess I don't fully understand what you meant by having the isset come first. I thought the second statement in my last post was what you meant since it was at the beginning of the statement and thus it was first. I'll check that again.

Yes, that's exactly what I meant. It should be

 

if(isset($settings['set_payment']) && $settings['set_payment']=='Y') {

Out of curiousity, is it possible to have just one isset cover all of them so they don't each have to be added individualy? It just seems like redundancy to me. But what do I know for a new guy. Understood if it is possible, but would require recoding much of the file.

Well..it depends on what the intentions of your code are.. for example, in the 2nd block of code I posted, with those custom error messages.. if all of the conditions were the same, only difference was the error message, you could refactor that code by putting the error messages in an associative array, where the indexes match the $_POST index. Then you could just loop through the array with a foreach loop and have a single condition inside the loop, and the index would be popped with the current $key in the loop.

 

Alternatively, you could make an array of all your expected stuff and set default values for them. But you'd have to refactor your code to work with the default values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.