Scabby Posted February 21, 2007 Share Posted February 21, 2007 NEW ! to php I'm trying to get these scripts to search the database and, well it aint! Can someone tell this noob what I'm doing wrong, I have spent 3 hours on this reading everywhere - no joy TOWN NAME INPUT <html> <body> <form name="input" action="search_db.php" method="post"> Town: <input type="text" name="town_search_input"> <input type="submit" value="Submit"> </form> </body> </html> SEARCH SCRIPT <?php // Search database for towns meetings $con = mysql_connect("localhost","scabby","skippy"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("meetings", $con); if ($result = mysql_query("SELECT * FROM meeting WHERE town={$_GET['town_search_input']}")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { echo $row['name'].$row['address'].$row['time']; } } } ?> My SQL database is set-up with a few sample entries running local When I run this it's returning nothing. I think I'm having trouble understanding variables and how they relate to php. I have programmed before in Basic and Hex but that was a few years ago now!! Database name: meetings table: meeting Fields: town, name, address, time Anyone help please, I'm sure if I can get a few basics inside this head of mine I will be off and running! also If anyone can recommend a tutorial site which explains the relationship between functions rather than simple giving examples would be helpful!! Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted February 21, 2007 Share Posted February 21, 2007 Try.... <?php // Search database for towns meetings $con = mysql_connect("localhost","scabby","skippy"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("meetings", $con); if ($result = mysql_query("SELECT * FROM meeting WHERE town = '{$_GET['town_search_input']}'")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { echo $row['name'].$row['address'].$row['time']; } } } else { echo mysql_error(); } ?> Quote Link to comment Share on other sites More sharing options...
Scabby Posted February 21, 2007 Author Share Posted February 21, 2007 Still blank screen, Is this a problem with my SQL DB? I created a new DB and interted 5 tables into it with some random info for testing purposes, but thats all. It might be worth mentioning I'm a MySQL noob aswell Quote Link to comment Share on other sites More sharing options...
Scabby Posted February 21, 2007 Author Share Posted February 21, 2007 Scratch that, I think I have my problem. off to do some more reading up on MySQL. Thanks for the reply Quote Link to comment Share on other sites More sharing options...
Scabby Posted February 22, 2007 Author Share Posted February 22, 2007 This thing is doing my head in, I still cannot get the lines to read from the database!! I have now read up on MySQL databases and yes I should have had an index, ok so I have now constructed a new database complete with index The database is as follows; HOST="localhost", USERNAME=scabby, PASSWORD=skippy database name = meetings_db table = meetings_records fields = "id" - (INT, AUTO INCREMENT, PRIMARY, NULL) "town" - VARCHAR (30) "address" - VARCHAR (100) "time" - VARCHAR (30) First Script = "input_town.php" <html> <body> <form name="input" action="search_db.php" method="post"> Town: <input type="text" name="town_search_input"> <input type="submit" value="Submit"> </form> </body> </html> Second Script = "search_db.php" <?php // Search database for towns meetings $con = mysql_connect("localhost","scabby","skippy"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("meetings_db", $con)or die(mysql_error()); if ($result = mysql_query("SELECT * FROM meetings_records WHERE town = '{$_GET['town_search_input']}'")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { echo $row['town'].$row['address'].$row['time']; } } } else { echo mysql_error(); } ?> Please Please folks, anyone. Why the hell isn't this working? town_search_input is working fine $result returns "Resource id #3" ie the third record for my search which is correct Quote Link to comment Share on other sites More sharing options...
corbin Posted February 23, 2007 Share Posted February 23, 2007 The problem was that you had it looking for something via the GET variables in the sql query, but your form is using POST.... It took me a while to figure that out. I even tried it on my own machine lol.... Anyway, I made some other changes to the script, too. I eliminated some extra if and elses, and mainly that's all. If you copy and paste this, you'll need to change the DB stuff back to your stuff. <?php // Search database for towns meetings if(!get_magic_quotes_gpc()) { //always clean variables for sql injection! If you don't know what that is, google it. foreach($_POST as $k => $v) { //for each post variable: add slashes to it. $_POST[$k] = addslashes($v); } foreach($_GET as $k => $v) { $_GET[$k] = addslashes($v); } } if(!$_POST['town_search_input']) { //if the input field is not set: echo "Please go <a href=\"input_town.php\">back</a> and enter a town name."; } else { //if the form field is set: $con = mysql_connect("localhost","root","root") or die('Could not connect: ' . mysql_error()); mysql_select_db("test", $con)or die(mysql_error()); $result = mysql_query("SELECT * FROM meetings_records WHERE town = '{$_POST['town_search_input']}'") or die(mysql_error()); //heres where your problem was. You had $_GET, but your form uses the post method. I changed it to post, but you may wish to change the form method to get. if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { echo $row['town'].$row['address'].$row['time']; } } } ?> Quote Link to comment Share on other sites More sharing options...
Scabby Posted February 23, 2007 Author Share Posted February 23, 2007 in truth I changed this around so many times i forgot to change that line back! what a nob head! soon as I changed that back it worked! Thank Corbin!! I'm new to this but VERY determined to clue myself up on php and MySQL. Can you recommend some sites for learning this correctly. What I mean by correctly is they give the reasons rather than simply examples as you have done in your amended script. Also which editor would you recommend? I'm using php Designer 2007 but I find the reference help cumbersome. once again thanks for your help Scabby Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.