insert data into $db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

insert data into $db

Post by Think Pink »

hello.
pls help with the following problem.

I have a form (2 dynamic dropdowns) that after i select data in both, and press submit, the values should be inserted into $db.

When I chhose the Country, the page refreshes, and according to the Country i choosed the Cities are loaded into dropdown Cityes.

The problem is after the page refreshes, because the data is automaticaly inserted into $db, and I want that data to be inserted in the $db only when I click "Submit"

here is the script (short version) but this is the data that refers to what I want.

Code: Select all

<form name="form1" action="<? $PHP_SELF; ?>" method="post">
<b>Country</b><br>
<select name="selCountry" onChange="document.formsї0].submit();">
<option value="0">Choose Country</option>

Code: Select all

$sqlCountry = "SELECT * FROM Country ORDER BY nume_Country ASC";
$resultCountry = mysql_query($sqlCountry);
while($row = mysql_fetch_array($resultCountry)) { 
print "<option value='".$row['id']."'"; 
if($_POST['selCountry']==$row['id']) echo "selected"; 
print "> - ".$row['nume_Country']."</option>"; 
}

Code: Select all

&lt;/select&gt;
&lt;br&gt;&lt;br&gt;
&lt;b&gt;City&lt;/b&gt;&lt;br&gt;
&lt;select name="selCity" class="form1"&gt;
&lt;option selected value="0"&gt;Choose City&lt;/option&gt;

Code: Select all

$sqlCity = "SELECT * FROM City WHERE id_Country='".$_POST['selCountry']."'";
$resultCity = mysql_query($sqlCity);
while($row = mysql_fetch_array($resultCity))	{
print "<option value='".$row['id']."'> - ".$row['nume_City']."</option>";
}

Code: Select all

&lt;/select&gt;
&lt;br&gt;&lt;br&gt;
&lt;input type="submit" name="add_Country_City"&gt;
&lt;/form&gt;

Code: Select all

$message = "";
if(isset($_POST['add_Country_City'])) {
$message = "error";
} else {
$sql = "INSERT INTO o_gen(id_Country, id_City) VALUES('".$_POST['selCountry']."', '".$_POST['selCity']."')";
mysql_query($sql);
$message = "Added succesfully";
}
i hope you wont have troubles understanding what i want. I have dificulties explaining in english, but I hope I'll make you understand.
THX in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

tell the script to only insert data when both are non-zero..
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

insert data into $db

Post by Think Pink »

thx
heere is the solution

Code: Select all

<?php
..................					
if(isset($_POST['selCountry']) || $_POST['selCity']== "0") {
..................
}
?>
Post Reply