PHP Dropdown Code note working from SELECT

0

I am using the code below to populate a dropdown in another page. The echo does give me the amoclient text however its not passing it to the $client in the SELECT part. I have changed the $client in the SELECT to a text like (“SELECT pcode FROM tbl_project WHERE pclient=‘myName’”) for a test and this does work and populate the dropdown with the pcode list from the database. I am not sure what is causing this not to work using the variable $client. Please assist.

<?php
require_once "dbConn.php";
$client = $_POST["amoclient"];
echo "<script>alert('$client');</script>";
$result = mysqli_query($db,"SELECT pcode FROM tbl_project WHERE pclient=$client");
?>
<option value="">Select Project Code</option>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row["pcode"];?>"><?php echo $row["pcode"];?></option>
<?php
}
?>

Why have you now switched to the mysqli extension? Your first thread on this forum was using the much simpler and better designed PDO extension.

Had you continued to use the PDO extension and a prepared query, what you are doing now would work, because the prepared query place-holder that goes in the sql query statement doesn’t need/use any quotes around it, regardless of the type of data.

Also, had you enabled exceptions for database statement errors, as mentioned in the previous thread (available for both the mysqli and PDO extension), you would be getting an error alerting you to the problem with the current query syntax.

Hi, I altered my 1st dropdown code to give me a unique list and this worked. I then changed my 2nd dropdown code to the one with prepared statements but it gives me a blank dropdown so when I resorted to straight mysqli it gave me a list but only like explained above. So I am presently lost… Please help.

//first dropdown code
<select onChange="getProjectCodes(this.value);" name="amoclient" id="amoclient" class="form-control">
                                <option value="">Select Client</option>
                                <?php
                                require_once "dbConn.php";
                                $result = mysqli_query($db, "SELECT DISTINCT pcleint FROM tbl_project");
                                while ($row = mysqli_fetch_array($result)) {
                                ?>
                                    <option value="<?php echo $row['pcode']; ?>"><?php echo $row["pclient"]; ?></option>
                                <?php
                                }
                                ?>
                            </select>
//my second dropdown code
<?php
require_once "dbConn.php";
$client = $_POST["amoclient"];
echo "<script>alert('$client');</script>";
$stmt = $mysqli->prepare($db,"SELECT pcode FROM tbl_project WHERE pclient=$client");
$stmt->bind_param("s", $client);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
?>
<option value="">Select Project Code</option>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row["pcode"];?>"><?php echo $row["pcode"];?></option>
<?php
}
?>
Sponsor our Newsletter | Privacy Policy | Terms of Service