Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi guys I have a problem that I need some assistance with my database results is not displaying results properly for different users, leaving blank tuples in a row.

Code is shown here (Ill explain a segment of code if anyone needs explaination they do not understand)

Lets say I have already assigned my variables using post.

 

<?php
session_start();
include('db.php');
$UserID = $_SESSION['UserID'];
$Firstname = $_POST['Firstname'];
$GroupID = $_POST['GroupID'];
$Surname = $_POST['Surname'];

if ($UserID) {
   $query = "SELECT a.UserID, a.GroupID, b.Firstname, b.Surname FROM table a, table b WHERE a.UserID = b.UserID
   AND b.UserID LIKE '%$UserID%'";
   $result = mysql_query($query);
   $row = mysql_fetch_array($result);

   while ($row = mysql_fetch_array($result)) {

       $UserID = mysql_real_escape_string($row['UserID']);
       $Firstname = mysql_real_escape_string($row['Firstname']);
       $Surname = mysql_real_escape_string($row['Surname']);
       $GroupID = mysql_real_escape_string($row['GroupID']);
   }
       $query2 = "INSERT INTO table2 (UserID, Forename, GroupID, Surname)
   VALUES('$UserID', '$Forename', '$GroupID', '$Surname')";
       $run = mysql_query($query2) or die(mysql_error());
       include('locate.php');

   mysql_close();
}
?>

 

FYI I have tested out SELECT query on mySQL and it works when the script is executed, the problem is however it does not display the results for all different logged in users.

 

(If a simular post exists please redirect!!)

Link to comment
Share on other sites

While it's probably not the main issue, try erasing the following line right above the while statement, because I do not think it's necessary:

 

$row = mysql_fetch_array($result);

 

I think the main problem is that you're assigning values within the while loop, but not outputting anything within the while loop, so you simply continue to overwrite the values in the variables, and are left with only one set of results (mainly, the last set of results).

 

Actually, the fact of the matter is, I don't see any echo statements whatsoever, so I have no clue when/where you're doing the displaying.

  • Upvote 1
Link to comment
Share on other sites

Apparently that line you told me to delete actually solved it thank you HartleySan.

 

To answer your question on what I am doing I am selecting values from a table and then inserting them into a new table.

I hope this answers your question.

Link to comment
Share on other sites

Oh, okay. Well, if you're only grabbing one row at a time, then my first solution would have worked, like I suggested. Although, if that's the case, you also don't need the while loop. Or I should say, you could use the line you erased in place of the while loop. You definitely don't need both though.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...