Hi..

I need some help. I have a problem to insert 2 values or variable from drop down list to database at the same time.

i want to insert name_relation into db, at the same time want to insert IcNo into db.

here my code:

<?php
 
	include ("ConnectDB.php");

?>
  <select name = "name_relation" id="name_relation">
    <option value="" selected>Please Select</option>
    <?php
			$strSQL = "SELECT * FROM family_detail WHERE EmployeeNo = '$EmployeeNo'";
			$objQuery = mysql_query($strSQL);
			while($objResuut = mysql_fetch_array($objQuery))
			{
			
			?>
    <option value="<?php echo $objResuut["Name"];?>"> <?php echo $objResuut["Name"]." - ".$objResuut["IcNo"] ." - ".$objResuut["relation"];?></option>
    <?php
			}
			?>
  </select>
 
  <?php
	mysql_close(); 
	
	
?>

Something like below etc etc blah blah

<?php

include ("ConnectDB.php");

if(isset($_POST['submit'])){
foreach($_POST as $key => $value){ //escapes any unwanted characters that can cause injections then also cut the variable off at 25 charaters to perevent the possibility of sql buffer overflows
$_POST['$key'] = mysql_real_escape_string(substr($value,25));
}
$sql="UPDATE family_detail SET some_value='some value', some_value='some_value', some_value='".$_POST['name_relation']."' WHERE EmployeeNo = '".$_POST['EmployeeNo']."'";
mysql_query($sql);   

}else{
?>
<form action="" method="post">
<input type="hidden" name="employeeNo" value="<?php echo $employeeNo;?>" />
<select name="name_relation" id="name_relation">
<option value="" selected>Please Select</option>
<?php
$strSQL = "SELECT * FROM family_detail WHERE EmployeeNo = '$EmployeeNo'";
$objQuery = mysql_query($strSQL);
while($objResuut = mysql_fetch_array($objQuery))
{

?>
<option value="<?php echo $objResuut["Name"];?>"> <?php echo $objResuut["Name"]." - ".$objResuut["IcNo"] ." - ".$objResuut["relation"];?></option>
<?php
}}
?>
</select>
<button type="submit" name="submit" />
<?php
mysql_close();


?>

Thats quick and dirty. You should actually check the employee number and make sure its a int with the function intval and if cut it off at 5 characters. if it's not a intval then make the script fail and block the ip address of the person who is submitting the data because they are obviously playing with the code to try injections.

<option value="<?php echo $objResuut["Name"];?>"> <?php echo $objResuut["Name"]." - ".$objResuut["IcNo"] ." - ".$objResuut["relation"];?></option>

and here in your code is the only value that is going to be passed to the database, and thats not a relation. $objResuut["Name"]; So you might want to adjust that also.

thanks skraps for your kind action..

actually, i want to get name and also IcNo from the drop down list. If user select one of the name from drop down list, it will insert only name and i want to know how drop down list can insert name and IcNo at the same time into db. IcNo and Name are different field name in db. code that i given just now,code for drop down list. Can or not one drop down list having 2 parameter??

Thanks

why you want to add one more field, One thing I feel is that your database is not normalized.

Now if this it is mandatory requirement, then you can do one thing
you must be having action page to insert or update mysql data, while inserting query you join from the table where there is lcno and name

insert into TABLENAME (col1, col2, LCNO, NAME) 
select '{$_POST['col1']}' , '{$_POST['col2']}', '{$_POST['code']}', NAME FROM MASTERTABLE WHERE LCNO='{$_POST['code']}'

Here i assume {$_POST} is dropdown name and master table is table from where you populate dropdown

this is more precise to your case

insert into TABLENAME (col1, col2,  NAME,lcno) 
select '{$_POST['col1']}' , '{$_POST['col2']}', '{$_POST['name_relation']}', lcno FROM family_details WHERE name='{$_POST['name_relation']}'

try this :
$value = $_POST;
list($value1,$value2) = explode("-",$value);

echo $value1;
echo "<br/>";
echo $value2;

<form method='post'>
<select>
<option value="abc-123">abc 123</option>
</select>
<input type='submit'>
</form>

try this :
$value = $_POST;
list($value1,$value2) = explode("-",$value);

echo $value1;
echo "<br/>";
echo $value2;

<form method='post'>
<select name='val'>
<option value="abc-123">abc 123</option>
</select>
<input type='submit'>
</form>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.