0

输出未通过 $userInfo 中的变量显示。当我使用 print_r($_SESSION) 时,它会出于某种原因在 $userInfo 中显示所有输出。我在最后一页设置了 $_SESSION,我知道它正在工作。当我再次使用 print_r 显示会话信息时,所有变量都显示在 $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> 
4

2 回答 2

1

我注意到的第一件事是您的echo声明超出了您定义的范围$userInfo。第二个是您在循环$userInfo的每次传递中分配。while像这样修复这两个问题:

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

请注意,这基本上是马特试图达到的目的。

于 2012-07-25T20:08:25.060 回答
0

我注意到的第一件事是在回显它之前您没有向 $userInfo 附加任何内容 - 您只是覆盖了变量的值。

试试这个,看看它是否有帮助:

$userInfo = "";
.
.
.
$userInfo .= "<table>...</table>";
.
.
.
<?php echo $userInfo; ?>
于 2012-07-25T20:00:07.797 回答