Jump to content

[SOLVED] Select Statement with changing tables base on user selection


Recommended Posts

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

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

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

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);
?>

 

 

<?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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.