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>
luchaninov
  • 6,792
  • 6
  • 60
  • 75
Mr Tom
  • 73
  • 2
  • 12
  • 1
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman May 11 '16 at 21:02

1 Answers1

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:

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37