MasksMaster Posted October 24, 2006 Share Posted October 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/ Share on other sites More sharing options...
ruano84 Posted October 24, 2006 Share Posted October 24, 2006 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! Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113708 Share on other sites More sharing options...
alpine Posted October 24, 2006 Share Posted October 24, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113712 Share on other sites More sharing options...
MasksMaster Posted October 24, 2006 Author Share Posted October 24, 2006 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! Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113714 Share on other sites More sharing options...
MasksMaster Posted October 24, 2006 Author Share Posted October 24, 2006 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"].'">Clickto remove if purchased</a></td></tr>';}echo "</table>";echo "<a href='view.php'>Add More</a>";$connection->disconnect();} Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113716 Share on other sites More sharing options...
ruano84 Posted October 24, 2006 Share Posted October 24, 2006 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'; } Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113717 Share on other sites More sharing options...
MasksMaster Posted October 24, 2006 Author Share Posted October 24, 2006 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! Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113721 Share on other sites More sharing options...
ruano84 Posted October 24, 2006 Share Posted October 24, 2006 Have you messenger? I think it could be easier to help you in that way. My email messenger is in my profile. Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113730 Share on other sites More sharing options...
alpine Posted October 24, 2006 Share Posted October 24, 2006 You have to set it as global inside the function, like this example:[code]<?php$var = "hello world!";function test(){global $var;return $var;}echo test(); // hello world!?>[/code] Link to comment https://forums.phpfreaks.com/topic/24945-variable-doesnt-work-for-a-table-name-in-phpmysql/#findComment-113733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.