Reputation: 1625
I am doing a project that uses php and MySQL. I am really new to php and I have searched a lot but still haven't found a clean solution.
The problem is, firstly user inputs key words and it returns searched values(table names) to variables.
echo '<h2> Search Result: </h2>';
$searchSQL = "select distinct table_name from information_schema.columns where lower(column_name) like lower('%$search%') and table_schema='university'";
$result = $conn->query($searchSQL);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$table_name = $row['table_name'];
echo "<input type='submit' name=$table_name value=$table_name>"."<br />";
}
Now the buttons are created. If user clicks on any button,it should go further and return the column names of each table. I don't know how to do with non-static buttons.
Upvotes: 1
Views: 2381
Reputation: 1667
Instead of creating submit button create button type='button'
Either using javascript/jquery or using html properties call a page which will return the table details:
echo "<input type='button' name='" . $table_name . "' value='" . $table_name .
"' onclick=\"location.href='http://yourwebsite.com?tbl=" . $table_name."'\"><br />";
You can do the same calling some js function also.
Upvotes: 0
Reputation: 41893
First, don't forget to setup the actual <form>
. Wrap them all up.
Second, I'd suggest, just use the same name attribute in your submit buttons:
name="table_name"
So it should look like this now:
echo '<h2> Search Result: </h2>';
$searchSQL = "
SELECT DISTINCT table_name FROM information_schema.columns
WHERE LOWER(column_name) LIKE LOWER('%$search%') AND table_schema = 'university'
";
$result = $conn->query($searchSQL);
echo '<form method="POST" action="show_colums.php">'; // opening form tag
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$table_name = $row['table_name'];
echo "<input type='submit' name='table_name' value='$table_name' /> <br/>";
// ^ don use the $table_name in the name attribute
}
echo '</form>'; // closing form tag
Don't forget to also add the action=""
attribute in the form tag. Just point it to whichever PHP script that will do the form processing. For this example, just use show_columns.php
Now, just apply the DESCRIBE
in that PHP file that will process the selected button:
Inside show_columns.php
:
if(!empty($_POST['table_name'])) {
$table_name = $_POST['table_name']; // get input
$sql = "DESCRIBE {$table_name}"; // use DESCRIBE
$query = $conn->query($sql); // execute query
while($row = $query->fetch_assoc()) { // fetch rows
// do whatver you need to do, table or whatever you like
echo $row['Field']; // field name
}
}
Edit: Or you can be a lot safer using prepared statements on the first form:
echo '<h2> Search Result: </h2>';
$searchSQL = "
SELECT DISTINCT table_name FROM information_schema.columns
WHERE LOWER(column_name) LIKE LOWER(?) AND table_schema = 'university'
";
$stmt = $conn->prepare($searchSQL);
$search = '%' . $search . '%';
$stmt->bind_param('s', $search);
$stmt->execute();
$stmt->bind_result($table_name);
echo '<form method="POST" action="show_columns.php">';
while($stmt->fetch()) {
echo "<input type='submit' name='table_name' value='$table_name' /><br/>";
}
echo '</form>';
Upvotes: 1