PhP Form Question!
Hello to all, and anyone that maybe able too help!
Been racking my brain with this one all night. I've created a simple form which will be intended to be used to return some sql results.
http://www.scenesters.co.uk/test/
What I have next is this php code,
$sql='SELECT *, DATE_FORMAT(date_time,"%M, %Y") as emon_year, DATE_FORMAT(date_time,"%D") as
edate,DATE_FORMAT(date_time,"%H:%i") as etime FROM lordlover WHERE (who = "Kaiser") OR (city = "Leeds")';
I know I have this correct as when the page is loaded it only returns the coloums with Kaiser and Leeds in. What I would like to do is use the form as a method too search for my users. I thought it would be as easy as
$sql='SELECT *, DATE_FORMAT(date_time,"%M, %Y") as emon_year, DATE_FORMAT(date_time,"%D") as
edate,DATE_FORMAT(date_time,"%H:%i") as etime FROM lordlover WHERE (who = "$search") OR (city = "$search")';
But this is not the case, search is the id tag of the form feild as you would be able too see in the source.
I hope I have explained this enouth.
Any help or a point in the right direction is very much thanked in advance
chrishirst posted this at 11:39 — 7th December 2005.
He has: 379 posts
Joined: Apr 2005
= $_POST['search']
JeevesBond posted this at 12:47 — 7th December 2005.
He has: 3,956 posts
Joined: Jun 2002
Chris is correct, you have to access the variable that is being POSTed by the form. You were pretty close to the answer though!
Take a look at: http://uk2.php.net/variables.external for the official documentation, good luck!
a Padded Cell our articles site!
KeithMcL posted this at 15:38 — 7th December 2005.
He has: 176 posts
Joined: Oct 1999
If you change the form method to GET you can use the $_GET['search]' option instead. This also means you can create links directly to search qeuries using a link like domain.com/search.php?search=Leeds
peanut posted this at 18:34 — 7th December 2005.
They have: 3 posts
Joined: Dec 2005
Hmm thanks for the replys. As I said I had been racking my brain most of the night trying to get it right, and had already tried the suggested. I'll post the full code too see if it makes it more understandable.
The Form
index.html
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form action="search_gigs.php" method="post">
<p><b>Search:</b> <input type="text" name="search" size="20" maxlength="200" /></p>
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
The Submitter
search_gigs.php
<?php
$var=1;//here 3 means 3days
$link = mysql_connect("localhost", "dbun", "dbpw") or die("Could not connect");
mysql_select_db("dbname") or die("Could not select database");
$sql='SELECT *, DATE_FORMAT(date_time,"%M, %Y") as emon_year, DATE_FORMAT(date_time,"%D") as
edate,DATE_FORMAT(date_time,"%H:%i") as etime FROM lordlover WHERE (who = $_POST["search"]) OR (city = $_POST["search"])';
$sql.=" ORDER BY date_time ASC";
$result = mysql_query($sql) or die("Query failed");
echo "<table width=100% border=0 cellspacing=0 cellpadding=0>\n";
echo "<tr><td width=12%><b>Date</b></td><td width=12%><b>Time</b></td><td width=12%><b>Who</b></td><td
width=12%><b>Venue</b></td><td width=12%><b>City</b></td><td width=12%><b>Support</b></td><td width=12%><b>Price</b></td><td
width=12%><b>Tickets</b></td></tr>";
$cmon_year="";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
//print_r($line);
if ($cmon_year != $line['emon_year']) {
print "\t<tr><td colspan=8><B>".$line['emon_year']."</B></td></tr>";
$cmon_year=$line['emon_year'];
}
print "\t<tr>\n";
echo "<td width=12%> ".$line['edate']."</td><td width=12%> ".$line['etime']."</td><td
width=12%><b> ".$line['who']."</b></td><td width=12%> ".$line['venue']."</td><td
width=12%> ".$line['city']."</td><td width=12%> ".$line['support']."</td><td
width=12%> £".$line['price']."</td><td width=12%> <a href=".$line['url']."><b>Buy Tickets</b></a></td>";
}
echo "</table>\n";
mysql_close($link);
?>
chrishirst posted this at 08:05 — 8th December 2005.
He has: 379 posts
Joined: Apr 2005
echo out the sql string after concatenation to see if it is what you expect.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.