Ryan Lackey
Ryan Lackey

Reputation: 7

Select query not showing with $_SESSION variable

The output isn't showing through the variables in the $userInfo. When I use print_r($_SESSION) it shows all the output in $userInfo for some reason. I set the $_SESSION on the last page and I know it's working. Again when I user print_r to show the session info all the variables show up in $userInfo.

    <?php
    session_start();
    error_reporting (E_ALL ^ E_NOTICE);
    include "convenienttomysql.php";
    //include "convenientglobal2localhost.php";

    if (isset($_SESSION['userId'])) {
      $pid = $_SESSION['userId'];
       $results = mysql_query("SELECT * FROM register WHERE userId='$pid'")or die(mysql_error());

    while($rowp=mysql_fetch_array($results)){

     $address1=$rowp['address1'];
     $address2=ucfirst($rowp['address2']);
     $city=ucfirst($rowp['city']);
     $region=$rowp['region'];
     $postalCode=$rowp['postalCode'];
     $country=$rowp['country'];
     $shippingRegion=$rowp['shippingRegion'];
     $userInfo='<table id="addressTableOutput">
         <tr>
            <td colspan="2"id="tableTh"> Address Info</td>
         </tr>
        <tr>
            <td>Address 1:</td><td> '.$address1.'</td>
        </tr>
        <tr>
        <td>Address 2:</td><td> '.$address2.'</td>
        </tr>
        <tr>
                <td>City:</td><td> '.$city.'</td>
          </tr>
          <tr>
           <td> Region:</td><td> '.$region.'</td>
          </tr>
          <tr>
           <td>Postal Code:</td><td> '.$postalCode.'</td>
            </tr>
            <tr>
           <td>Country:</td><td>'.$country.'</td>
            </tr><tr>
           <td>Shipping Region:</td><td> '.$shippingRegion.'</td>
          </tr>
            <tr>
               <td id="editTd"><a id="edit" href="convenienteditaddress.php">Edit</a></td>
            </tr>
        </table>';
 }
    }
    $errors= array();

     if (isset($_POST['submit'])){

        $address1=$_POST['address1'];
         $address2 = $_POST['address2'];
        $city = $_POST['city'];
        $region=$_POST['region'];
        $postalCode=$_POST['postalCode'];
        $country=$_POST['country'];
        $shippingRegion=$_POST['shippingRegion'];
        $msg_to_user="";

        if(empty($address1) || empty($city)|| empty($region)|| empty($country)|| empty($shippingRegion)|| empty($postalCode)){

      $errors[] = "<span id='asterisk'>*</span>All fields need to be filled in.<span id='asterisk'>*</span>";
        }
        else{
        if(strlen($postalCode) > 5){
        $errors[] ="<span id='asterisk'>*</span>Postal Code length is 5 characters.<span id='asterisk'>*</span>";
        }
        if(empty($region)){
        $errors[]="<span id='asterisk'>*</span>Select a Region.<span id='asterisk'>*</span>";
        }
        if(empty($shippingRegion)){
        $errors[]="<span id='asterisk'>*</span>Select a shipping region.<span id='asterisk'>*</span>";
        }
        if(strlen($country)< 2){
        $errors[]="<span id='asterisk'>*</span>Please enter a country.<span id='asterisk'>*</span>";
        }
        }

        if(!empty($errors)){
        foreach($errors as $error){
            $msg_to_user2= "$error";
        }
        }
        else{

    $check = mysql_query("UPDATE register SET address1='$address1',address2='$address2',city='$city',region='$region',postalCode='$postalCode',country='$country',shippingRegion='$shippingRegion' WHERE userId='$pid'")or die(mysql_error());
       }
     }


     ?>

    <html>
    <body>
    <?php echo $userInfo;?>
    </body>
    </html> 

Upvotes: 0

Views: 149

Answers (2)

Palladium
Palladium

Reputation: 3763

The first thing I noticed is that your echo statement is outside the scope in which you defined $userInfo. The second is that you're assigning $userInfo on each pass of the while loop. Fix these two problems like this:

//beginning of code; session_start, error_reporting, include;
$userInfo = '';
//in the while loop:
$userInfo .= //... whatever it is

Note this is essentially what Matt was trying to get at.

Upvotes: 1

Matt
Matt

Reputation: 7040

The first thing I noticed is that you're not appending anything to $userInfo before echoing it - you're just overwriting the value of the variable.

Try this and see if it helps:

$userInfo = "";
.
.
.
$userInfo .= "<table>...</table>";
.
.
.
<?php echo $userInfo; ?>

Upvotes: 0

Related Questions