telsiin Posted April 18, 2007 Share Posted April 18, 2007 Hello All I am trying to make a select statement that would allow me to change the table, base on the user selection from a drop down menu list. I don't know if its possible or my syntax is wrong but then again I am a Newbie to PHP sql = SELECT * FROM "'.$_POST[Tableselection].'" WHERE "'.$_POST[userid].'" ='1'; Thank you Antonio Quote Link to comment https://forums.phpfreaks.com/topic/47656-solved-select-statement-with-changing-tables-base-on-user-selection/ Share on other sites More sharing options...
MadTechie Posted April 18, 2007 Share Posted April 18, 2007 try <?php sql = "SELECT * FROM "'.$_POST['Tableselection'].'" WHERE "'.$_POST['userid'].'" ='1'"; ?> better looking still <?php sql = "SELECT * FROM `{$_POST['Tableselection']}` WHERE `{$_POST['userid']}` ='1';"; ?> EDIT: Updated 2nd one Quote Link to comment https://forums.phpfreaks.com/topic/47656-solved-select-statement-with-changing-tables-base-on-user-selection/#findComment-232725 Share on other sites More sharing options...
telsiin Posted April 19, 2007 Author Share Posted April 19, 2007 Ok MadTechie I try that but I did not get the result I was hoping for. When I made the change it didn't display an error .But it did not show the form either. Could it be because the varible is not set at the time the script runs. I have a session variable running as well for testing purpose. The session variable was displayed but the rest of the page is blank. Antonio Quote Link to comment https://forums.phpfreaks.com/topic/47656-solved-select-statement-with-changing-tables-base-on-user-selection/#findComment-233088 Share on other sites More sharing options...
AndyB Posted April 19, 2007 Share Posted April 19, 2007 Without seeing any of your code, it's impossible to make rational suggestions on making it do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/47656-solved-select-statement-with-changing-tables-base-on-user-selection/#findComment-233092 Share on other sites More sharing options...
telsiin Posted April 19, 2007 Author Share Posted April 19, 2007 Here copy of the tables -- Database: `test` DROP TABLE IF EXISTS `newjerseydept`; CREATE TABLE IF NOT EXISTS `newjerseydept` ( `User_id` int(5) NOT NULL auto_increment, `Fname` varchar(30) collate latin1_general_ci default NULL, `Lname` varchar(30) collate latin1_general_ci default NULL, `Phone` varchar(11) collate latin1_general_ci default NULL, PRIMARY KEY (`User_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=9 ; INSERT INTO `newjerseydept` VALUES (1, 'test5', 'test5', '12016896587'); INSERT INTO `newjerseydept` VALUES (2, 'test6', 'test6', '19732569854'); INSERT INTO `newjerseydept` VALUES (3, 'test7', 'test7', '17325478543'); INSERT INTO `newjerseydept` VALUES (4, 'test8', 'test8', '12015698524'); DROP TABLE IF EXISTS `floridadept`; CREATE TABLE IF NOT EXISTS `floridadept` ( `User_id` int(5) NOT NULL auto_increment, `Age` int(3) NOT NULL, `Sex` enum('Male','Female') collate latin1_general_ci NOT NULL, `Uname` varchar(30) collate latin1_general_ci NOT NULL, `Fname` varchar(30) collate latin1_general_ci default NULL, `Lname` varchar(30) collate latin1_general_ci default NULL, `Phone` varchar(11) collate latin1_general_ci default NULL, PRIMARY KEY (`User_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ; INSERT INTO `floridadept` VALUES (1, 33, 'Male', 'test1', 'test1', 'test1', '14075698526'); INSERT INTO `floridadept` VALUES (2, 34, 'Female', 'test2', 'test2', 'test2', '13215648526'); INSERT INTO `floridadept` VALUES (3, 21, 'Male', 'test3', 'test3', 'test3', '18635894672'); INSERT INTO `floridadept` VALUES (4, 18, 'Female', 'test4', 'test4', 'test4', '14075698563'); And here copy of the php page name changingtables.php <?php require_once('../Connections/test.php'); ?> <?php mysql_select_db($database_test, $test); $query_Recordset1 = "SELECT * FROM `{$_POST['location']}` WHERE `{$_POST['location']}`.User_id='3'"; $Recordset1 = mysql_query($query_Recordset1, $test) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> <html> <head> <title>Changing Tables </title> </head> <body> <form name="ChangingTables" method="post" action="changingtables.php"> <table width="257"> <tr> <th width="78" scope="row"><div align="left">Location</div></th> <td width="167"><label for="select"></label> <select name="location" id="select"> <option value="floridadept">Florida</option> <option value="newjerseydept">New Jersey</option> </select> </td> </tr> <tr> <th scope="row"><div align="left">User Id </div></th> <td><label for="textfield"></label> <input type="text" name="userid" id="textfield" value="<?php echo $row_Recordset1['User_id']; ?>"> <label for="label"></label></td> </tr> <tr> <th scope="row"><div align="left">First Name </div></th> <td><label for="label2"></label> <input type="text" name="fname" id="label2" value="<?php echo $row_Recordset1['Fname']; ?>"></td> </tr> <tr> <th scope="row"><div align="left">Last name </div></th> <td><label for="label3"></label> <input type="text" name="lname" id="label3" value="<?php echo $row_Recordset1['Lname']; ?>"></td> </tr> <tr> <th scope="row"><div align="left">Phone</div></th> <td><label for="label4"></label> <input type="text" name="phone" id="label4" value="<?php echo $row_Recordset1['Phone']; ?>"></td> </tr> <tr> <th scope="row"><div align="left"> <label for="Submit"></label> </div></th> <td><input type="submit" name="Select Location" value="Select Location" id="Select Location"></td> </tr> </table> </form> </body> </html> <?php mysql_free_result($Recordset1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/47656-solved-select-statement-with-changing-tables-base-on-user-selection/#findComment-233136 Share on other sites More sharing options...
telsiin Posted April 24, 2007 Author Share Posted April 24, 2007 <?php $hostname_test = "localhost"; $database_test = "test"; $username_test = "xxxxx"; $password_test = "xxxxx"; $test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(),E_USER_ERROR); if( isset($_POST['location']) && ($_POST['userid']) ){ mysql_select_db($database_test, $test); $query_Recordset1 = "SELECT * FROM {$_POST['location']} WHERE User_id={$_POST['userid']}"; $Recordset1 = mysql_query($query_Recordset1, $test) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); } ?> <html> <head> <title>Changing Tables </title> </head> <body> <form name="ChangingTables" method="post" action="changingtables.php"> <table width="257"> <tr> <th width="78" scope="row"><div align="left">Location</div></th> <td width="167"><label for="select"></label> <select name="location" id="select"> <option><?php echo $location ?></option> <option value="floridadept" >florida</option> <option value="newjerseydept">New Jersey </option> </select> </td> </tr> <tr> <th scope="row"><div align="left">User Id </div></th> <td><label for="textfield"></label> <input type="text" name="userid" id="textfield" value="<?php echo $row_Recordset1['User_id']; ?>"> <label for="label"></label></td> </tr> <tr> <th scope="row"><div align="left">First Name </div></th> <td><label for="label2"></label> <input type="text" name="fname" id="label2" value="<?php echo $row_Recordset1['Fname']; ?>"></td> </tr> <tr> <th scope="row"><div align="left">Last name </div></th> <td><label for="label3"></label> <input type="text" name="lname" id="label3" value="<?php echo $row_Recordset1['Lname']; ?>"></td> </tr> <tr> <th scope="row"><div align="left">Phone</div></th> <td><label for="label4"></label> <input type="text" name="phone" id="label4" value="<?php echo $row_Recordset1['Phone']; ?>"></td> </tr> <tr> <th scope="row"><div align="left"> <label for="Submit"></label> </div></th> <td><input type="submit" name="SelectLocation" value="Select Location" id="Select Location"></td> </tr> </table> </form> </body> </html> <?php //mysql_free_result($Recordset1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/47656-solved-select-statement-with-changing-tables-base-on-user-selection/#findComment-236969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.