PhP Form Question!

They have: 3 posts

Joined: Dec 2005

Hello to all, and anyone that maybe able too help!

Sad 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's picture

He has: 379 posts

Joined: Apr 2005

= $_POST['search']

JeevesBond's picture

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!

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

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%>&nbsp;".$line['edate']."</td><td width=12%>&nbsp;".$line['etime']."</td><td

width=12%><b>&nbsp;"
.$line['who']."</b></td><td width=12%>&nbsp;".$line['venue']."</td><td

width=12%>&nbsp;"
.$line['city']."</td><td width=12%>&nbsp;".$line['support']."</td><td

width=12%>&nbsp;&pound;"
.$line['price']."</td><td width=12%>&nbsp;<a href=".$line['url']."><b>Buy Tickets</b></a></td>";

}   
echo
"</table>\n";
mysql_close($link);

?>
'

chrishirst's picture

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.