Jump to content

PhP code help


BlackWidow

Recommended Posts

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.  :confused:

 

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.