Skip to content Skip to sidebar Skip to footer

Php - Search Database And Return Results On The Same Page

Many thanks in advance for your time. I am in the process of creating a CRM for my business. It will be pretty basic, but include some key features my industry specifically needs

Solution 1:

Your option could just be written as follows:

echo '<option value="$row[client_id]">$row[firstname] $row[lastname] -$row[city], $row[state]</option>';

Also, note that mysql_ functions has been deprecated and used in the wrong way can be very dangerous, leaving your website vulnerable.

Use prepared statements using mysqli or PDO.

Solution 2:

...
$r_query = mysql_query($sql); 
while ($row = mysql_fetch_array($r_query)) { ?>

    <option value='<?= $row['client_id'];?>'>
         <?=$row['firstname'] . " " . $row['lastname']; ?> - 
         <?=$row['city'] . ", " . $row['state']; ?>
    </option>

<?php  }}  ?>

Or some variation of that. Point is, your PHP doesn't have to be a continuous block, you can close your PHP tag at any time, resume using regular HTML, and then open a new PHP tag and continue in your loop.

Also, in the above example, <?= is shorthand for <?php echo.

Also, in your example, you're using mysql_ functions, which have been deprecated in later versions of PHP5 and removed in PHP7. Best to study up on mysqli_ or PDO (which you can also use with MySQL databases).

Lastly, once you start using either of those, look into prepared statements, which will make your code function better/avoid SQL injections.

Post a Comment for "Php - Search Database And Return Results On The Same Page"