Jump to content

MYSQL_ASSOC problem - mysterious phenomenon


roughie

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.