Jump to content

Retrieving Data Partially From The Database


heshan

Recommended Posts

Hi all,

 

I want to view the records of a particular student. (Admission Number, Name With Initials and Marks) These data display after filling a user form..I have one issue. That is the Name With Initials of the student is displayed partially..(only one letter)

 

Ex : If the name is 'Keith' then only 'k' is displayed..What will be the issue?? Here is my coding..

 

<form id="form1" name="form1" method="post" action="">
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("student_management",$con);
$grade=$_GET["grade"];
$class=$_GET["class"];
$stream=$_GET["stream"];
$subject=$_GET["subject"];
$term=$_GET["term"];
$year=$_GET["year"];
?>
<table>-------
  <tr>
    <td width="17"> </td>
    <td width="17"> </td>
    <td>Subject </td>
    <td colspan="2"><?php
  $query="select name  from subject
  where subject_id='$subject'";

 $result=mysql_query($query);
 while($row=mysql_fetch_array($result)){
 $name=$row['name'];
 }
 echo $name;  
 ?>
	  </td>
    <td colspan="2"> </td>
    <td width="184"> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td width="94"><label>Term</label> </td>
    <td colspan="2"><?php
  echo $term;  
 ?></td>
    <td colspan="2"> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td><label>Grade</label> </td>
    <td width="74"><?php
  echo $grade;  
 ?></td>
    <td width="82"><label><label>Class</label>
	  </label></td>
    <td width="78"><?php 
 echo $class;
 ?></td>
    <td width="67"><label>Stream</label> </td>
    <td><?php  

  echo $stream;

 ?></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td colspan="2"> </td>
    <td colspan="2"> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
<?php
if($stream!=null){

$query="SELECT class_id FROM class
WHERE class.grade_id='$grade' AND class.class_name='$class' AND class.stream='$stream'";
}else{

$query="SELECT class_id FROM class
WHERE class.grade_id='$grade' AND class.class_name='$class'";
}
$result=mysql_query($query) or die (mysql_error());
while($row=mysql_fetch_array($result)){
$class_id=$row['class_id']; }

$query="SELECT exam_id FROM exam
    WHERE exam_name='$term' AND class_id='$class_id' AND year='$year'";
$result=mysql_query($query) or die (mysql_error());
while($row=mysql_fetch_array($result)){
$exam_id=$row['exam_id']; }

$query="SELECT admission_no,mark FROM mark
    WHERE subject_id='$subject' AND exam_id='$exam_id'";
$result=mysql_query($query) or die (mysql_error());
$i=0;
while($row=mysql_fetch_array($result)){
$i++;
$admission_no[$i]=$row['admission_no'];
$mark[$i]=$row['mark'];}

for($j=1;$j<=$i;$j++){

$query1="SELECT name_with_initial FROM student_info WHERE admission_no='".$admission_no[$j]."'";
$result1=mysql_query($query1) or die (mysql_error());
while($row1=mysql_fetch_array($result1)){

$name[$j]=$row1['name_with_initial'];
}

}

?>
    <td colspan="6"><table width="444" border="1">
	  <tr>
	    <td width="98" align="center"><label>Admission No</label>
		   </td>
	    <td width="205" align="center"><label>Name</label>
		   </td>
	    <td width="119" align="center"><label>Mark</label> </td>
	  </tr>
	  <?php

   for($y=1;$y<=$i;$y++)
   {

   echo "<tr>";
   echo"<td>".$admission_no[$y]."</td>";
   echo "<td>".$name[$y]."</td>";
   echo "<td>".$mark[$y]."</td>";
   echo "</tr>";
   }
  ?>



    </table>
</form>

Link to comment
Share on other sites

Have you checked what is stored in the Database? If the value in the DB is only one letter then the problem is in the saving of the data - not the display. Could be the PHP code it truncating the value or, perhaps, the DB field was set up with a length of 1. In the latter case the value will be automatically truncated when you save a record.

 

In the code above I see the following:

while($row1=mysql_fetch_array($result1))
{
$name[$j]=$row1['name_with_initial'];
}

I don't know why you would name a field as "name_with_initial", but anyway, that loop makes no sense. If you have a while() loop I would assume the query could be returning multiple records [Note: if a query would only return one value then don't use a while() loop to extract the record]. But, that loop will only overwrite the previous value on each iteration since the value of $j is not changing. So, $name[$j] will only contain the last value when the loop completes. But, I don't even see that value used anywhere in the code that follows. So, where is the output - exactly - that is causing the problem?

 

Note: There are a lot of other problems with that code (error handling, SQL Injection exploitable, etc.), but too much to go into here.

 

EDIT: I also see you are running a query within a loop - you need to learn how to do JOINs in your queries.

Edited by Psycho
Link to comment
Share on other sites

@nodirtyrockstar; No i didn't want that kind of thing...In my database table there is a seperate field called "name_with_initial". For an understanding perspective let's

say it is a full name.. I want that name to be displayed as a result.

 

But the problem here is only first string of that letter is displayed instead of retrieving full string of letters.

 

Ex : Let's say full name is "kevin Barber". But it displayed in the name column field as "k" only..

Edited by heshan
Link to comment
Share on other sites

This exhibiting the symptoms of missing quotes somewhere along the line where the name has been placed as a value in an input field without quotes.

 

Example code:

<?php
   if (isset($_POST['name'])) {
    echo "Name : {$_POST['name']}";
   }


   $name = "K Barber";					  // hard coded name for field value
?>

<form method="post">
   <input type='hidden' name='name' value=<?=$name?> />	   
   <!- ^^ note no quotes around value ie value=K Barber -->

   <input type='submit' value='Test' />
</form>

 

When the name value is printed it shows as "Name: K" as without the quotes the value of the field is the string up to the first space character.

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.