hi all im attempting to update some script and i have a undefined error in a page see coding for error

$column = "select * ";
$sql .= " from favorites where user_id = '".$_SESSION['userid']."' ";

of which the information a want to echo shows up

so when i put the following in

if(!empty($sql)) {
$sql .= " from favorites where user_id = '".$_SESSION['userid']."' ";
}

the information echoed no longer shows up

here is the full code for this

<?php 
include("config/db_connect.php");
include("config/ckh_session.php");

// Add user favorite list
if(isset($_POST['favor_id']))
{
    $check_refer = mysqli_query($conn,"select favor_id , user_id from favorites where favor_id = '".$_POST['favor_id']."' and user_id = '".$_SESSION['userid']."'");

    if(mysqli_num_rows($check_refer)<1)
    {
        $sdate = date('d-m-Y h:i:s A');
        $insert = mysqli_query($conn,"insert into favorites set user_id = '".$_SESSION['userid']."', favor_id = '".$_POST['favor_id']."', sdate = '".$sdate."' ") or die(mysqli_error($conn));
        $last_id = mysqli_insert_id($conn);
    }
}

// for pagination code

$start=0;
if(isset($_GET['start'])) $start=$_GET['start'];
$pagesize=10;
if(isset($_GET['pagesize'])) $pagesize=$_GET['pagesize'];

// Fetch favorite user 

$column = "select * ";
if(!empty($sql)) {
$sql .= " from favorites where user_id = '".$_SESSION['userid']."' ";
}
//$user_refer   = mysqli_query($conn,"select * from favorites where user_id = '".$_SESSION['userid']."' ");

$search_ord = " order by id desc  ";

$sql1 = " select count(*) ".$sql;

$sql .= $search_ord;
$sql .= " limit $start, $pagesize ";
$sql = $column.$sql;
$reccnt = getSingleResult($sql1);
$finalsearch = mysqli_query($conn,$sql);

?>

I'm looking at the full code of 43 lines and on line 28, why would that work? I don't see where $sql was created so it looks to be an emptry string to me. Explain that one.

i do have more code in the html section in a php block which didnt think to include see below

<?php if($reccnt>0){
      while($fetch_refer = mysqli_fetch_array($finalsearch)){ 

      // fetch details form favorite user
      $referer      = mysqli_fetch_array(mysqli_query($conn,"select * from user where user_id = '".$fetch_refer['favor_id']."'"));
      $fetch_img    = mysqli_fetch_array(mysqli_query($conn,"select * from user_images where user_id = '".$referer['user_id']."'"));
      $show_img     = $fetch_img['user_image'];

      $referer_info = mysqli_fetch_array(mysqli_query($conn,"select * from user_info where user_id='".$fetch_refer['favor_id']."'"));
      $user_birthdate = $referer['user_birthdate'];
      $lastlogin    = mysqli_fetch_array(mysqli_query($conn,"select * from user_lastlogin where user_id='".$fetch_refer['favor_id']."'"));
      $onstatus     = $lastlogin['online_status'];
      ?>

then i have this section hope this helps hun

<div class="blank"><a href="viewprofile.php?profid=<?php echo $referer['user_id'];?>"><img src="images/user_images/smallthumb/<?php echo $show_img;?>" height="90px" width="90px"/></a></div>
        <table width="761">
          <tr>
            <td width="234" height="85" class="padding_top20"><div class="inbox_txt fav_marg_wid"> <a href="viewprofile.php?profid=<?php echo $referer['user_id'];?>">
                <?php echo $referer['user_name'];?>
                </a></div>
              <div class="inbox_txt fav_marg_wid">
                <?php echo $lastlogin['lastlogin'];?>
              </div></td>
            <td width="286" class="padding_top20"><div class="inbox_txt fav_marg_wid"><?php echo Age($user_birthdate).", "; echo ucfirst($referer_info['city']);?></div>
              <div class="inbox_txt fav_marg_wid">
                <?php echo $referer_info['lookingfor'];?>
                &nbsp;&nbsp;<a href="send_msg.php?uid=<?php echo $referer['user_id'];?>"><img src="images/mail.png" /></a>&nbsp;&nbsp;&nbsp;
                <?php if($onstatus=='1') { 
 $words = explode(' ', trim($referer['user_name'])); ?>
                <a href="javascript:void(0)" onClick="javascript:chatWith('<?php echo $words[0]?>')"><font color="#009900">Chat</font></a>
                <?php } ?>
              </div></td>
            <td width="225" class="padding_top20"><table>
                <tr>
                  <td height="75px;"><div class="inbox_txt fav_marg_wid"><a href="update_note.php?uid=<?php echo $fetch_refer['id'];?>">Update Note</a></div>
                    <div class="inbox_txt fav_marg_wid"><a href="delete_favorite.php?uid=<?php echo $fetch_refer['id'];?>">Remove</a></div>
                    <div class="inbox_txt fav_marg_wid"><a href="viewallmessages.php?sender_id=<?php echo $referer['user_id']?>">View MSGs</a></div></td>

Still didn't find where $sql is created so that means in your first 43 line code passage at line 28 appears and reads to me that the value/variable/string is non-existant.

Try again. I must have missed where you created $sql and gave it meaning and value.

it came with a script that trying up date so didnt write it myself but ill search another not to worry x

I apologize a little here. I was under the impression that this is your code. Folk seem to be finding scripts and then asking for others to fix them. Something of a pattern developing in many forums.

Anyhow, I do see something wrong with the line I called out. The owner of this code or the person that uses it will have to find where the variable/string is created. It's not in the supplied code unless I missed it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.