I'm trying to assign multiple value to option in php so i can extract them later and separately and insert to mysql
where is how i'm trying to assign.
<select name="val[parent]">
<?php
if($cnt > 0){
while($parent = mysqli_fetch_assoc($sql))
{
?>
<option value="<?php echo $parent['category_id']. "-" . $parent['trade_id'];?> "> <?php echo $parent['trade_name']; ?> </option>
<?php }} ?>
</select>
and this is how i'm trying to extract them
if(isset($_POST['val']))
{
list($category_id, $trade_id) = explode("-", $_POST['val'], 2);
$aVals = $_POST['val'];
$category_id = 0;
$category_name = $aVals['category_name'];
$parent = $aVals['parent'];
$trade_id =$aVals['trade_id'];
if(isset($aVals['category_id']))
{
$category_id = $aVals['category_id'];
}
$sql = "INSERT INTO category SET category_name = '$category_name', parent = $parent, vcat_id = '$trade_id', status = 1";
$sql = mysqli_query($databaseLink,$sql);
header('location:category.php');
}
I dont know how i can extract them assign them. Please help.
Here is the fullform
<?php
require_once "connection.php";
$bEdit = false;
if(isset($_POST['val']))
{
$Value = explode("-",$_POST['val']['parent']);
$aVals = $_POST['val'];
$category_id = 0;
$category_name = $aVals['category_name'];
$parent = $aVals['parent'];
$trade_id = $Value[1];
if(isset($aVals['category_id']))
{
$category_id = $aVals['category_id'];
}
$sql = "INSERT INTO category SET category_name = '$category_name', parent = $parent, vcat_id = '$trade_id', status = 1";
$sql = mysqli_query($databaseLink,$sql);
header('location:category.php');
}
?>
<form role="form" method="post">
<table cellpadding="5" cellspacing="5">
<tr><td>
<h2><?php echo $bEdit ? 'Edit' : 'Add '; ?> Category</h2>
<?php if($bEdit){ ?>
<input type="hidden" name="val[category_id]" value="<?php echo $aRow['category_id']; ?>">
<?php } ?>
</td></tr>
<tr><td>
Select Parent : <br />
<?php
$sql = mysqli_query($databaseLink,"SELECT * FROM `category` WHERE 1 ");
$cnt = mysqli_num_rows($sql);
?>
<select name="val[parent]">
<?php
if($cnt > 0){
while($parent = mysqli_fetch_assoc($sql))
{
?>
<option value="<?php echo $parent['category_id']. "-" . $parent['trade_id'];?> "> <?php echo $parent['trade_name']; ?> </option>
<?php }} ?>
</select>
</td></tr>
<tr><td>
Name : <br />
<input class="form-control" placeholder="Name" name="val[category_name]" type="text" autofocus required value="<?php echo $bEdit ? $aRow['name'] : ''; ?>">
</td></tr>
<tr><td><br />
<button type="submit" class="btn btn-lg btn-success btn-block">Save</button>
</td></tr>
</table>
</form>
multipleto your select box.<select multiple></select>, if you want the user to select multiple options.$Value = explode("-",$_POST['val']['parent']), then assign$CategoryID = $Value[0]&$TradeID = $Value[1]?list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2);It's a bit difficult to troubleshoot without seeing your full form. Tryprint_r($_POST), to see what you are actually posting to your PHP.