Jump to content
Jedijon

How to make it insert info from session

Recommended Posts

I don't wan't it to be inserted into the database but use the session info as the name of the table to grave data from for the html table

Share this post


Link to post
Share on other sites

you need to put {..} around the array element inside a string

$result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; 

 

Share this post


Link to post
Share on other sites

$result = mysqli_query($con,"SELECT * FROM table name"); but where the table name goes I wan't it use $_SESSION['datakey'] to get the table name

Share this post


Link to post
Share on other sites

Define "didn't work" . That tells us nothing.

What does $_SESSION['datakey'] contain? Is it the name of a table?

What is your code after you get the $result?

We are not telepathic!

Share this post


Link to post
Share on other sites
24 minutes ago, mac_gyver said:

this sounds like a bad design. 

I couldn't agree more. Variable tables names always rings alarm bells

Share this post


Link to post
Share on other sites
1 minute ago, Jedijon said:

$_SESSION['datakey'] contains the name of the table

OK, so that leaves

  • Define "didn't work" . That tells us nothing.
  • What is your code after you get the $result?

Share this post


Link to post
Share on other sites

I know OP, tell us what the Real problem is you are trying to solve rather than asking about the poorly implemented attempt at solving the problem. What is the overall task at hand?

Edited by benanamen

Share this post


Link to post
Share on other sites

<?php
$con=mysqli_connect("IP","Username","Password","Database name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; 

echo "<table border='1'>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['column1'] . "</td>";
echo "<td>" . $row['column2'] . "</td>";
echo "<td>" . $row['column3'] . "</td>";
echo "<td>" . $row['column4'] . "</td>";
echo "<td>" . $row['column5'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

Share this post


Link to post
Share on other sites

Try putting session_start() right at the top of your code.

You need that to access session data

Share this post


Link to post
Share on other sites

I have that 

<?php
// Initialize the session
session_start();
 
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: ../login");
    exit;
}
?>

Share this post


Link to post
Share on other sites

Error message 

Parse error: syntax error, unexpected ';', expecting ',' or ')' in /srv/disk1/2648096/www/site/test/index.php on line 20

Share this post


Link to post
Share on other sites

PS Before you connect to mysql, call

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

That will force it to report errors.

Share this post


Link to post
Share on other sites

Notice anything wrong here?

$result = mysqli_query($con,"SELECT * FROM {$_SESSION['datakey']}"; 

 

Share this post


Link to post
Share on other sites

You should write query statements into a variable to make it easier to debug should you need to, as in this case.

	$q = "SELECT * FROM {$_SESSION['datakey']}"; 
	echo "Query looks like: $q";
	 $results = mysqli_query($con, $q);
	

Run this code and look at the actual query you created to ensure that your assumptions are correct.

Share this post


Link to post
Share on other sites
3 minutes ago, Jedijon said:

I have that 

Not in the code you showed us

Share this post


Link to post
Share on other sites

Query looks like: SELECT * FROM datakey
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in /srv/disk1/2648096/www/site/test/index.php on line 35

 

It shows the session data

Edited by Jedijon

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.