Jump to content

Selecting Tables


deved

Recommended Posts

Is it possible to select different tables within a database in MySQL based on the input from a form?If so, could anyone advise on what commands are required to make it work with radio buttons.Any help would be much appreciated.

Link to comment
Share on other sites

You mean execute a different query based on what radio button is selected???

$radioresult = $_POST['radiobuttonname'];if($radioresult == "something"){    $query = "sql statement";}else if($radioresult == "something else"){    $query = "a different sql statement";}

Link to comment
Share on other sites

You mean execute a different query based on what radio button is selected???
$radioresult = $_POST['radiobuttonname'];if($radioresult == "something"){    $query = "sql statement";}else if($radioresult == "something else"){    $query = "a different sql statement";}

Sort of, but instead of using the 2 radio buttons to perform a query within the table... actually using the 2 radio buttons to select one of 2 tables, then executing other queries.
Link to comment
Share on other sites

It's the same thing as what aspnetguy posted, just a decision structure based off the radio button.

if($radioresult == "something"){   $table = "table1";}else if($radioresult == "something else"){   $table = "table2";}$sql = "SELECT * FROM {$table} WHERE ... ";

Link to comment
Share on other sites

It's the same thing as what aspnetguy posted, just a decision structure based off the radio button.
if($radioresult == "something"){   $table = "table1";}else if($radioresult == "something else"){   $table = "table2";}$sql = "SELECT * FROM {$table} WHERE ... ";

Thanks for that. Sorry aspnetguy, still a bit new to PHP and sometimes can't work out the code unless it hits me in the face. Cheers.
Link to comment
Share on other sites

Can 2 tables be specified from the 1 result such as:

if($radioresult == "something"){   $table = "table1a" && $tbl = "table1b";}else if($radioresult == "something else"){   $table = "table2a" && $tbl = "table2b";}

I'm trying to retrieve data from a row of one table by using data in a row from the second table. The link is a column in both tables that has identical data.

Link to comment
Share on other sites

Well, the code you wrote is syntactally correct, but it doesn't do what you think. && is a logical AND operator, it evaluates to a boolean true/false. So this statement:$table = "table1a" && $tbl = "table1b";gets parsed like this:$table = ("table1a" && $tbl = "table1b");$table = ("table1a" && ($tbl = "table1b"));Since $tbl = "table1b" is a logical true statement (assigment statements always evaluate to logical true), it goes to this:$table = ("table1a" && true);The statement "table1a" && true also evaluates to true, since any non-empty value (such as "table1a") is considered true.So, in the end, this statement:$table = "table1a" && $tbl = "table1b";has the effect of setting $tbl equal to "table1b" and $table equal to boolean true. If you want to set two different variables, just use two statements:

if($radioresult == "something"){  $table = "table1a";  $tbl = "table1b";}

Link to comment
Share on other sites

if($radioresult == "something"){  $table = "table1a";  $tbl = "table1b";}

Can u tell me why this doesn't work (I only get the result from the first query).

<?php$con = mysql_connect("localhost","name","password");if (!$con) {die('Could not connect: ' . mysql_error());}mysql_select_db("dBase", $con);$acc = $_POST['acc'];$radio = $_POST['area'];if ($radio == "input1"){$table = "table1a"; $tbl = "table1b";}else if ($radio == "input2"){$table = "table2a"; $tbl = "table2b";}$result = mysql_query("SELECT * FROM {$table} WHERE account='$acc'");while($row = mysql_fetch_array($result)) { echo "<h3>Job Number</h3>"; echo $row['job']; } $result = mysql_query("SELECT * FROM {$tbl} WHERE Job='job'");while($row = mysql_fetch_array($result)) { echo $row['data']; }?>

Link to comment
Share on other sites

It looks fine, I would guess that there is nothing in the table where Job='job'.  Open up the database and doublecheck.

I checked and there is data in the field.I also entered a lineecho "DATA";aboveecho $row['data'];which didn't show upIn the 2nd $result line I've tried linking Job='job' to the line echo $row['job']; could this be the cause?Could the result at line echo $row['job']; be turned into a variable, which can then be used in the 2nd query?Have just tested and its definately the link between Job='job' and echo $row['job']; by removing the WHERE function, and all the data including heading DATA showed. Edited by deved
Link to comment
Share on other sites

I'm not sure I'm following.  If you still have problems, post your code and let me know.

Problem sorted. Ended up running a more specific query that matched information from both tables.It would be useful however, if information retrieved and echoed, such as the "echo $row['job']; line, could then be used in another query, as in the line $result = mysql_query("SELECT * FROM {$tbl} WHERE Job='job'");. I'm sure there's a way and with more experience will probably be able to work it out.Thanks for all your help to now tho.
while($row = mysql_fetch_array($result)){echo "<h3>Job Number</h3>";echo $row['job'];}$result = mysql_query("SELECT * FROM {$tbl} WHERE Job='job'");while($row = mysql_fetch_array($result)){echo $row['data'];

Link to comment
Share on other sites

You can substitute any string into any other string, it doesn't matter if you're using it for databases or output or whatever, it's just a string.$result = mysql_query("SELECT * FROM {$tbl} WHERE Job='{$row['job']}'");

Link to comment
Share on other sites

You can substitute any string into any other string, it doesn't matter if you're using it for databases or output or whatever, it's just a string.$result = mysql_query("SELECT * FROM {$tbl} WHERE Job='{$row['job']}'");

Would it then be possible to send the result of echo $row['job'] to another page using $_POST without using fields in a form? If that makes any sense.
Link to comment
Share on other sites

If it's going through POST, you will need to submit it in a form, but it can be a hidden value so that it doesn't show up on the page. If you don't want to use a form, you can keep it in the session and have access on the next page.

Link to comment
Share on other sites

If it's going through POST, you will need to submit it in a form, but it can be a hidden value so that it doesn't show up on the page.  If you don't want to use a form, you can keep it in the session and have access on the next page.

How do you set the value to hidden if value has already been used ie. <input type="text" name="name" value="$var">I'm also having trouble finding info on sessions. Could you tell me how far off the mark this is?Insert the <?php session_start(); ?> before the <html> tag (is this required on each page)?Insert$_SESSION['user'] = $user;$_SESSION['pass'] = $encrypted_pass;on the following page somewhere after the $_POST, $user & $encrypted_passSorry for all the questions, but I've tried a number of tutorials and they're a bit vague in this department. :)
Link to comment
Share on other sites

If you have a value, you can do whatever you want with it. You can display it on the page, or put it in a form variable, or whatever you want. There aren't any constraints on what you can do, all you have is data. <input type="text" name="name" value="<?php echo $var ?>">You're just printing a variable, PHP doesn't care where you print it, and the browser doesn't know any difference.For sessions, that's pretty much it. You can see some examples here:http://www.php.net/manual/en/function.session-start.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...