Helaas heb ik weinig verstand van MySQLi maar mijn OC website maakt hier gebruik van. Ik heb een zoekformulier via HTML met 4 dropdown menu’s en deze zijn alle 4 standaard ingevuld met diverse waardes. Nu is het de bedoeling dat wanneer ik op ‘zoek’ druk dat hij mijn data gaat op zoeken in mijn database en dan de resultaten weergeeft. Iemand die mij een duwtje in de goede richting kan geven?
Heb nog even wat op google gezocht en krijg nu de product_id's weergegeven wanneer ik zoek. Dit is in ieder geval al meer als wat ik had. Dit is niet helemaal wat ik weergegeven wou hebben, maar het is een begin.


<?php
$con = mysqli_connect("localhost", "root", "", "opencart");

if (isset($_POST['search'])) { //If the form's "Submit" button was clicked...
    echo 'Search Result :<br />';
    //Set variables to info posted from the form
    $width = $_POST['width'];
    $height = $_POST['height'];
    $length = $_POST['length'];
    //Escape the strings so they're safe to use in a MySQL query
    $width = mysqli_real_escape_string($con, $width);
    $height = mysqli_real_escape_string($con, $height);
    $length = mysqli_real_escape_string($con, $length);
    //Start building the query
    $where = " 1 = 1 ";
    if ($width != "") {  //If the user selected a width
        $where.=" AND width='$width'";  //Add a width specification to the query
    }
    if ($height != "") {  //If the user selected a height
        $where.=" AND height='$height'"; //Add a height specification to the query
    }
    if ($length != "") { //If the user selected a length
        $where.=" AND length='$length'"; //Add a length specification to the query
    }
    //If the user made all three selections, your query (essentially) would be like this example:
    //$sql = "select * from oc_product where 1=1 AND width='$width' AND height='$height' AND length='$length'";
    $sql = "select * from oc_product where $where";
    $rs = mysqli_query($con, $sql);
    while ($sar = mysqli_fetch_assoc($rs)) {  //For each 'mobile_name' in the database that matches the query
        echo '<br />' . $sar['product_id']; //Print a new line along with the value from 'mobile_name' field
    }
}
?>
Echo eens $sql, dan weet je wat er uitgevoerd wordt.
Dan krijg ik de volgende uitkomst.

select * from oc_product where 1 = 1 AND width='0' AND height='0' AND length='0'
vanwaar die 1 = 1? Je kan toch direct WHERE width=0 AND height=0 gebruiken?
Verder zijn single-quotes niet nodig voor getallen (integers) in een voorwaarde. Zie mijn cursieve deel.

Wat vul je in je POST-formulier in? Komen deze ook overeen?
De quotes zijn aangepast. Onderstaand mijn formulier.


<form name="frm" method="post">
    WIDTH
    <select name="width">
        <option value="">- - Select Width - -</option>
		<option value="0">0</option>
        <option value="200">200</option>
        <option value="250">250</option>
        <option value="300">300</option>
    </select>
    HEIGHT
    <select name="height">
        <option value="">- - Select Height - -</option>
		<option value="0">0</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="150">150</option>
    </select>
    LENGHT
    <select name="length">
        <option value="">- - Select Length - -</option>
		<option value="0">0</option>
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="30">30</option>
    </select>
    <input name="search" value="Search" type="submit" />
</form>
Als ik deze query zie:
select * from oc_product where 1 = 1 AND width='0' AND height='0' AND length='0'

Dan vermoed ik gewoon dat er geen enkele record is met zowel lengte, width als height een waarde van 0.
Controleer dat eens in je database via phpMyAdmin of o.i.d.
Deze query geeft wel de resultaten weer. Als ik bijvoorbeeld mijn product_id laat weergeven laat hij wel diverse id's zien. Ik krijg, wanneer ik op zoeken druk, wel resultaten op mijn scherm.
Het probleem is dus opgelost, lijkt me?
Ja klopt, heel erg bedankt voor alle hulp en moeite. Het formulier werkt nu.

Reageren