Jump to content

[SOLVED] Apply PHP variable to MySQL result with stored variables


ifis

Recommended Posts

I am trying to create a form were the result is a field in a MySQl database, but inside that field I have an number of variables I want to replace with variables I have previously set. I tried str_replace, but it does not seem to be working for 1 variable, and I am going to have a couple.  Any suggestions?  Is this not the easiest way to do it?  Here is the code:

<?php
// Connect to server and select databse.
mysql_connect("*******", "*****", "******")or die("cannot connect"); 
mysql_select_db("*****")or die("cannot select DB");

//get data
$make=$_POST['make'];
$lic = $_POST['lic'];
$student=$_POST['student'];

$sql = "SELECT * FROM Endorsements WHERE lic='$lic'";
$result=mysql_query($sql);

//get the first entry from the result
$row = mysql_fetch_array($result);

echo str_replace("$student", $student,"$row[end]");
?>

The field in the MySQL database is:

I certify that $student has satisfactorily completed the test for the $make.

You could use an array.

 

http://ca.php.net/str_replace

 

 

try

 

<?php
// Connect to server and select databse.
mysql_connect("*******", "*****", "******")or die("cannot connect"); 
mysql_select_db("*****")or die("cannot select DB");

//get data
$make=$_POST['make'];
$lic = $_POST['lic'];
$student=$_POST['student'];

$sql = "SELECT * FROM Endorsements WHERE lic='$lic'";
$result=mysql_query($sql) or die(mysql_error());

//get the first entry from the result
$row = mysql_fetch_array($result);

$replace = array("$student","$make");
$with = array($studen,$make);

echo str_replace($replace, $with, $row['end']);
?>

I updated the code to:

//get the first entry from the result
$row = mysql_fetch_array($result);

$find = array("$student","$make");
$replace = array($student,$make);

echo str_replace($find,$replace,$row['end']);

but it still does not replace the variables.  The result is still:

I certify that $student has satisfactorily completed the test for the $make.

any more suggestions?

 

I updated the code to:

//get the first entry from the result
$row = mysql_fetch_array($result);

$find = array("$student","$make");
$replace = array($student,$make);

echo str_replace($find,$replace,$row['end']);

but it still does not replace the variables.  The result is still:

I certify that $student has satisfactorily completed the test for the $make.

any more suggestions?

 

 

 

I forgot to add slashes

 

try

 

<?php
// Connect to server and select databse.
mysql_connect("*******", "*****", "******")or die("cannot connect"); 
mysql_select_db("*****")or die("cannot select DB");

//get data
$make=$_POST['make'];
$lic = $_POST['lic'];
$student=$_POST['student'];

$sql = "SELECT * FROM Endorsements WHERE lic='$lic'";
$result=mysql_query($sql) or die(mysql_error());

//get the first entry from the result
$row = mysql_fetch_array($result);

$replace = array("\$student","\$make");
$with = array($student,$make);

echo str_replace($replace, $with, $row['end']);
?>

I suggest you change $student in your database to *student* to make things easier in the future.

Then I think your code should be:

<?php
// Connect to server and select databse.
mysql_connect("*******", "*****", "******")or die("cannot connect"); 
mysql_select_db("*****")or die("cannot select DB");

//get data
$make=$_POST['make'];
$lic = $_POST['lic'];
$student=$_POST['student'];

$sql = "SELECT * FROM Endorsements WHERE lic='$lic'";
$result=mysql_query($sql);

//get the first entry from the result
while ($row = mysql_fetch_assoc($result))
{
  echo str_replace("*student*", $student,$row["end"]);
}
?>

Archived

This topic is now archived and is closed to further replies.

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