Passing variable from form to sql query
Posted: Wed Feb 24, 2010 5:27 am
I want to use the variable input into a form to be the WHERE value in an sql query.
I have tried the following code which seems to work ie it generates no error messages but the query returns an empty result. If I run the query by inputing the value entered in the form directly in the query it works.
Can anyone suggest what is the problem or is the method itself incorrect.
Thanks
Dave
FORM "form1.html"
<form method="post" action="array_script.php">
0.<br/>
<input type="varchar" Day="Day"/><br/><br/>
<input type="submit" name="submit"/> </form>
PHP Script "array_script.php"
<?php
$Day = $_POST['Day'];
$username="user";
$password="abcdefg";
$database="use_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM courttimes Where Day='$Day'";
$result=mysql_query($query);
$num=mysql_numrows($result);
echo "<b><center>Database Output</center></b><br><br>";
echo "<table border='1'>
<tr>
<th>Day</th>
<th>Date</th>
<th>Time1</th>
<th>Player1</th>
<th>Player2</th>
<th>Time2</th>
<th>Player3</th>
<th>Player4</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Day'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time1'] . "</td>";
echo "<td>" . $row['Player1'] . "</td>";
echo "<td>" . $row['Player2'] . "</td>";
echo "<td>" . $row['Time2'] . "</td>";
echo "<td>" . $row['Player3'] . "</td>";
echo "<td>" . $row['Player4'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I have tried the following code which seems to work ie it generates no error messages but the query returns an empty result. If I run the query by inputing the value entered in the form directly in the query it works.
Can anyone suggest what is the problem or is the method itself incorrect.
Thanks
Dave
FORM "form1.html"
<form method="post" action="array_script.php">
0.<br/>
<input type="varchar" Day="Day"/><br/><br/>
<input type="submit" name="submit"/> </form>
PHP Script "array_script.php"
<?php
$Day = $_POST['Day'];
$username="user";
$password="abcdefg";
$database="use_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM courttimes Where Day='$Day'";
$result=mysql_query($query);
$num=mysql_numrows($result);
echo "<b><center>Database Output</center></b><br><br>";
echo "<table border='1'>
<tr>
<th>Day</th>
<th>Date</th>
<th>Time1</th>
<th>Player1</th>
<th>Player2</th>
<th>Time2</th>
<th>Player3</th>
<th>Player4</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Day'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time1'] . "</td>";
echo "<td>" . $row['Player1'] . "</td>";
echo "<td>" . $row['Player2'] . "</td>";
echo "<td>" . $row['Time2'] . "</td>";
echo "<td>" . $row['Player3'] . "</td>";
echo "<td>" . $row['Player4'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>