I cannot seem to pass $_GET into my MYSQL query. Lets say I have 2 tables in my DB, example1 and example2. I would like to query the table selected from the previous page. If I simply put the table into the query it runs fine. Here is what I have so far...
<html>
<form id="area" name="area" method="GET" action="test.php">
Select:
<select id="area" name="area">
<option value="example1">Example1</option>
<option value="example2">Example2</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</html>
test.php
include "connect.php";
$area = $_GET['area'];
$sql = "SELECT * FROM '$area' ";
$query = mysqli_query($sql);
if (isset($_POST['searchquery'])) {
$search_term = $_POST['searchquery'];
$result = mysqli_query($con, $sql);
}
?>
<strong>Search</strong>
<p>
<form action="test.php" method="POST">
Search: <input type="text" name="searchquery" />
<input type="submit" name="searchname" value="Search">
</form>
<table class="sortable.js" width="100%" cellpadding="1" cellspace="0">
<tr>
<td><strong>1</strong></td>
<td><strong>2</strong></td>
<td><strong>3</strong></td>
<td><strong>4</strong></td>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['1']; ?>
<td><?php echo $row['2']; ?>
<td><?php echo $row['3']; ?>
<td><?php echo $row['4']; ?>
<?php } ?>
</table>
If I echo $area it will show properly.
Your table name is quoted. Either remove the quote or use escape character like tick
$sql = "SELECT * FROM `$area` ";
Unlike mysql_(), mysqli_ functions require the connection to be specified explicitly. On the statement $query = mysqli_query($sql);
you used msqli_ but you didn't provide the connection (mysqli link resource variable ) you should pass in the connection as the firs parameter of the call like this $query = mysqli_query($connection, $sql);
. And also, don't forget to select a database. you can select a database like this. mysqli_select_db($connection,"database_name");
Remember to replace $connection
with your connection variable name.
I found that the following was the solution to my problem.
I changed:
<form action="test.php" method="POST">
to
<form action="test.php?area=<?php echo $area?>" method="POST">
Thanks for everyone help!