heshan Posted October 10, 2012 Share Posted October 10, 2012 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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 10, 2012 Share Posted October 10, 2012 (edited) 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 October 10, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
nodirtyrockstar Posted October 10, 2012 Share Posted October 10, 2012 It sounds to me like you want to return the student's full name and reduce the string of their first name so that it displays only their first initial. If that is the case, a simple call to substr() will allow you to truncate the string as you wish. Check it out here: http://www.php.net/manual/en/function.substr.php Quote Link to comment Share on other sites More sharing options...
heshan Posted October 11, 2012 Author Share Posted October 11, 2012 (edited) @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 October 11, 2012 by heshan Quote Link to comment Share on other sites More sharing options...
Barand Posted October 11, 2012 Share Posted October 11, 2012 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. Quote Link to comment Share on other sites More sharing options...
heshan Posted October 11, 2012 Author Share Posted October 11, 2012 Thanks guys..Yeah i missed '$name' instead of '$name_with_initials' in two locations.Now i have corrected it and works fine..:-) @psycho; Thanks for your recommendations also..Highly appreciated them.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.