Jump to content

Dispalying Data in the Correct Area


ashrafzia

Recommended Posts

I am inserting data to mysql and then at the same time retrieving it.
While retrieving i cannot put it to the place where i want.
I want to dispaly the data below the header.
I have a registration form(which is in php) below the header (which is in html) when user submits the registration form at the same time and the same area i want to retrieve all records from the table.I have this code:
[code]if (isset($_POST['submit_registration_x'])){
include "connection.php";
$table_name = "registration";
$sql = "INSERT INTO $table_name (id, name, father_name, gender, address)
VALUES ('$_POST[id]', '$_POST[name]', '$_POST[father_name]', '$_POST[gender]', '$_POST[address]')";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$sql = "SELECT * from $table_name";
$result = mysql_query($sql, $connection) or die (mysql_error());
echo "<form method='post' action='student.php' name='student_records'>
  <table border='1' cellspacing='4' cellpadding='2' align='center'>
  <tr><th>ID</th><th>Name</th><th>Father's Name</th><th>Gender</th><th>Address</th></tr>";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$name = $row['name'];
$father_name = $row['father_name'];
$gender = $row['gender'];
$address = $row['address'];
echo"<tr><td>$id</td><td>$name</td><td>$father_name</td><td>$gender</td><td>$address</td></tr>";
}
echo "</table></form>";
}[/code]
Link to comment
Share on other sites

[quote author=steveclondon link=topic=109670.msg442257#msg442257 date=1159345952]
why not just retrieve the data then below the header and display it there?
[/quote]
Bcoz i am handling all this through one file, and i have echoed multiple php codes in just one html line. If i retrieve it below the header then i have to write the above code below the header, and when the page loads up the data will be present, which i dont want.
[quote author=steveclondon link=topic=109670.msg442257#msg442257 date=1159345952]
Don't really understand what you are trying to get at here?[/quote]
Dont get confused. I am simply inserting data from a form, then at the same time retrieving it. But the problem is when i retrieve the data it is dispalyed above the html header. I dont want it to be displayed there, and why? i explained above.
[code]
echo "<form method='post' action='student.php' name='student_records'>
  <table border='1' cellspacing='4' cellpadding='2' align='center'>
  <tr><th>ID</th><th>Name</th><th>Father's Name</th><th>Gender</th><th>Address</th></tr>";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$name = $row['name'];
$father_name = $row['father_name'];
$gender = $row['gender'];
$address = $row['address'];
echo"<tr><td>$id</td><td>$name</td><td>$father_name</td><td>$gender</td><td>$address</td></tr>";
}
echo "</table></form>";
[/code]
Here is actually a problem. I am here echoing the data in the table format. when i put this in a variable let's say :
[code]
$std_records = "<tr><td>$id</td><td>$name</td><td>$father_name</td><td>$gender</td><td>$address</td></tr>";
[/code]
and then echo this variable $std_records where i want it to be displayed in my html. It only displays the last record from the table,not the whole records.
Hope you get.
Link to comment
Share on other sites

can't really say that I do get you. Can't see the problem perhaps its just me. If you want to use the data more than once you could put it in an array

$i=0;
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$name = $row['name'];
$father_name = $row['father_name'];
$gender = $row['gender'];
$address = $row['address'];
$tableRow[$i]="<tr><td>$id</td><td>$name</td><td>$father_name</td><td>$gender</td><td>$address</td></tr>";
        $i++;
}

then you could use this to uput again where you want. From what you have above I still don't see why you would need to to this. Sorry.
Link to comment
Share on other sites

Here's the full coding from A-Z try to undrstand it:
[code]
<?php

if (isset($_POST['students_x'])){
$students = "<form method='post' action='student.php' name='students_form'>
<table border='0'>
<tr><td><input type='image' src='images/addstudent.gif' name='addstudent'></td></tr>
<tr><td><input type='image' src='images/editstudent.gif' name='editstudent'></td></tr>
<tr><td><input type='image' src='images/deletestudent.gif' name=deletestudent'></td></tr>
<tr><td><input type='image' src='images/viewallstudents.gif' name='viewallstudent'></td></tr>
</table>
</form>";
}

if (isset($_POST['addstudent_x'])) {
$registration_form = "<form method='post' action='student.php' name='registration_form'>
<table border='0' align='center' cellspacing='8' cellpadding='2'>
<tr><td>ID :</td><td><input type='text' name='id' size='1'></td></tr>
<tr><td>Name :</td><td><input type='text' name='name'></td></tr>
<tr><td>Father's Name :</td><td><input type='text' name='father_name'></td></tr>
<tr><td>Gender :</td><td>Male<input type='radio' value='Male' name='gender'>
    Female<input type='radio' value='Female' name='gender'></td>
</tr>
<tr><td>Address :</td><td><input type='text' name='address' size='50'></td></tr>
<tr><td colspan='2' align='center'><input type='image' src='images/submit_registration.gif' name='submit_registration'></td></tr>
</table>
</form>";
}

if (isset($_POST['submit_registration_x'])){
include "connection.php";
$table_name = "registration";
$sql = "INSERT INTO $table_name (id, name, father_name, gender, address)
VALUES ('$_POST[id]', '$_POST[name]', '$_POST[father_name]', '$_POST[gender]', '$_POST[address]')";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$sql = "SELECT * from $table_name";
$result = mysql_query($sql, $connection) or die (mysql_error());
echo "<form method='post' action='student.php' name='student_records'>
  <table border='1' cellspacing='4' cellpadding='2' align='center'>
  <tr><th>ID</th><th>Name</th><th>Father's Name</th><th>Gender</th><th>Address</th></tr>";
$i=0;
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$name = $row['name'];
$father_name = $row['father_name'];
$gender = $row['gender'];
$address = $row['address'];
$student_records[$i] = "<tr><td>$id</td><td>$name</td><td>$father_name</td><td>$gender</td><td>$address</td></tr>";
$i++;
}
echo "</table></form>";
}
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Examination System</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
<body>
<form method="post" action="student.php">
  <table width="761" border="0">
    <tr>
      <td colspan="4"><img src="images/header.jpg" width="755" height="133"></td>
    </tr>
    <tr>
      <td width="169"><input type="image" src="images/students.gif" width="169" height="32" name="students"></td>
    </tr>
  </table>
  <p><?php echo "$registration_form"; ?><?php echo "$student_records[$i]"; ?></p>
  <p><?php echo "$students"; ?></p>

</form>
</body>
</html>
[/code]
I have also applied your method but no result
Link to comment
Share on other sites

right now i see what you are doing.

$rows=count($student_records);
$i=0;
while ($i<$rows)
{
$student_records[$i];
$i++
}

however instead of this why don't you just put your bit of code below where you currently are trying to echo the student records. You do not have to pull details from the database at the top of the page. I assumed you must have wanted it there for some reason and then was going to use it again.

while($row = mysql_fetch_array($result)){
$id = $row['id'];
$name = $row['name'];
$father_name = $row['father_name'];
$gender = $row['gender'];
$address = $row['address'];
echo "<tr><td>$id</td><td>$name</td><td>$father_name</td><td>$gender</td><td>$address</td></tr>";
}
echo "</table></form>";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.