Jump to content

Update Database Using Select Box


Manhag

Recommended Posts

hello

i am trying to update database using select box value

 

i failed to do that

 

 


<?php
include('lib/db.php');
$id = htmlspecialchars($_GET["fid"]);

$query1 = "SELECT `city` FROM `salaty_fb`.`users` WHERE `id`='$id'";

if ($result1 = mysqli_query($link, $query1)) {

while($result = mysqli_fetch_array($result1))
{
$city_db = $result['city'];

}
//$link->close();
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<div style="<? if (isset($_POST['submit'])) {echo "display:none";} ?>;direction:rtl;" >
<form method="post" action="updatesql.php?fid=<? echo $facebook_id; ?>" target="POPUPW"
onsubmit="POPUPW = window.open('about:blank','POPUPW',
'width=200,height=100');">


<?php
echo "<select name=\"choose_city\">\n";
$choose_city = array('0' => 'Please choose user group', 'eg_cairo' => 'cairo', 'eg_alex' => 'alex', 'eg_giza' => 'giza');
foreach($choose_city as $city_key => $city_realname)
{
echo "<option value=\"$city_key\" name=\"$city_key\" id=\"$city_key\" ";
if($city_key == $city_db)
{
echo " selected";
}
echo ">$city_realname</option>\n";
}
echo "</select><p />";


?>
<input type="submit" name="submit" value="Submit">

<br>
<br>
</form>
</div>
<?php
}
else
{
die(mysqli_error($link));
}

if (isset($_POST['submit']))
{
echo $_POST['city'];
echo "||";

//get value of selected box
$location = $_POST['city_key'];


$query ="UPDATE `salaty_fb`.`users` SET `city`='$location' WHERE `id`='$id'";


if ($result = mysqli_query($link, $query)) {
printf("تم حفظ التحديثات");
echo '<script language="javascript">
<!--
setTimeout("self.close();",1000)
//-->
</script> ';
/* free result set */
// $result->close();
$link->close();
}
else
{
die(mysqli_error($link));
}
}


?>

Link to comment
https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/
Share on other sites

i wana use select box to update database

 

 

// I get id from url and get "city data stored in database

<?php

include('lib/db.php');

$id = htmlspecialchars($_GET["fid"]);

 

$query1 = "SELECT `city` FROM `salaty_fb`.`users` WHERE `id`='$id'";

 

if ($result1 = mysqli_query($link, $query1)) {

 

while($result = mysqli_fetch_array($result1))

{

$city_db = $result['city'];

 

}

//$link->close();

?>

 

 

//intialize form

 

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<div style="<? if (isset($_POST['submit'])) {echo "display:none";} ?>;direction:rtl;" >

<form method="post" action="updatesql.php?fid=<? echo $facebook_id; ?>" target="POPUPW"

onsubmit="POPUPW = window.open('about:blank','POPUPW',

'width=200,height=100');">

 

 

 

 

create select box depending on array

<?php
echo "<select name=\"choose_city\">\n";
$choose_city = array('0' => 'Please choose user group', 'eg_cairo' => 'cairo', 'eg_alex' => 'alex', 'eg_giza' => 'giza');
foreach($choose_city as $city_key => $city_realname)
{
echo "<option value=\"$city_key\" name=\"$city_key\" id=\"$city_key\" ";
if($city_key == $city_db)
{
echo " selected";
}
echo ">$city_realname</option>\n";
}
echo "</select><p />";


?>

 

//end form code

<input type="submit" name="submit" value="Submit">

<br>
<br>
</form>
</div>

 

 

//check database connection and update selected value to database

<?php
}
else
{
die(mysqli_error($link));
}

if (isset($_POST['submit']))
{
echo $_POST['city'];
echo "||";

//get value of selected box
$location = $_POST['city_key'];


$query ="UPDATE `salaty_fb`.`users` SET `city`='$location' WHERE `id`='$id'";

 

 

//if data updated ...use java script pop up to inform user that update was done


if ($result = mysqli_query($link, $query)) {
printf("update done");
echo '<script language="javascript">
<!--
setTimeout("self.close();",1000)
//-->
</script> ';
/* free result set */
// $result->close();
$link->close();
}
else
{
die(mysqli_error($link));
}
}


?>

$location = $_POST['city_key'];

 

That's the problem. city_key does not exist in your form.

 

Only <select> can hold have a name attribute. <option> does not. <option> contains value="" which is what is passed through the form submission.

 

You need to grab the value of the selected option like so:

 

$location = $_POST['choose_city'];

 

Furthermore, you need to sanitize your form data before blindly tossing it into a query. See mysqli_real_escape_string

Then just manipulate your code to ensure the correct values are being placed in the option value:

 

<option value=\"$city_key\">...</option>

 

that was i did

 

echo "<option value=\"$city_key\" name=\"$city_key\" id=\"$city_key\" ";

so i wana to print $city_realname

and store in database $city_key

 

is it clear now ??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.