Forum Moderators: coopster

Message Too Old, No Replies

PHP User Form

Help with using variables in a SQL statement using listboxes

         

c21cmo

11:55 am on Mar 30, 2006 (gmt 0)

10+ Year Member



I am trying to create several list boxes on a form, which displays the columns names of the tables in my database. This is to allow the user to create and display their own queries.

I have created the list boxes and can display the result on the target form but does anybody know the syntax for the following query?

<?php
$field1 = $_POST['select1'];
$field2 = $_POST['select2'];

$conn = mysqli_connect("localhost","root","password","db1");
$result = mysqli_query($conn, "select $field1 from allcolumns where assetid like '$field2';");
?>

Any Help would be much appreciated.

Thanks

omoutop

1:23 pm on Mar 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if i understand correctly u want to show database field names in a list?

If so perhapos this threat will help you
[webmasterworld.com...]

c21cmo

2:43 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



I am only wanting to display some field names from five different tables. There are 56 fields in total so just plan on using a few from each table.

I just want to be able to create a query which can select the field names selected from the list boxes. As I only want to show some fields I am happy to declare them as options manually.

I would like "Select $Listbox1, $Listbox2, $listbox3 from.....";

This is the incorrect syntax for selecting variables in a sql statement though. If you know the right syntax that would be great.

Thanks.

jatar_k

3:45 pm on Mar 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that syntax is correct unless the variable is empty

if each field name is variable meaning there is a dynamic number of fields then you need to do a little more testing to build your query.

check whoich are set and append with a comma. It needs to work so that if there is only one, then no comma. If there is more than you could loop it and add a comma between each making sure you don't leave a trailing comma.