2016-03-26 21 views
1

aşağıdaki sütunları olan user_bio adında bir tabloya sahip:değişken iki değeri verilmesi

id; 1 
    age: 30 
    studying: test 
    relationship_status: single 
    username: conor 
    about_me: test 
:

id 
    age 
    studying 
    relationship_status 
    username 
    about_me 

Şu anda, bu tablo kullanıcıya conor için bir satır vardır

Conor olarak oturum açıp bio.php/conor adresine giderseniz, conor biyografisi düzgün görüntülenecektir. Ancak, Alice olarak oturum açıp bio.php/alice'a gidersem, doğru dışında (her kullanıcı için doğru değerle) yaş dışında tanımlanmamış değişken hatalar alıyorum. İşte

kodum ve ben verilerinin görüntülenmesi am nasıl:

<?php 
/****************************************/ 
// Getting data to display on bio page for user 
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$user'"); 
$row_returned = mysqli_num_rows($get_bio); 
if ($row_returned == 1){ 
    while ($get_bio_data = mysqli_fetch_assoc($get_bio)){ 
     $about_me = $get_bio_data['about_me']; 
     $age = $get_bio_data['age']; 
     $studying = $get_bio_data['studying']; 
     $lang = $get_bio_data['language']; 
     $rel_status = $get_bio_data['relationship_status']; 
     /*****************************************/ 
     // change data held by vars 
     // if no bio for the user exists in the db (no rows in db)s 
     if ($row_returned == 0){ 
      if ($about_me == ""){ 
       $about_me = "None set.."; 
      } else { 
       $about_me = $get_bio_data['about_me']; 
      } // else closed 
     } // if closed 
    } // while closed 
} // if closed 
/*********************************************/ 
?> 
<div id="userposts_panel"> 
    <div class="bio_here"> 
     <p> <b> About me: </b> <?php echo $about_me; ?> </p> 
     <p> <b> Age: </b> <?php echo $age; ?> </p> 
     <p> <b> Studying: </b> <?php echo $studying; ?> </p> 
     <p> <b> Language(s): </b> <?php echo $lang; ?> </p> 
     <p> <b> Relationship Status: </b> <?php echo $rel_status; ?> </p> 
    </div> 
</div> 

username Alice tekabül veritabanında hiçbir satır olduğu için sanırım, bu hataya neden oluyor. Bu durumda, seçili dizgimi yeniden tanımlamak istiyorum, örneğin, kullanıcı için about_me varsa, sonra görüntüle, $ about_me eşittir none set. Ama hala tanımlanmamış hatalar alıyorum?

cevap

0

. Bu nedenle, bundan önce değişkenleri ($ about_me, $ age, ...) başlatmanız gerekir.

bu kodu deneyin: sen de cahil olarak & kullanmak ve denetlemek için bir işlev kullanabilirsiniz

<?php 
/****************************************/ 

// change data held by vars if no bio for the user exists in the db (no rows in db)s 
$about_me = "None set.."; 
$age = "None set.."; 
$studying = "None set.."; 
$lang = "None set.."; 
$rel_status = "None set.."; 

// Getting data to display on bio page for user 
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$user'"); 
$row_returned = mysqli_num_rows($get_bio); 
if ($row_returned == 1){ 
    while ($get_bio_data = mysqli_fetch_assoc($get_bio)){ 
     $about_me = $get_bio_data['about_me']; 
     $age = $get_bio_data['age']; 
     $studying = $get_bio_data['studying']; 
     $lang = $get_bio_data['language']; 
     $rel_status = $get_bio_data['relationship_status']; 
     /*****************************************/ 
    } // while closed 
} // if closed 
/*********************************************/ 
?> 
<div id="userposts_panel"> 
    <div class="bio_here"> 
     <p> <b> About me: </b> <?php echo $about_me; ?> </p> 
     <p> <b> Age: </b> <?php echo $age; ?> </p> 
     <p> <b> Studying: </b> <?php echo $studying; ?> </p> 
     <p> <b> Language(s): </b> <?php echo $lang; ?> </p> 
     <p> <b> Relationship Status: </b> <?php echo $rel_status; ?> </p> 
    </div> 
</div> 
0

Sen değişkeni ayarlanmamış ya da aşağıdaki olup olmadığını kontrol etmek isset() yöntemi kullanabilirsiniz:

<div id="userposts_panel"> 
        <div class="bio_here"> 
          <p> <b> About me: </b> <?php if(isset($about_me)){ echo $about_me;} else{ echo "none";} ?> </p> 
          <p> <b> Age: </b> <?php if(isset($age)){ echo $age;} else{echo "none";} ?> </p> 
          <p> <b> Studying: </b> <?php if(isset($studying)){ echo $studying;} else{ echo "none";} ?> </p> 
          <p> <b> Language(s): </b> <?php if(isset($lang)){ echo $lang;} else { echo "none";} ?> </p> 
          <p> <b> Relationship Status: </b> <?php if(isset($rel_stautus)){ echo $rel_status;} else{ echo "none";} ?> </p> 
        </div> 
0

bu deneyin: Kullanıcı Alice varsa sadece if ($row_returned == 1){ girmeyeceğini

<?php 
/****************************************/ 
// Getting data to display on bio page for user 
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$user'"); 
$row_returned = mysqli_num_rows($get_bio); 
if ($row_returned == 1){ 
    while ($get_bio_data = mysqli_fetch_assoc($get_bio)){ 
     $about_me = $get_bio_data['about_me']; 
     $age = $get_bio_data['age']; 
     $studying = $get_bio_data['studying']; 
     $lang = $get_bio_data['language']; 
     $rel_status = $get_bio_data['relationship_status']; 
    } // while closed 
} // if closed 
else { 
    $about_me = "Not set..."; 
    $age = "Not set..."; 
    $studying = "Not set..."; 
    $lang = "Not set..."; 
    $rel_status = "Not set..."; 
} // else closed 
/*********************************************/ 
?> 
<div id="userposts_panel"> 
    <div class="bio_here"> 
     <p> <b> About me: </b> <?php echo $about_me; ?> </p> 
     <p> <b> Age: </b> <?php echo $age; ?> </p> 
     <p> <b> Studying: </b> <?php echo $studying; ?> </p> 
     <p> <b> Language(s): </b> <?php echo $lang; ?> </p> 
     <p> <b> Relationship Status: </b> <?php echo $rel_status; ?> </p> 
    </div> 
</div> 
0
<?php 
function checkValue($getValue) { 
    return isset($getValue) ? $getValue : "N/A"; 
} 
?> 

<div id="userposts_panel"> 
    <div class="bio_here"> 
     <p> <b> About me: </b> <?php echo checkValue(&$about_me); ?> </p> 
     <p> <b> Age: </b> <?php echo checkValue(&$age); ?> </p> 
     <p> <b> Studying: </b> <?php echo checkValue(&$studying); ?> </p> 
     <p> <b> Language(s): </b> <?php echo checkValue(&$lang); ?> </p> 
     <p> <b> Relationship Status: </b> <?php echo checkValue(&$rel_status); ?> </p> 
    </div> 
</div> 

, değeri ayarlı ve set değilse bazı gerekli Metin görüntülemek olup olmadığını.