Forum Moderators: coopster

Message Too Old, No Replies

Using a PHP variable as a column name in a MySQL query

         

mamlo30

5:27 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



hi all,
I have problem with following script if anyone can halp me I really apreciate
I just started to learn PHP.

<?php
if (isset($_REQUEST['Submit']))
{
$StartDate = $_REQUEST["StartDate"];
$EndDate = $_REQUEST["EndDate"];
}
$result=mysql_query("SELECT * FROM Counterparty2008 WHERE TradeDate =>' $_REQUEST'[StartDate] and TradeDate <' $_REQUEST'EndDate'", $link);
print "<table border=\1\><tr><th>Broker</th>";
print "<th>AccountN</th> <th>TicketN</th> <th>TradeDate</th> <th>ValueDate</th> <th>CCYPair</th> <th>BS</th> <th>Amount</th> <th>StrikePrice</th> <th>ID</th> <th>Comentary</th>";
while ($row= mysql_fetch_array($result))
{
print "<tr>";
foreach($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";

}

print"</table>";
mysql_close($link);
?>
So poin is that I wan to retrive data with form and in form I have dates

<html>
<head>
<title>Dealers Traded Lots data</title>
</head>

<body>

<form action="myconection2.php" method= "post">

<table>

<tr>

<td>Start Date (yyyy-mm-dd): </td>

<td><input type="text" name ="StartDate"></td>

</tr>

<tr>

<td>End Date (yyyy-mm-dd):</td>

<td><input type="text" name ="EndDate"></td>

</tr>


<tr><td colspan=2 align=left>
<tr><td>
<input type="Submit" value ="Submit">
</table>
</form>
</body>
</html>

please help me if you can.
thank you

[edited by: jatar_k at 2:40 pm (utc) on Nov. 7, 2008]
[edit reason] fixed sidescroll [/edit]

roMancer

7:01 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



hi,

I think your question is basically how to form the proper mysql-query from php, because it looks faulty to me.
I'd suggest like this:

$result = mysql_query("SELECT * FROM Counterparty2008 WHERE TradeDate =>'".$_REQUEST['StartDate']."' and TradeDate <'".$_REQUEST['EndDate']."';", $link);

note the dots (.) which joins strings. and note the different quotation marks: start with double quotes (for php), start the single quotes (for mysql), end double quotes (php), join in php-variable, restart double quotes (php), end single quotes (mysql), continue sql statement, ...

this is the most correct way to do it: keep your php-variables out of the strings, by closing the string and joining the variable (and joining more strings in this case).

there's more to quotes, but I'll leave it for now. ;-)

Bruce

9:13 pm on Nov 7, 2008 (gmt 0)

10+ Year Member



Hi ...

there are problem in mark condition i mean =>

WHERE TradeDate =>'".$_REQUEST['StartDate']."

correct code
WHERE TradeDate >='".$_REQUEST['StartDate']."

Anyango

5:40 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




this is the most correct way to do it: keep your php-variables out of the strings, by closing the string and joining the variable (and joining more strings in this case).

PHP variables will work within Double quotes, its just that when you want arrays to work within double quotes you need to do some little extra, but if u want to avoid that then simply copy the array value on that index to a variable and use that variable name. Like you are storing $_REQUEST["StartDate"] into $StartDate, so why not use $StartDate and simplify our query.

<?php
if (isset($_REQUEST['Submit']))
{
$StartDate = $_REQUEST["StartDate"];
$EndDate = $_REQUEST["EndDate"];
}

$result=mysql_query("SELECT * FROM Counterparty2008 WHERE TradeDate =>'$StartDate' and TradeDate < '$EndDate'", $link);
print "<table border=\1\><tr><th>Broker</th>";
print "<th>AccountN</th> <th>TicketN</th> <th>TradeDate</th> <th>ValueDate</th> <th>CCYPair</th> <th>BS</th> <th>Amount</th> <th>StrikePrice</th> <th>ID</th> <th>Comentary</th>";
while ($row= mysql_fetch_array($result))
{
print "<tr>";
foreach($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";

}

print"</table>";
mysql_close($link);

?>
So poin is that I wan to retrive data with form and in form I have dates

<html>
<head>
<title>Dealers Traded Lots data</title>
</head>

<body>

<form action="myconection2.php" method= "post">

<table>

<tr>

<td>Start Date (yyyy-mm-dd): </td>

<td><input type="text" name ="StartDate"></td>

</tr>

<tr>

<td>End Date (yyyy-mm-dd):</td>

<td><input type="text" name ="EndDate"></td>

</tr>

<tr><td colspan=2 align=left>
<tr><td>
<input type="Submit" value ="Submit">
</table>
</form>
</body>
</html>

[edited by: Anyango at 5:48 am (utc) on Nov. 8, 2008]

Bruce

8:18 pm on Nov 8, 2008 (gmt 0)

10+ Year Member



hi

the problem is here in code below
TradeDate =>'$StartDate'

replace with

TradeDate >='$StartDate'

Anyango

6:20 am on Nov 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea Bruce is right, it has to be like that.