Author Topic: [SOLVED] Help Required MYSQL Query Please  (Read 747 times)

0 Members and 1 Guest are viewing this topic.

Offline mrbigdogTopic starter

  • Irregular
  • Posts: 6
    • View Profile
[SOLVED] Help Required MYSQL Query Please
« on: December 30, 2007, 11:26:29 AM »
Hi,

Hoping someone can help me. I have looked on the net for tutorials and examples but have found nothing...

Basically, I have a table for trainees details, and then two other tables for logging those trainees though training on different devices (t545, t34)

The form before my details page needs to get the trainees name and details for those trainees in either the t545 or the t34 table.

How do I get my mysql recordset to choose data from the t545 or the t34 table depending on what the user selects from the drop down menu?

Eg.

SELECT cust_id, user_id, user_firstname, user_surname
FROM trainees, tablename
WHERE tablename.cust_id = trainees.cust_id

The 'tablename' variable would be ($_GET['product_name']) from the previous form on the previous page...

Any ideas how I can do this? Help appreciated. Hope i have explained it well enough.

Paul.

Offline revraz

  • Freak!
  • Posts: 6,866
  • Gender: Male
  • Problems Resolved: 352
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #1 on: December 30, 2007, 12:17:25 PM »


$tablename = $_GET['product_name'];

$sql = "SELECT cust_id, user_id, user_firstname, user_surname
FROM trainees, tablename
WHERE '$tablename'.cust_id = trainees.cust_id";
If I can't make it better, then I'll make it worse, but it definitely won't be the same.

Offline mrbigdogTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #2 on: December 30, 2007, 06:33:20 PM »
Hi,

I have used he following code in the recordset:

SELECT trainees.user_id, user_firstname, user_surname, user_designation, user_dept
FROM trainees, tablename
WHERE `tablename`.user_id = colname AND trainees.user_id = colname

colname = $_GET['cust_id']
tablename = $_GET['product_name']

I get the following error when i get to the details page:

Table 'mckinl3.'t34'' doesn't exist

any suggestions?

Offline revraz

  • Freak!
  • Posts: 6,866
  • Gender: Male
  • Problems Resolved: 352
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #3 on: December 30, 2007, 07:14:08 PM »
Code: [Select]

$colname = $_GET['cust_id'];
$tablename = $_GET['product_name'];

$sql="SELECT trainees.user_id, user_firstname, user_surname, user_designation, user_dept
FROM trainees, '$tablename'
WHERE '$tablename'.user_id = '$colname' AND trainees.user_id = '$colname'";
$result=mysql_query($sql) or die(mysql_error());

If I can't make it better, then I'll make it worse, but it definitely won't be the same.

Offline mrbigdogTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #4 on: December 31, 2007, 07:09:08 AM »
Hi Revraz,

Thanks for that. However, still cant get it to work. I'm using dreamweaver, so the sql code is going into a recorset (which normally works fine) - not as an actual $sql query into the php. Every alternative I try gives me a msql syntax error - especially if i use single quotes around 'tablename'.

The only way it will vaguely work is if i enclose `tablename` in the forward slash type of quotes. Then I get the error from the post above.

I have tried pasting the code you wrote for me into the php script instead, but get a mysql syntax error.

The page, if you want to have a look is :http//www.mckinleymed.co.uk/online-training/Admin/training_lookup.php

You will have to log in as ( username: phampson@interactive-tutors.co.uk , Password: arnold ).

Any other ideas?

And thanks for your time...

Paul.

« Last Edit: December 31, 2007, 07:11:06 AM by mrbigdog »

Offline mrbigdogTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #5 on: January 01, 2008, 05:20:07 AM »
hhhheeeeeellllllpppppp!

Offline noister

  • Irregular
  • Posts: 10
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #6 on: January 01, 2008, 08:29:22 AM »
try don't put a single quote ('') and this (``)...

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #7 on: January 01, 2008, 04:03:21 PM »
Column or table names only need `...` if they contain spaces or if they have the same name as a mysql reserved word. They never have '...', those are for string literals.
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline mrbigdogTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Help Required MYSQL Query Please
« Reply #8 on: January 03, 2008, 04:56:46 AM »
Thanks very much guys. The problem is now fixed using the following code:

<?php

$table = $_GET['product_name'];
$customer = $_GET['cust_id'];

mysql_connect("mysql-3.db.vi.net", "mckinl3", "password") or die(mysql_error());
mysql_select_db("mckinl3") or die(mysql_error());


$query = mysql_query("SELECT * FROM trainees, `$table` WHERE trainees.cust_id = $customer AND `$table`.user_id = trainees.user_id");
while($row = mysql_fetch_array($query, MYSQL_ASSOC)) {

   foreach($row as $k=>$v)
      $$k = $v;
   
   
   ?>

     
        <td width="26%" class="normal_caps"><? echo $user_firstname; ?> <? echo $user_surname; ?></td>
        <td width="21%" class="normal_caps"><? echo $user_designation; ?></td>
        <td width="18%" class="normal_caps"><? echo $user_dept; ?></td>
        <td width="35%" class="normal_caps">EDIT<br />
        DELETE</td>
      </tr> <?
++$i;
}
echo "";

?>
    </table>
  </div>

Easy when you know how!

Thanks Paul.