View Full Version : MySQL WHERE command question
Beowolf
24th Jul 2003, 04:07 PM
I can successfully use the WHERE command when displaying data, but I am trying to use a variable and let the user choose exactly what is displayed. You see, it's a gaming site, and this particular section has strategies. I want to create a drop down menu thingy (<select>) to select what God strategies are shown (the game has 3 gods, Odin Thor and Loki)
This is the code I have.
$Query = "SELECT * from $TableName where(Strat='$Display')";
Up above I tried making a form to select the god for $Display This is where I come to the problem, I'm missing something (in the form, I think)
<form action="norse.php" method=post>
Display: <select name="$Order" id="$Order" value=$Order>
<option value="Loki">Loki</option>
<option value="Odin">Odin</option>
<option value="Thor">Thor</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
For one, whenever I put <? ?> around it, it gives me a Parse error
Parse error: parse error in /home/the-barr/public_html/temp/norse.php on line 4
The page i'm working on is called norse.php
Here is the whole page code, if you need it:
<? include("header.php") ?>
<table width="60%" border=1 bordercolor=#000000 class="content" align=center>
<tr><td valign=center align=center>
<form action="norse.php" method=post>
Display: <select name="$Order" id="$Order" value=$Order>
<option value="Loki">Loki</option>
<option value="Odin">Odin</option>
<option value="Thor">Thor</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</td></tr>
</table><br>
<?php
// Set the variables for the database access:
$Host = "localhost";
$User = "user";
$Password = "password";
$DBName = "name";
$TableName = "Norse";
$Link = mysql_connect ($Host, $User, $Password);
$Query = "SELECT * from $TableName where(Strat='$Display')";
$Result = mysql_db_query ($DBName, $Query, $Link);
// Create a table.
print ("<TABLE WIDTH=\"60%\" BORDER=1 BORDERCOLOR=#000000 CELLSPACING=0 CELLPADDING=4 ALIGN=CENTER class=content>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP colspan=2>Norse Strategies</TD>\n");
print ("</TR>\n");
// Row God and Row Strat are switched around
while ($Row = mysql_fetch_array ($Result)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP WIDTH=\"50%\"><b>$Row[God]</b> - <i>$Row[Strat]</i></TD>\n");
print ("<TD ALIGN=right VALIGN=TOP WIDTH=\"50%\"><i>$Row[Name]</i></TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP colspan=2>$Row[Body]</TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=center VALIGN=TOP colspan=2>...............</TD>\n");
print ("</TR>\n");
}
mysql_close ($Link);
print ("</TABLE>\n");
?>
<? include("footer.php") ?>
Of course, my MySQL connect data is correct, just changed it for security reasons though.
CaSCaDe
24th Jul 2003, 04:12 PM
iirc its something like
$Query = "SELECT * from $TableName where Strat = '$Display'";
that should do it
[edit] sorry read that wrong, you should use the name off the select box, this in this case Order in stead off Display
thus making the code
$Query = "SELECT * from $TableName where Strat = '$Order'";
also remove the $ from the forms name
CaSCaDe
24th Jul 2003, 04:24 PM
hmm as i read it (and understand it this stuff will never work..... you havent decalred any thing that will parse the form into or from the Dbase..... lemme post a lil example off some stuff i used to modify some Dbase data... it may bee a bit messy, but youll get the idea (and this 100% works)
<?php
$dbamcon = mysql_connect("localhost", "****", "****");
mysql_select_db("****", $dbamcon);
$amquery = ("select * from news where ID = '$uid'");
$amresult = mysql_query($amquery, $dbamcon);
$row = mysql_fetch_array($amresult);
$id = $row[0];
$mnews = $row[1];
$mheader = $row[2];
$mauthor = $row[3];
$mdate = $row[4];
?>
<link rel="stylesheet" href="../../CSS/DP.css" type="text/css">
<form name="form1" method="post" action="<?php echo($PHP_self . "?username=" . $username . "&password=" . $password . "&page=2&uid=$id"); ?>">
<span class="main">header:</span> <br>
<input type="text" name="modheader" class="Box" value="<?php echo($mheader); ?>" size="100">
<br>
<span class="main">Author: </span><br>
<span class="head"><?php echo($mauthor); ?></span>
<br>
<span class="main">news:</span> <br>
<textarea name="modnews" class="Box" cols="100" rows="20"><?php echo($mnews); ?></textarea>
<br>
<input type="submit" name="modify" value="modify" class="Box">
</form>
<?php
if ("modify" == $modify) {
$dbupcon = mysql_connect("localhost", "****", "****");
mysql_select_db("****", $dbupcon);
$sql = "UPDATE news SET " .
"header='$modheader', " .
"news='$modnews' " .
"WHERE ID='$id'";
if (mysql_query($sql)) {
echo("You just modified news topic '$mheader'");
}
}
?>
Beowolf
24th Jul 2003, 04:41 PM
Hmm...I think that just confused me more. Your first post I understood. What exactly in your example am I looking for?
Also
<form action="norse.php" method=post>
Display: <select name="Display" id="$Order" value=$Order>
<option value="Loki">Loki</option>
<option value="Odin">Odin</option>
<option value="Thor">Thor</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
What should my form action be? And do I need that form id and value?
CaSCaDe
24th Jul 2003, 04:42 PM
im not realy sure what your trying to do with this document, could you eleborate a bit more?
what that form basicly does i get some data from the Dbase, put it in a form and then when you hit the modify button it will change the data in the Dbase, wha im guessing you need is the part where the stuff is sent to the Dbase and gets other stuff from it... am i close?
CaSCaDe
24th Jul 2003, 04:51 PM
ok i took a quick look again and modified your code a bit, try this, i think this should fix your problem
<? include("header.php")
if (!$select) {
?>
<table width="60%" border=1 bordercolor=#000000 class="content" align=center>
<tr><td valign=center align=center>
<form action="<?php echo($PHP_Self . "?Select=" . $Order); ?>" method=post>
Display: <select name="Order">
<option value="">Select</option>
<option value="Loki">Loki</option>
<option value="Odin">Odin</option>
<option value="Thor">Thor</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</td></tr>
</table><br>
<?php
}
else if ($submit == "Submit") {
// Set the variables for the database access:
$Host = "localhost";
$User = "user";
$Password = "password";
$DBName = "name";
$TableName = "Norse";
$Link = mysql_connect ($Host, $User, $Password);
$Query = "SELECT * from $TableName where Strat ='$Select')";
$Result = mysql_db_query ($DBName, $Query, $Link);
// Create a table.
print ("<TABLE WIDTH=\"60%\" BORDER=1 BORDERCOLOR=#000000 CELLSPACING=0 CELLPADDING=4 ALIGN=CENTER class=content>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP colspan=2>Norse Strategies</TD>\n");
print ("</TR>\n");
// Row God and Row Strat are switched around
while ($Row = mysql_fetch_array ($Result)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP WIDTH=\"50%\"><b>$Row[God]</b> - <i>$Row[Strat]</i></TD>\n");
print ("<TD ALIGN=right VALIGN=TOP WIDTH=\"50%\"><i>$Row[Name]</i></TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP colspan=2>$Row[Body]</TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=center VALIGN=TOP colspan=2>...............</TD>\n");
print ("</TR>\n");
}
mysql_close ($Link);
print ("</TABLE>\n");
}
?>
<? include("footer.php") ?>
[edit - made a lil error, should work now :P]
Beowolf
24th Jul 2003, 05:01 PM
Parse error line 3....
Here is my test page, maybe this will help you understand more
http://www.the-barracks.com/temp/norse.php
EDIT: If you are using Mozilla, it won't display properly. Which is a whole other problem I will tackle after this.
Beowolf
24th Jul 2003, 05:05 PM
Got the page to display now, but it won't display any strats no matter what is selected....
Might it be because the form is not in <? ?>
CaSCaDe
24th Jul 2003, 05:06 PM
hmm afaik this should be correct, maybee you copied the source with the #php_self (madea lil error @ first)
if that doesnt work i realy dont know, ill have to take a closer look and maybee rewite your page, but since im going ona 2 week vacation in about 4 hours, i cant, sorry..........
CaSCaDe
24th Jul 2003, 05:08 PM
try to remove the else if (****) { and replace it with just else {
CaSCaDe
24th Jul 2003, 05:17 PM
ah found it you will need to make a second page, cuse the info from the form doesnt get parsed directly....
so besicly what you have to do is create 3 pages..
page 1 valled norse.php with following code
<? include("header.php")
if (!$select) {
require('norseform.php');
}
else {
require('Norsedisplay.php');
}
?>
then make a second page named norseform.php with the following code
<table width="60%" border=1 bordercolor=#000000 class="content" align=center>
<tr><td valign=center align=center>
<form action="<?php echo($PHP_Self . "?Select=" . $Order); ?>" method=post>
Display: <select name="Order">
<option value="">Select</option>
<option value="Loki">Loki</option>
<option value="Odin">Odin</option>
<option value="Thor">Thor</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</td></tr>
</table><br>
then a thirth page named norsedisplay.php with the following code
<?php
// Set the variables for the database access:
$Host = "localhost";
$User = "user";
$Password = "password";
$DBName = "name";
$TableName = "Norse";
$Link = mysql_connect ($Host, $User, $Password);
$Query = "SELECT * from $TableName where Strat ='$Select')";
$Result = mysql_db_query ($DBName, $Query, $Link);
// Create a table.
print ("<TABLE WIDTH=\"60%\" BORDER=1 BORDERCOLOR=#000000 CELLSPACING=0 CELLPADDING=4 ALIGN=CENTER class=content>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP colspan=2>Norse Strategies</TD>\n");
print ("</TR>\n");
// Row God and Row Strat are switched around
while ($Row = mysql_fetch_array ($Result)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP WIDTH=\"50%\"><b>$Row[God]</b> - <i>$Row[Strat]</i></TD>\n");
print ("<TD ALIGN=right VALIGN=TOP WIDTH=\"50%\"><i>$Row[Name]</i></TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP colspan=2>$Row[Body]</TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=center VALIGN=TOP colspan=2>...............</TD>\n");
print ("</TR>\n");
}
mysql_close ($Link);
print ("</TABLE>\n");
?>
those 2 pages should be in the same directory as the others
try that, its the last thing i can think off right now..... and i realy need to go now.....
CaSCaDe
24th Jul 2003, 05:21 PM
i see you got it, never mind then, cuse i also just came to the concolusion this solution is actualy the same as the one before that....... any how... sorry i couldnt realy help out
Beowolf
24th Jul 2003, 05:21 PM
Hmm I might try that, some guy at sitepoint got it working with this: <? include("header.php") ?>
<table width="60%" border=1 bordercolor=#000000 class="content" align=center>
<tr><td valign=center align=center>
<form action="norse.php" method=post>
Display: <select name="Display" id="Display">
<option value="Loki">Loki</option>
<option value="Odin">Odin</option>
<option value="Thor">Thor</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</td></tr>
</table><br>
<?php
$Display = $_POST['Display'];
// Set the variables for the database access:
$Host = "localhost";
$User = "--";
$Password = "--";
$DBName = "---";
$TableName = "Norse";
$Link = mysql_connect ($Host, $User, $Password);
$Query = "SELECT * from $TableName where(Strat='$Display')";
$Result = mysql_db_query ($DBName, $Query, $Link);
// Create a table.
print ("<TABLE WIDTH=\"60%\" BORDER=1 BORDERCOLOR=#000000 CELLSPACING=0 CELLPADDING=4 ALIGN=CENTER class=content>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP colspan=2>Norse Strategies</TD>\n");
print ("</TR>\n");
// Row God and Row Strat are switched around
while ($Row = mysql_fetch_array ($Result)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP WIDTH=\"50%\"><b>$Row[God]</b> - <i>$Row[Strat]</i></TD>\n");
print ("<TD ALIGN=right VALIGN=TOP WIDTH=\"50%\"><i>$Row[Name]</i></TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=left VALIGN=TOP colspan=2>$Row[Body]</TD>\n");
print ("</TR>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=center VALIGN=TOP colspan=2>...............</TD>\n");
print ("</TR>\n");
}
mysql_close ($Link);
print ("</TABLE>\n");
?>
<? include("footer.php") ?>
He mentioned something about var security or something, though.
vBulletin® v3.8.0 Release Candidate 2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.