0

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>
9
  • In your UI (user interface) you need to add multiple to your select box. <select multiple></select>, if you want the user to select multiple options. Commented Aug 16, 2017 at 9:38
  • Presumably you just need to explode the $Value = explode("-",$_POST['val']['parent']), then assign $CategoryID = $Value[0] & $TradeID = $Value[1] ? Commented Aug 16, 2017 at 9:39
  • let me try that Commented Aug 16, 2017 at 9:40
  • nopes not working. Commented Aug 16, 2017 at 9:47
  • Try list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2); It's a bit difficult to troubleshoot without seeing your full form. Try print_r($_POST), to see what you are actually posting to your PHP. Commented Aug 16, 2017 at 9:50

1 Answer 1

1

From your form, you can assign your PHP variables like so:

if(isset($_POST['val']['parent'])){
    $parent = $_POST['val']['parent'];
    # split the value by "-", and assign to category_id & trade_id
    list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2);
}
if(isset($_POST['val']['category_name'])){
    # set the category_name variable
    $category_name = $_POST['val']['category_name'];
}
if(isset($_POST['val']['category_id'])){
    # override category_id variable
    $category_id = $_POST['val']['category_id'];
}

You may want to tighten up your MySQL query though, it seems it's open to security flaws.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.