Probleme selected PHP

Hello everyone,
I have a small problem in my php script
In my select I have two types of request, “Show all” that displays all the marks and the other as a parameter query that takes the brand chosen.
When I chose to deal with brand it works, and when I selected after dealing with “Show all”
It works.
What I wanted to make is that the first time, “show all” market will be
Here’s the script:

You have this:

<option ><?php echo "show all" ?></option>

Where there’s no reason to go into PHP mode instead of just writing directly

<option>show all</option>

Anyway, to make that selected by default

<option <?php if (!isset($idr)) echo 'selected="selected"'; ?>>show all</option>

I have no idea where $idr comes from, but that’s the variable you’re using everywhere, so I will assume there’s some magic populating it.

Once you get this part working, please rewrite the whole thing to stop relying on register_globals (disabled by default and deprecated for years) and don’t allow trivial SQL injection attacks on your code.

you can download the entire script?

Pardon me, while I Bogart your code…

Here is the proper way to code this, more or less.

&lt;?php 
require_once('connect.php');
connect();
if (!empty($_REQUEST['idr'])) {
	$idr = trim(htmlspecialchars(strip_tags($_REQUEST['idr'])));
}
$html = '
	&lt;html&gt;
	&lt;head&gt;
	&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
	&lt;/head&gt;
	&lt;body&gt;
	&lt;form action="nchaneteste.php" method="post" id="formulaire"&gt;
	&lt;strong&gt;Sélectionner Make:&lt;/strong&gt;
		&lt;select selected="show all" name="make" id="make" onchange="document.forms[\\'formulaire\\'].submit();"&gt; 
			&lt;option&gt;Show all&lt;/option&gt;
';

$sql = 'SELECT DISTINCT(make) FROM `cars` ORDER BY `make` LIMIT 30'; 
$req = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($req) == 0) {
	die('It ain\\'t got no gas, mhmmm.');
}
while($row = mysql_fetch_assoc($req)) {
	$make = stripslashes($row['make']);
	if ($idr == $make) {
		$addMe .= ' selected="selected"';
	} else {
		$addMe .= '';
	}
	$html .= '&lt;option  value="'.$make.'"'.$addMe.'&gt;'.$make.'&lt;/option&gt;';
}
if ($idr != 'Show all' && $idr != '-1') {
      	$select = 'SELECT * FROM `cars` WHERE `make` = "'. $idr .'" ORDER BY `colour` LIMIT 30'; 
      	$result = mysql_query($select) or die(mysql_error());
      	// this line can be deleted below, if afficher_cars.php does not use the $total variable 
      	$total = mysql_num_rows($result);
} else {
      	$select = 'SELECT * FROM `cars` ORDER BY `car_id` LIMIT 30';
      	$result = mysql_query($select) or die(mysql_error());
      	// this line can be deleted below, if afficher_cars.php does not use the $total variable 
      	$total = mysql_num_rows($result);
}
require_once('afficher_cars.php');
$html .= '	
	&lt;/form&gt;
	&lt;/body&gt;
	&lt;/html&gt;
';
echo $html;
?&gt;

Notes:

  1. Do not use mysql_fetch_array() unless you need to use an associative array along with a numeric array. Either use mysql_fetch_assoc() for associative or mysql_fetch_row() for numeric. mysql_fetch_array() created a numeric and an associative array.

  2. Do not use mysql_free_result() unless you are really sucking up the RAM. In this case, you aren’t. Using mysql_free_result() uses resources in order to free resources and in this case, just not worth the overhead.

  3. Do not wrap your strings in double quotes, it causes more overhead as that PHP reads through it to figure out what is a string and what is a variable. It also keeps you from having to backslashe all of your quotation marks.

  4. Do not use $_SERVER[‘PHP_SELF’] unless the script name will change often for some odd reason.

  5. Store data intoa variable and ech when you need it. This makes the software more scalable and allows the data to be buffered into cache files or output filters.

  6. Do not use a while loop to take data from an array only to put it back into an array and loop a second time with foreach just to get it out again. Your original script converts the array to strings and then strings to array and then to strings again. Just silly.

  7. Do not use isset(). It does not check for empty strings. Instead use empty(). empty() checks for null values and is superior to isset().

  8. You check for passed variables at the beginning of the code, not 3 or 4 times throughout the code. Check for it, validate it, sterilize it and convert it nto a variable at the beginning.

  9. Try to never SELECT * in queries. By selecting specific columns you can create a MySQL INDEX for those specific columns to increase data selection performance and lighten the server load.

<h1>the Car Market</h1>
<table border=“1” cellpadding=“2” cellspacing=“0” width=“640”>
<tbody><tr>

&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Year&lt;/th&gt;
&lt;th&gt;&lt;form id="frm_make" name="frm_make" method="get" action="index.php"&gt;Make | &lt;select name="make" id="make" onchange="document.forms['frm_make'].submit();"&gt;&lt;option value="show all"&gt;show all&lt;/option&gt;&lt;option value="aaa"&gt;aaa&lt;/option&gt;&lt;option value="BM"&gt;BM&lt;/option&gt;&lt;option value="BMW"&gt;BMW&lt;/option&gt;&lt;option value="Dodge"&gt;Dodge&lt;/option&gt;&lt;option value="Honda"&gt;Honda&lt;/option&gt;&lt;option value="Mazda"&gt;Mazda&lt;/option&gt;&lt;option value="Mercedes"&gt;Mercedes&lt;/option&gt;&lt;option value="Toyota"&gt;Toyota&lt;/option&gt;&lt;option value="Volvo"&gt;Volvo&lt;/option&gt;&lt;/select&gt;&lt;/form&gt;&lt;/th&gt;

&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;&lt;form id="frm_colour" name="frm_colour" method="get" action="index.php"&gt;Make | &lt;select name="colour" id="colour" onchange="document.forms['frm_colour'].submit();"&gt;&lt;option value="show all"&gt;show all&lt;/option&gt;&lt;option value="aaaa"&gt;aaaa&lt;/option&gt;&lt;option value="Black"&gt;Black&lt;/option&gt;&lt;option value="Blue"&gt;Blue&lt;/option&gt;&lt;option value="Gold"&gt;Gold&lt;/option&gt;&lt;option value="green"&gt;green&lt;/option&gt;&lt;option value="Red"&gt;Red&lt;/option&gt;&lt;option value="Silver"&gt;Silver&lt;/option&gt;&lt;option value="White"&gt;White&lt;/option&gt;&lt;/select&gt;&lt;/form&gt;&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;

</tr>

<tr>
<td>1</td>
<td>2004</td>
<td>BMW</td>
<td>323</td>

&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;$19222.25&lt;/td&gt;

</tr>

<tr>
<td>2</td>
<td>2000</td>
<td>Mercedes</td>

&lt;td&gt;C230&lt;/td&gt;
&lt;td&gt;Silver&lt;/td&gt;
&lt;td&gt;$10001.95&lt;/td&gt;

</tr>

<tr>
<td>3</td>
<td>1990</td>

&lt;td&gt;Mercedes&lt;/td&gt;
&lt;td&gt;190e&lt;/td&gt;
&lt;td&gt;Gold&lt;/td&gt;
&lt;td&gt;$5445.95&lt;/td&gt;