roughie Posted February 13, 2008 Share Posted February 13, 2008 My php paradigm is to pull data from a Mysql table, replace an HTML form template with these values, then display the results. I cannot get a particular input field's value to display if I use mysql_fetch_assoc(). Were I to hardcode an arbitrary value, it "takes", but arriving at the value from MYSQL_ASSOC renders the input field blank. It's mysterious. Or, is it the str_replace that's doing it? Kudos to this riddle's solver. Here are the main 2 files. Maybe you'll see something I don't. File indexTST.php <?php include_once "config.php"; if (!isset($_POST['sessDate']) ) { $s1 = date("m/d"); $result = mysql_query("SELECT * from Prospect"); $myinfo = mysql_fetch_assoc($result); if (is_null($myinfo["alias"])) { $p1 = $myinfo["realname"]; } else { // echo $myinfo["alias"]; $p1 = $myinfo['alias']; } // $p1 = "xyz"; } $tmp = @file_get_contents("tst.tpl"); $tmp = str_replace( array("%%s1%%", "%%p1%%"), array($s1, $p1), $tmp); echo $tmp; ?> File tst.tpl <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>tst</title> <link rel="stylesheet" href="hpp.css" type="text/css" /> <script language="JavaScript" src="tst.js" type="text/javascript"></script> </head> <body> <form name="form1"> <div id="session"> <fieldset> <legend>Session</legend> <label>Date</label> <input name="sessDate" type="text" value="%%s1%%" size="5" /> </fieldset> </div> <div id="prospect"> <fieldset> <legend>Prospect</legend> <label>Name</label> <input name="pname" type="text" value="%%p1%%" size="25" /> </fieldset> </div> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/ Share on other sites More sharing options...
Bauer418 Posted February 13, 2008 Share Posted February 13, 2008 First of all, formatting your code and using code tags makes it much easier to read...anyway...what does the script output when you use this code? <?php include_once "config.php"; if (!isset($_POST['sessDate'])) { $s1 = date("m/d"); $result = mysql_query("SELECT * from Prospect"); $myinfo = mysql_fetch_assoc($result); print_r($myinfo); exit; if (is_null($myinfo["alias"])) { $p1 = $myinfo["realname"]; } else { $p1 = $myinfo['alias']; } // $p1 = "xyz"; } $tmp = @file_get_contents("tst.tpl"); $tmp = str_replace( array("%%s1%%", "%%p1%%"), array($s1, $p1), $tmp); echo $tmp; ?> Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/#findComment-465958 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 Also, if you do a View Source on the page, what does the generated content look like? You may want to run $s1 and $p1 through htmlspecialchars() to convert any specials characters. Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/#findComment-465961 Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 I see a few problems. First, this isn't your problem, but you specified an XHTML doctype, and the following is invalid XHTML: <script language="JavaScript" src="tst.js" type="text/javascript"> Take out the 'language="JavasScritp"' as that is now depracated. Next is the same type of thing: <form name="form1"> the 'name' attribute has been deprecated in XHTML. Your form tag is also where your problem is. Your form tag should look like this: <form action="indexTST.php" method="post"> (don't add the 'name') Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/#findComment-465962 Share on other sites More sharing options...
roughie Posted February 14, 2008 Author Share Posted February 14, 2008 Bower418 - you are right; And you provided a beautiful model to boot. Thanks Haku! I made the two corrections you pointed out. But those changes did not change the result. Then I took rhodesa's advice and bingo! My mistake became immediately evident. My table's data came wrapped with a set of quotes; The value attribute of that input field contained, therefore, two consecutive quotes, one generated by the server, and one I had there in the 1st place. It's was my import of quoted data into the database that messed me up. Rhodesa, you'd make a good detective. Mystery solved - thank you people. Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/#findComment-466785 Share on other sites More sharing options...
roughie Posted February 14, 2008 Author Share Posted February 14, 2008 By the way, might someone know how I can globally change values across all columns of a Mysql table, to get rid of the quotes in each column? Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/#findComment-466791 Share on other sites More sharing options...
rhodesa Posted February 14, 2008 Share Posted February 14, 2008 first: BACKUP your data before you do a global change! Then, you should be able to use TRIM with will remove any leading/trailing double quotes: UPDATE `tableName` SET `fieldName` = TRIM(BOTH '"' FROM `fieldName`) Again, I can't emphasize enough...backup your data...just in case Link to comment https://forums.phpfreaks.com/topic/90917-mysql_assoc-problem-mysterious-phenomenon/#findComment-466837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.