I want to create a main page for my learning site (trying to learn stuff), but I have a problem with (I think) getting response from query.
HTML for getting the table what I want. Value in button is the table name from Database
<body>
<div class="s">
<form action="table.php" method="POST">
<ul>
<!-- <li><input class="button" type="submit" name="ok" value="**table name**"></li> -->
<li><input class="button" type="submit" name="ok" value="name"></li>
<li><input class="button" type="submit" name="ok" value="exam"></li>
</ul>
</form>
</div>
</body>
PhP
if (isset($_POST["ok"]))
{
$TableIWant = $_POST["ok"]; //Stores the name of the table
$servername = "X";
$username = "X";
$password = "X";
$dbname = "X";
//Sql - gets column names?
$sqlcol = "SELECT CONCAT('',GROUP_CONCAT(`COLUMN_NAME`),'') FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='$dbname' AND `TABLE_NAME`= '$TableIWant'";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
echo "$sqlcol <br>"; // writes out (just for chechk)
$column = $conn->query($sqlcol);
echo "$column <br>";
// after I get column names I go and get the data from database and make table, but I am stuck on getting the column names
}
2
Answers
Well the solution It is not the best you can do but it works.
This is a response to your answer, as opposed to your original question.
Your current
die
statements for handling errors will not do anything. As of PHP 8.1.0, the defaultmysqli_driver::$report_mode
isMYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT
, meaning an exception will be thrown if there is a connection or query error. For more detail on error handling, you might want to read this write up on error handling.Your "solution" uses an unnecessary separate info schema query to get the column names and it is interpolating variables directly into the SQL. Parameterising this is trivial, and I would suggest that it is even more important to do things "properly" while learning, as it helps you avoid forming bad habits.
Instead of the info schema query, you can retrieve the list of columns with mysqli_result::fetch_fields.
Now that
$columns
is an array of objects, you would change your column header iteration to:The query above is interpolating
$TableIWant
directly into the SQL, as the table name cannot be parameterised. Even though this is just for your "learning site", it is another good example of where you should implement some sensible checks. If this was public facing you definitely wouldn’t want the end user to grab all the data from any table they want, or to exploit the vulnerability by attempting a SQL Injection attack. So, to make sure the user can only access allowed tables you would have a whitelist, something like: