简体   繁体   中英

Only the first data display from my database but other shows but not on a table

Please kindly look into my code and help me fix the errors in my code. only my first data displays on the table on my view page but the other data displays on the view page not on the table.

my image name and the image itself successfully inserted in my database and my image directory which i name "upload" but the image wont display on my view page.

<?php
include ("config.php");
// Retrieve data from database 

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>address</th>
<th>nationality</th>
<th>accountnumber</th>
<th>accounttype</th>
<th>balance</th>
<th>passport</th>
<th>username</th>
<th>passport</th>
<th>update</th>
<th>delete</th>
</tr>";

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

  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['nationality'] . "</td>";
  echo "<td>" . $row['account'] . "</td>";
  echo "<td>" . $row['accounttype'] . "</td>";
  echo "<td>" . $row['balance'] . "</td>";
  echo "<td><h1><img src=\"upload/\" height=35 width=35  /> $row[id]</h1></td>";  
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['password'] . "</td>";``
 echo "<td><a href=\"update.php?id=" . $row['id'] . "\">update</a></td>";
 echo "<td><a href=\"delete.php?id=" . $row['id'] . "\">delete</a></td>";
echo "</table>";

// close while loop 
}
?>

With a quick view, I see you are closing the table inside the while loop. You should change it for a

</tr>

and remember using the thead and tbody tags. Close tbody and table after ending the loop.

This needs to be outside your while loop.

echo "</table>";

And

<img src=\"upload/\"

Is only pointing to the upload directory, you aren't specifying an actual image. Try something like:

echo "<td><h1><img src=\"upload/$row['image']\" height=35 width=35  /> $row['id']</h1></td>";

Moving the table closing tab out side the while loop will solve the first issue.

Change the code as follows to solve the image problem.

echo '<td><img src="upload/'.$row['your image name'].'" height=35 width=35 ></td>';

Your code should be as follows.

<?php
include ("config.php");
// Retrieve data from database 

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>address</th>
<th>nationality</th>
<th>accountnumber</th>
<th>accounttype</th>
<th>balance</th>
<th>passport</th>
<th>username</th>
<th>passport</th>
<th>update</th>
<th>delete</th>
</tr>";

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

  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['nationality'] . "</td>";
  echo "<td>" . $row['account'] . "</td>";
  echo "<td>" . $row['accounttype'] . "</td>";
  echo "<td>" . $row['balance'] . "</td>";
  echo '<td><img src="upload/'.$row['your image name'].'" height=35 width=35 ></td>';  
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['password'] . "</td>";``
  echo "<td><a href=\"update.php?id=" . $row['id'] . "\">update</a></td>";
  echo "<td><a href=\"delete.php?id=" . $row['id'] . "\">delete</a></td>";
 echo "</tr>";

// close while loop 
}
 echo "</table>";
 ?>

i am having a little difficulty in displaying my id info on my update page. i have two entries 6 and 7 if i click update on id 6 , it will show on my update page but if i click update on id 7, on the address bar, it will indicate that the id =7 but it will still show id 6 infos.

here is my display code that display data on my update page

 <?ph
 include ('config.php');  
 // Retrieve data from database 
 $sql="SELECT * FROM $tbl_name ";
 $result=mysql_query($sql);
 $row=mysql_fetch_array($result);
 ?>

secondly if i change data my update page, the change wont reflect on my view page.

<?php
include('config.php');
//This is the directory where images will be saved 
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$nationality=$_POST['nationality'];
$accountnumber=$_POST['account'];
$accounttype=$_POST['accounttype'];
$balance=$_POST['balance'];
$username=$_POST['username'];
$password=$_POST['password'];
$id=$_POST['id'];
// update data in mysql database 
$sql="UPDATE $tbl_name SET firstname='$firstname', lastname='$lastname', address='$address', nationality='$nationality',accountnumber='$account',accounttype='$accounttype',balance='$balance',username='$username',password='$password'
WHERE id='$id'";
$result=mysql_query($sql);

header ("Location: details.php");

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM