Log in

View Full Version : SQL question in php



jes___per
03-01-2009, 09:42 PM
Hi

are working on a php site where u can make a SQL question
by choosing from different options from a php dropdown meny:


<b>Select:</b>
<select name="Select">
<option>Välj</option>
<option>Alla (*)</option>
<option>Förnamn</option>
<option>Efternamn</option>
<option>Adress</option>
<option>Telefon</option>
<option>E-post</option>
<option>Personnummer</option>
</select>
<b>From:</b>
<select name="From">
<option>Välj</option>
<option>DB</option>
<option>DB2</option>
</select>
<b>Where:</b>
<select name="Where">
<option>Välj</option>
<option>Alla (*)</option>
<option>Förnamn</option>
<option>Efternamn</option>
<option>Adress</option>
<option>Telefon</option>
<option>E-post</option>
<option>Personnummer</option>
</select>
<select name="Where2">
<option>Välj</option>
<option>=</option>
<option>></option>
<option><Less then</option>
<option>>=</option>
<option><=</option>
<option><></option>
<option>LIKE</option>
</select>
<b>Optional:</b>
<input type="text" size="30" name="Optional"></br>

The php dokument:


<?php
mysql_connect("localhost", "admin", "") or die('Du är inte välkomen här!!!');
mysql_select_db('test');

$Select=$_POST['Select'];
$From=$_POST['From'];
$Where=$_POST['Where'];
$Where2=$_POST['Where2'];
$Optional=$_POST['Optional'];

if ($Select==$_POST['Select'] && $From=$_POST['From'] && $Where=$_POST['Where'] && $Where2=$_POST['Where2'] && $Optional=$_POST['Optional'])
{
echo "Data ur databas $From med följande SQL kod:", "</p>";
echo "<b>Select</b> $Select <b>From</b> $From <b>Where</b> $Where <b>Where2</b> $Where2 <b>Optional</b> $Optional";
// Make a MySQL Connection
$query = "SELECT $Select FROM $From WHERE $Where$Where2 '$Optional'";

$result = mysql_query($query) or die(mysql_error());
echo "<table border =1> </br>";
echo "<tr>
<th>$Select</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>".
"<td>".$row[$Select]."</td>".
"</tr>";
}
echo "</table>";
}
else
{
echo "...";
}
?>

but i cant get it to work the problom starts when it are defining the where question, do any1 have any idée what the problom is and have a solution to it?

city_coder
03-01-2009, 10:12 PM
try echoing each variable you receive
ie
echo $select;
echo $from;
etc or just
echo $select.$from

and see if the contents are what you wanted.

if everything seems fine then echo $query. once you've got the query echoed, put that to the database and see if it comes back with answers and take it from there.

hope that helps

JasonDFR
03-02-2009, 07:06 AM
$query = "SELECT '$Select' FROM '$From' WHERE '$Where' '$Where2' '$Optional' ";

You must place single quotes around your variables in the SQL query.

They way your query is written, $Optional is not optional.

Good luck.

jes___per
03-02-2009, 08:24 AM
try echoing each variable you receive
ie
echo $select;
echo $from;
etc or just
echo $select.$from

and see if the contents are what you wanted.

if everything seems fine then echo $query. once you've got the query echoed, put that to the database and see if it comes back with answers and take it from there.

hope that helps

ok, thx but when i try it, the code printing i get from it are "Select Förnamn From 1 Where 1 Where2 1 Optional test"

when From are selected as DB, Where as Förnamn and Where2 as =

city_coder
03-02-2009, 01:37 PM
As Jason says, i think the problem may be that there needs to be quotes round the variables otherwise it will just use the variable names.

See if that makes a difference.

jes___per
03-02-2009, 05:34 PM
yeah think that did the trick, thanks for all.

BTW hove do i make a checkbox that when i check it a new "select dropdown" will apere?

Schmoopy
03-02-2009, 05:38 PM
That will be one for the JavaScript section :)