BlackWidow Posted May 21, 2012 Share Posted May 21, 2012 Hi, can anyone see what is wrong with this code. The search works ok on the model field, but will not work on the make field. <?php include("canoe_header.php"); ?> <div id ="container"> <div id="content"> <div id="header"> <div id="midd"> <img src="canoe_image.jpg" width="850" height="160" alt="Search Canoes" /> </div> </div> <div id = "middle"> <h2>Edit Canoes/Kayaks</h2> <?php $make = $_POST['make']; $model = $_POST['model']; if ((!isset($make)) || (!isset($model))) { ?> <p> Search for the canoe or kayak by Make and/or Model. </p> <p> Click on edit underneath the canoe or kayak that you wish to edit. </p> <form method="post" action="update_canoes.php"> <fieldset> <h2>Please enter search details.</h2> <p><label for="make">Make:</label><input type="text" name="make"/><br /></p> <p><label for="model">Model:</label><input type="text" name="model"/></p> <p class="submit"><input type="submit" name="submit" value="Search Canoes/Kayaks"/></p> </fieldset> </form> <?php } else { $hostname = "localhost"; $username = "changeme"; $password = "changeme"; $database = "changeme"; $con = mysql_connect("$hostname", "$username", "$password"); mysql_select_db("$database"); if(!$con) { echo "Cannot connect to database." . mysql_error();; exit; } if(!mysql_select_db("$database")) { echo "Unable to select database: " . mysql_error(); exit; } //query to see if there is a record which matches $sql="SELECT * FROM canoes WHERE make LIKE '%$make%' OR model LIKE '%$model%'"; $rs = mysql_query($sql,$con); //run the query if(!$rs) { echo "Cannot run query."; exit; } if (mysql_num_rows($rs) == 0) { echo "Sorry no canoes or kayaks can be found.."; exit; } while ($row = mysql_fetch_assoc($rs)) { echo '<table class="search1"> <tr><th width="20">ID</th><th width="40">Make</th><th width="65">Model</th><th width="75">Colour</th><th width="75">Type</th> <th width="50">Location</th><th width="100">Comments</th><th width="250">Image</th></tr> <tr><td width="20">' . $row['id'] . '</td><td width="40">' . $row['make'] . '</td><td width="65">' . $row['model'] . '</td> <td width="75">' . $row['colour'] . '</td><td width="75">' . $row['type'] . '</td><td width="50">' . $row['location'] . '</td> <td width="100">' . $row['comments'] . '</td><td width="250">' . $row['image'] . '</td></tr> <tr><td colspan="2" width="100"><a href="edit_canoes.php?id=' . $row['id'] . '">Edit Record</a></td></tr> </table>'; echo "<br />"; } mysql_free_result($rs); mysql_close($con); } ?> <?php include("canoe_footer.php"); ?> </div> </div> <?php include("canoe_menu.php"); ?> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/ Share on other sites More sharing options...
cyberRobot Posted May 21, 2012 Share Posted May 21, 2012 Did you try displaying $make to verify that it contains what you expect? Are you getting any errors...what happens when you try searching with $make? FYI, the <label> tags won't work unless the id attribute is added to the corresponding input fields. For example: ... <label for="make">Make:</label><input type="text" name="make" id="make" /> ... Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347267 Share on other sites More sharing options...
ManiacDan Posted May 21, 2012 Share Posted May 21, 2012 Black Widow, you've posted your database connection information in public. Change all your passwords immediately. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347276 Share on other sites More sharing options...
BlackWidow Posted May 21, 2012 Author Share Posted May 21, 2012 Thank you ManiacDan, I hadn't realised I had done that. Normally my user, password details are all in one file that is brought in. I have changed them all now. Checked the database and it is ok. Thank you again Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347284 Share on other sites More sharing options...
BlackWidow Posted May 21, 2012 Author Share Posted May 21, 2012 Hi cyberRobot, tried that doesn't make any difference. What I can't understand is that the model field works ok, it's just the make field that doesn't work. I'm completely confused Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347287 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2012 Share Posted May 21, 2012 Maybe you should define "doesn't work". Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347289 Share on other sites More sharing options...
cyberRobot Posted May 21, 2012 Share Posted May 21, 2012 When searching by $make, what do you get? Does it display "Cannot run query.", "Sorry no canoes or kayaks can be found..", etc.? Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347290 Share on other sites More sharing options...
BlackWidow Posted May 21, 2012 Author Share Posted May 21, 2012 Hi cyberRobot, the search brings back a list of all the canoes in the database. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347293 Share on other sites More sharing options...
BlackWidow Posted May 21, 2012 Author Share Posted May 21, 2012 Hi Pikachu2000, sorry, If I search using the model field only, for example if I use sonic as the search criteria it brings back a list of all of the canoes with sonic in the model field. If I search using the make field only, for example if I use pyr as the search criteria it brings back a list of all of the canoes in the database, instead of all of the canoes made by Pyranha. This is the bit that doesn't work. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347295 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2012 Share Posted May 21, 2012 Have you echoed the query string to make sure it contains the values you'd expect it to contain? That's where I'd probably start debugging this. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347297 Share on other sites More sharing options...
BlackWidow Posted May 21, 2012 Author Share Posted May 21, 2012 Hi Pikachu2000, I have used a debugger and it comes back with the error message: Notice: Undefined index: make on line 43 and Notice: Undefined index: model on line 44. These lines relate to the following $make = $_POST['make']; $model = $_POST['model']; I can't see what is wrong with them. I'm off to my paying job now, so please accept my apologies if I don't answer anymore replies today. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347299 Share on other sites More sharing options...
Jessica Posted May 21, 2012 Share Posted May 21, 2012 What are the names of your inputs? They should match exactly. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347300 Share on other sites More sharing options...
cyberRobot Posted May 21, 2012 Share Posted May 21, 2012 After thinking about the query a little more, the problem is most likely due to the OR part. You're asking to get all results which match part of the make or model field. If one of those fields is blank, you'll get all results. Try changing the query to something like: <?php //... $sqlCombine = ''; $sql = "SELECT * FROM canoes WHERE "; if($make != '') { $sql .= "make LIKE '%$make%' "; $sqlCombine = ' OR '; } if($model != '') { $sql .= "{$sqlCombine}model LIKE '%$model%'"; } //... ?> Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347301 Share on other sites More sharing options...
cyberRobot Posted May 21, 2012 Share Posted May 21, 2012 Hi Pikachu2000, I have used a debugger and it comes back with the error message: Notice: Undefined index: make on line 43 and Notice: Undefined index: model on line 44. These lines relate to the following $make = $_POST['make']; $model = $_POST['model']; I can't see what is wrong with them. The $POST array isn't defined until the form is submitted. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347303 Share on other sites More sharing options...
ManiacDan Posted May 21, 2012 Share Posted May 21, 2012 OP is receiving help elsewhere Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1347330 Share on other sites More sharing options...
BlackWidow Posted May 24, 2012 Author Share Posted May 24, 2012 Thank you for your help, it is now sorted and please accept my apologies if I have upset anyone for posting on another forum. Link to comment https://forums.phpfreaks.com/topic/262861-php-code-help/#findComment-1348394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.