Help with displaying the name "O'Brien" from a database to an input box

Discussion in 'PHP' started by cholland, Mar 2, 2010.

  1. #1
    Hi folks,

    I'm using php and a mysql database. I've a page where users can edit a name. When i select to edit the name "O'Brien" the only thing that appears in the input box is "O" before i start editing it. When these names are added i have the code below featured before it writes it to the database:

    
    
      $SurName=mysql_real_escape_string($_POST['inputSurName']);
    
    
    Code (markup):
    I thought that would have fixed it, as it's writing "O'Brien" to the database but it's not. Below is the code from the form that displays the name in the text box:

    
    
    $query = "SELECT * FROM tblpupil WHERE PupilID = $id";
    	
    	
    	$result = mysql_query($query);
    	
    	
    	
    	while($row = mysql_fetch_array($result)) {
    		echo "<form method=post action=editname2.php?id=" . $row['PupilID'] . ">";
    		echo "<table border=0 cellSpacing=2 cellPadding=1 width=90%>";
    		echo "<tr><td colspan=4><FONT align=left color=#808080 size=5>Edit Pupil Name</Font></td></tr>";
    		
    		echo "<TR><TD></td></tr><TR><TD></td></tr><TR><TD></td></tr>";
    		echo "<tr><td width=50%>First Name</td>";
    		echo "<td><input type=text name=editFirstName size=20 value='" . $row['FirstName'] . "'></td></tr>";
    		echo "<tr><td>Surname</td>";
    		echo "<td><input type=text name=editSurName size=20 value='" . $row['SurName'] . "'></td></tr>";
    		echo "<tr><td colspan=2 align=center><input type=submit name=submit value=Save>  <input type=submit name=cancel value=Cancel></td></tr></table>";
    		
    	}
    
    Code (markup):

    Here the surname is for the pupil Stephen O'Brien is displaying just "O". Does anybody know how to fix this? Any help would be greatly appreciated.

    Thanks guys!
     
    cholland, Mar 2, 2010 IP
  2. redlightshooter

    redlightshooter Greenhorn

    Messages:
    94
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #2
    its because the code line :
    echo "<td><input type=text name=editSurName size=20 value='" . $row['SurName'] . "'></td></tr>";
    Code (markup):
    will produce html code :
    <td><input type=text name=editSurName size=20 value='O' Brien'></td></tr>
    Code (markup):
    and the correct should :
     <td><input type=text name=editSurName size=20 value="O' Brien"></td></tr>
    Code (markup):
    try this :


    $query = "SELECT * FROM tblpupil WHERE PupilID = $id";
    	
    	
    	$result = mysql_query($query);
    	
    	
    	
    	while($row = mysql_fetch_array($result)) {
    		echo "<form method=post action=editname2.php?id=" . $row['PupilID'] . ">";
    		echo "<table border=0 cellSpacing=2 cellPadding=1 width=90%>";
    		echo "<tr><td colspan=4><FONT align=left color=#808080 size=5>Edit Pupil Name</Font></td></tr>";
    		
    		echo "<TR><TD></td></tr><TR><TD></td></tr><TR><TD></td></tr>";
    		echo "<tr><td width=50%>First Name</td>";
    		echo "<td><input type=text name=editFirstName size=20 value='" . $row['FirstName'] . "'></td></tr>";
    		echo "<tr><td>Surname</td>";
    		[B]echo "<td><input type=text name=editSurName size=20 value=\"" . $row['SurName'] . "\"></td></tr>";[/B]
    		echo "<tr><td colspan=2 align=center><input type=submit name=submit value=Save>  <input type=submit name=cancel value=Cancel></td></tr></table>";
    		
    	}
    
    Code (markup):
     
    redlightshooter, Mar 2, 2010 IP
  3. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    echo "<td><input type=text name=editSurName size=20 value=\"$row[SurName] \"></td></tr>";

    In my case i do like this check it for your need and let me know...
     
    Om ji Kesharwani, Mar 2, 2010 IP
  4. halfdata

    halfdata Active Member

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    I think it is better to use htmlspecialchars() to display anything in any form control elements. In this case all HTML-specific (', ", <, >, etc) symbols will be displayed correctly.
    So, try this one:
    echo "<td><input type=text name=editSurName size=20 value='" . htmlspecialchars($row['SurName'], ENT_QUOTES) . "'></td></tr>";
     
    halfdata, Mar 2, 2010 IP
  5. cholland

    cholland Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Worked like a charm thank you!
     
    cholland, Mar 3, 2010 IP
  6. sunlcik

    sunlcik Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is better than others
     
    sunlcik, Mar 4, 2010 IP