Forum Moderators: coopster
I am NEWBIE & trying to develop internal control system with PHP-MySql & have one problem.
I want to enter dynamic variable into MySql like :
"I have checked report of Mr. $userName from $previousDate to $currentDate…..”
Wherein - $username, $previousDate, $currentDate will be called from other table.
My codes :
PHP Code:
INSERT INTO `checkReport ` (`schID`, `schNo`, `noteNo`, `subschName`, `content`) VALUES (1, 1, 'a', '', ' I have checked report of Mr. ‘.$username.’ from ‘.$previousDate.’ to ‘.$currentDate.’ ….)
which is not working… Please guide me through.
Thanks,
Vikram
Where are you defining those variables that you want to insert into your query? Also, are you receiving any errors from PHP or MySQL? The query that you have above looks fine. What's actually not working?
$query = " INSERT INTO checkReport (schID, schNo, noteNo, subschName, content) ";
$query .= " VALUES ('1', '1', 'a', '', 'I have checked report of Mr. $username from $previousDate to $currentDate') ";
It's easier to read. If the contents of the variables still don't show up, it's because the variables aren't defined.
<?
$username="Mr.XYZ";
$previousDate="31/12/2006";
$currentDate="31/04/2007";
print
'I have checked report of Mr. $username from $previousDate to $currentDate';
And result I get is :
'I have checked report of Mr. $username from $previousDate to $currentDate';
Values are not displayed
Vikram
The problem seems to lie with your variables. It doesn't seem like they are being defined correctly. Show us where you are getting the variables $userName, $previousDate, $currentDate.
<?
//Variables defined
$username="Mr.XYZ";
$previousDate="31/12/2006";
$currentDate="31/04/2007";
print
'I have checked report of Mr. $username from $previousDate to $currentDate';
And result I get is :
'I have checked report of Mr. $username from $previousDate to $currentDate';
?>
Thanks again,
Vikram
print "I have checked report of Mr. $username from $previousDate to $currentDate";
//Data fetched from Table A
$username=$row_view[‘username’];
$previousDate= $row_view[‘previousDate’];
$currentDate=$row_view[‘currentDate’];
//[value in Table A = ('Mr. XYZ’, ‘31/12/2006’, ‘02/05/2007’) ]
//Data fetched from Table B
$content =$row_view[‘content’];
//[value in Table B = ('1', '1', 'a', '', 'I have checked report of Mr. $username from $previousDate to $currentDate') ]
print
”$content”;
I want result to be like this
I have checked report of Mr. XYZ from 31/12/2006 to 02/05/2007.
Whereas I am getting result like this :
"I have checked report of Mr. $username from $previousDate to $currentDate"
?>
Thanks again,
Vikram
Once the variables have been extracted from Table A and moved into the global namespace of the script we need to evaluate the string as PHP code. PHP has a function for this properly named eval() [php.net].
<?php
// Variables extracted from Table A:
$username = "Mr.XYZ";
$previousDate = "31/12/2006";
$currentDate = "31/04/2007";
// Variable from Table B that contains PHP code
// we want to parse (evaluate):
// $row_view['content'] = 'I have checked report of Mr. $username from $previousDate to $currentDate';
// Create a variable named $content that contains the parsed PHP variables:
eval("\$content = \"{$row_view['content']}\";");
print $content;
// prints:
// I have checked report of Mr. Mr.XYZ from 31/12/2006 to 31/04/2007
exit;
?>