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.

Link to comment
Share on other sites

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']);
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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']);
?>

Link to comment
Share on other sites

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"]);
}
?>

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.