onlinespider
New Member
Dear All,
I wish to have 2 drop down boxes, Country Select Box and Locality Select Box. The locality select box will be affected by the value chosen in the country select box.
All is working fine except that the locality select box is not being populated.
I know that the problem is in the sql statement
WHERE country_id='$co'
because i am having an error that $co is an undefined variable.
All the rest works fine because i have replaced the $co variable directly with a number (say 98) for a particular country id and it worked fine. In what way can i define this variable $co so that it is accepted by my sql statement?
Thank you for your help in advance.
MySQL Tables indicated below:
CREATE TABLE countries(
country_id INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
country_name VARCHAR(30) NOT NULL,
PRIMARY KEY(country_id),
UNIQUE KEY(country_name),
INDEX(country_id),
INDEX(country_name))
ENGINE=MyISAM;
CREATE TABLE localities(
locality_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
country_id INT(3) UNSIGNED NOT NULL,
locality_name VARCHAR(50),
PRIMARY KEY (locality_id),
INDEX (country_id),
INDEX (locality_name))
ENGINE=MyISAM;
Extract PHP script included below:
// connect to database
require_once(MYSQL);
if(isset($_POST['submitted']))
{
// trim the incoming data
/* this line runs every element in $_POST through the trim() function, and assigns the returned result to the new $trimmed array */
$trimmed=array_map('trim',$_POST);
// clean the data
$co=mysqli_real_escape_string($dbc,$trimmed['country']);
$lc=mysqli_real_escape_string($dbc,$trimmed['locality']);
}
?>
<form action="form.php" method="post">
<p>Country
<select name="country">
<option>Select Country</option>
<?php
$q="SELECT country_id, country_name
FROM countries";
$r=mysqli_query($dbc,$q) or
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while($row=mysqli_fetch_array($r))
{
$country_id=$row[0];
$country_name=$row[1];
echo '<option value="' . $country_id . '"';
if(isset($trimmed['country']) && ($trimmed['country']==$country_id))
echo 'selected="selected"';
echo '>' . $country_name . '</option>\n';
}
?>
</select>
</p>
<p>Locality
<select name="locality">
<option>Select Locality</option>
<?php
$ql="SELECT locality_id, country_id, locality_name
FROM localities
WHERE country_id='$co'
ORDER BY locality_name";
$rl=mysqli_query($dbc,$ql) or
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while($row=mysqli_fetch_array($rl))
{
$locality_id=$row[0];
$country_id=$row[1];
$locality_name=$row[2];
echo '<option value="' . $locality_id . '"';
if(isset($trimmed['locality']) && ($trimmed['locality']==$locality_id))
echo 'selected="selected"';
echo '>' . $locality_name . '</option>\n';
}
// close database connection
mysqli_close($dbc);
?>
</select>
</p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
I wish to have 2 drop down boxes, Country Select Box and Locality Select Box. The locality select box will be affected by the value chosen in the country select box.
All is working fine except that the locality select box is not being populated.
I know that the problem is in the sql statement
WHERE country_id='$co'
because i am having an error that $co is an undefined variable.
All the rest works fine because i have replaced the $co variable directly with a number (say 98) for a particular country id and it worked fine. In what way can i define this variable $co so that it is accepted by my sql statement?
Thank you for your help in advance.
MySQL Tables indicated below:
CREATE TABLE countries(
country_id INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
country_name VARCHAR(30) NOT NULL,
PRIMARY KEY(country_id),
UNIQUE KEY(country_name),
INDEX(country_id),
INDEX(country_name))
ENGINE=MyISAM;
CREATE TABLE localities(
locality_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
country_id INT(3) UNSIGNED NOT NULL,
locality_name VARCHAR(50),
PRIMARY KEY (locality_id),
INDEX (country_id),
INDEX (locality_name))
ENGINE=MyISAM;
Extract PHP script included below:
// connect to database
require_once(MYSQL);
if(isset($_POST['submitted']))
{
// trim the incoming data
/* this line runs every element in $_POST through the trim() function, and assigns the returned result to the new $trimmed array */
$trimmed=array_map('trim',$_POST);
// clean the data
$co=mysqli_real_escape_string($dbc,$trimmed['country']);
$lc=mysqli_real_escape_string($dbc,$trimmed['locality']);
}
?>
<form action="form.php" method="post">
<p>Country
<select name="country">
<option>Select Country</option>
<?php
$q="SELECT country_id, country_name
FROM countries";
$r=mysqli_query($dbc,$q) or
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while($row=mysqli_fetch_array($r))
{
$country_id=$row[0];
$country_name=$row[1];
echo '<option value="' . $country_id . '"';
if(isset($trimmed['country']) && ($trimmed['country']==$country_id))
echo 'selected="selected"';
echo '>' . $country_name . '</option>\n';
}
?>
</select>
</p>
<p>Locality
<select name="locality">
<option>Select Locality</option>
<?php
$ql="SELECT locality_id, country_id, locality_name
FROM localities
WHERE country_id='$co'
ORDER BY locality_name";
$rl=mysqli_query($dbc,$ql) or
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while($row=mysqli_fetch_array($rl))
{
$locality_id=$row[0];
$country_id=$row[1];
$locality_name=$row[2];
echo '<option value="' . $locality_id . '"';
if(isset($trimmed['locality']) && ($trimmed['locality']==$locality_id))
echo 'selected="selected"';
echo '>' . $locality_name . '</option>\n';
}
// close database connection
mysqli_close($dbc);
?>
</select>
</p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>