1

Here is my code, I am trying to add multi items in database, in these 5 items my one item is default, when I am checked on Item no 3, In database item 3 value save as 0 and 1st item value save as 1.

This is not correct, I want that when Item 3 is checked than after submit form than in database Item 3 save as 1 else save value 0.

Please any one tell me how to this work

<?php

if (isset($_POST['action'])) {
    $prods = count($_POST['prod']);

    for ($i = 0; $i < $prods; $i++) {
        $prod = $_POST['prod'][$i];
        $default = (!empty($_POST['is'][$i])) ? 1 : 0;
        mysql_query("INSERT INTO `items` (`prod`, `is_default`)VALUES('{$prod}', '{$default}')");
    }
}
?>

<form action="" method="post">
    <input type="hidden" name="action" value="add">

    <span>Item Name</span> <span style="padding-left:100px">Is default</span> <br>

    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="1">Item 1<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="1">Item 2<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="1">Item 3<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="1">Item 4<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="1">Item 5<br>

    <input type="submit" value="Save">
</form>
1

1 Answer 1

1

First of all, change your checkbox value attributes like this:

value="0", value="1", value="2" etc.

And then during form processing, get the $default value like this:

$default = (isset($_POST['is'][0]) && $_POST['is'][0] == $i) ? 1 : 0;

So your code should be like this:

HTML:

<form action="" method="post">
    <input type="hidden" name="action" value="add">

    <span>Item Name</span>  <span style="padding-left:100px">Is default</span>  <br>

    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="0"> Item 1<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="1">Item 2<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="2">Item 3<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="3">Item 4<br>
    <input type="text" name="prod[]" value="">
    <input type="radio" name="is[]" value="4">Item 5<br>

    <input type="submit" value="Save">
</form>

PHP:

if(isset($_POST['action'])){
    $prods = count($_POST['prod']);

    for($i=0; $i < $prods; $i++){
        $prod = $_POST['prod'][$i];
        $default = (isset($_POST['is'][0]) && $_POST['is'][0] == $i) ? 1 : 0;
        $query = "INSERT INTO `items` (`prod`, `is_default`)VALUES('{$prod}', '{$default}')";
        if(mysql_query($query)){
            // success
        }else{
            // error
        }
    }

}

Sidenotes:

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.