Web Design Forum  
 
Go Back   Web Design Forum > Web Software > Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-07-2012, 03:09 PM   #1
New Member
 
Join Date: Jan 2012
Posts: 1
Default Country SELECT box Affecting Locality Select Box

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>
onlinespider is offline   Reply With Quote


Old 01-09-2012, 12:07 AM   #2
Gold Member
 
conor's Avatar
 
Join Date: Oct 2008
Location: Ireland
Posts: 349
Default

Hi,

I can't see any obvious reasons for this to be happening. A technique I use when trying to diagnose errors like this is to add die statements along the script to see what the status of a variable is at a specific stage in runtime. Before this line:

Code:
$co=mysqli_real_escape_string($dbc,$trimmed['country']);
try adding this, to see what the content of the trimmed array actually is. make sure that the 'country' element actually exists in the array:

Code:
die( print_r( $trimmed ) );
post the output here if you need more help
__________________
Conor
conor is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:19 AM.


Camera Forum - Computer Forum - Web Design Forum

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.