Jump to content

Variable doesn't work for a table name in PHP/MYSQL?


MasksMaster

Recommended Posts

Hello,
I'm putting together a simple wishlist and I encountered a problem where from a login page I'm sending a variable that will be a table name, but as the script executes it gives a mistake showing that it "can't see" where I'm trying to insert the data. Any advice will be very appreciated! Thank you!

[i][color=purple]index.html[/color][/i]

<form action="view.php" method="post">
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
<input type="Submit">

[i][color=purple]view.php[/color][/i]

<?php

[b]$table[/b]=$_POST['first'];

function insert_db($wish, $link){
require_once('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: <br />". DB::errorMessage($connection));
}


$query = "INSERT INTO [b]$table[/b] VALUES (NULL,'$wish','$link','','','')";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />". $query." ".DB::errorMessage($result));
}
echo "Inserted OK.<br />";

$query = "SELECT * FROM [b]$table[/b]";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />". $query." ".DB::errorMessage($result));
}
...

[i]error message[/i]
Could not query the database:
INSERT INTO VALUES (NULL,'hjk','ghj','','','') syntax error
You haven't posted the full code from view.php (missing brackets) so i'm assuming all viewable code is a part of the function insert_db() ?
If you are using the variable $table from inside this function you have to set it to global first, [color=green]global $table;[/color]
Thank you, [b]ruano84[/b], for looking into it!
Unfortunately it does the same thing and I'm just not sure why it wouldn't be giving the value... basically during the register procedure a table being created with all the needed parameters that is called exactly as a first field... Previous version worked just fine, but I needed to change it using PEAR and I'm stuck now... Thank you again!
Thank you, [b]Alpine[/b]!

I changed it but still seem to encounter the same thing. I'm new to php. Sorry that I'm asking such basic questions! Does this look correct?

global $table;
$table = $_POST['first'];

function insert_db($wish, $link){
require_once('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: <br />". DB::errorMessage($connection));
}

$query = "INSERT INTO $table VALUES (NULL,'$wish','$link','','','')";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />". $query." ".DB::errorMessage($result));
}
echo "Inserted OK.<br />";
// Display the table
$query = "SELECT * FROM $table";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />". $query." ".DB::errorMessage($result));
}
echo '<table border="1">';
echo "<tr><th>wish</th><th>link</th>><th>Remove</th></tr>";
while ($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
echo "<tr><td>";
echo $result_row["wish"] . '</td><td>';
echo $result_row["link"] . '</td><td>';
echo '<a href="delete.php?id='.$result_row["id"].'">Click
to remove if purchased</a></td></tr>';

}
echo "</table>";
echo "<a href='view.php'>Add More</a>";
$connection->disconnect();
}
I presume that the line:
[i]$query = "INSERT INTO $table VALUES (NULL,'$wish','$link','','','')";[/i] is outside of any function, so the problem could be in the posting.

Try to test if the index.html is posting the values to view.php. Give a name to the submit button, and at the begining of the code, write:
if($[i]buttonname[/i]){ echo 'it is posting'; }else { echo 'it isnt posting'; }
I tried and it is posting for the first part of the script where it assigns it to $table, but still for some reason doesn't insert the variable value into the INSERT. And it is inside the function, so I'm trying the global variable too. Thank you!
I restarted Apache and am getting it is not posting. So how would that be solved? But I got it to be posting again! Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.