Jump to content


Photo

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


  • Please log in to reply
8 replies to this topic

#1 MasksMaster

MasksMaster
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 October 2006 - 04:28 PM

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!

index.html

<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">

view.php

<?php

$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 />";

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

error message
Could not query the database:
INSERT INTO VALUES (NULL,'hjk','ghj','','','') syntax error

#2 ruano84

ruano84
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationVenezuela

Posted 24 October 2006 - 04:56 PM

hi,

Be sure that $_POST['first'] is giving you a value, it doesn't seems to. instead of POST use $_REQUEST['first'], and try again.

Good luck!


Alexis RR

#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 24 October 2006 - 05:04 PM

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, global $table;

#4 MasksMaster

MasksMaster
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 October 2006 - 05:06 PM

Thank you, ruano84, 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!

#5 MasksMaster

MasksMaster
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 October 2006 - 05:13 PM

Thank you, Alpine!

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();
}

#6 ruano84

ruano84
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationVenezuela

Posted 24 October 2006 - 05:17 PM

I presume that the line:
$query = "INSERT INTO $table VALUES (NULL,'$wish','$link','','','')"; 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($buttonname){ echo 'it is posting'; }else { echo 'it isnt posting'; }
Alexis RR

#7 MasksMaster

MasksMaster
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 October 2006 - 05:26 PM

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!

#8 ruano84

ruano84
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationVenezuela

Posted 24 October 2006 - 05:56 PM

Have you messenger? I think it could be easier to help you in that way. My email messenger is in my profile.
Alexis RR

#9 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 24 October 2006 - 05:59 PM

You have to set it as global inside the function, like this example:
<?php

$var = "hello world!";

function test()
{
global $var;
return $var;
}

echo test(); // hello world!

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users