0

I have three check box and three input field something like this,

        <input type="checkbox" name="fruit" value="orange" >
        <input type="text" name="orange" >Orange
        <input type="checkbox" name="fruit" value="apple" >
        <input type="text" name="n" >Apple<br/>
        <input type="checkbox" name="fruit" value="mango" >
        <input type="text" name="mango" >Mango

I have a mysql table named fruits with four column named id, apple, orange, mango. I want to do something like if user select orange , apple , mango (checkboxes) I want to insert data into orange , apple , mango in the table from the three input field , or if user select two user check box it will just insert data into two columns from the input field's value, or if just one check box is selected then insert into one column! what is the standard procedure to do that? Thanks in advance

8
  • 1
    Use conditional statements. php.net/manual/en/control-structures.if.php Commented Feb 21, 2015 at 16:42
  • how if (isset($_POST['fruit'])) {} Commented Feb 21, 2015 at 16:46
  • 1
    That's the general idea. Checkboxes, well you can use a foreach and implode. Commented Feb 21, 2015 at 16:49
  • 1
    You have an answer below. Commented Feb 21, 2015 at 17:01
  • 1
    By the way, you need to treat your checkboxes as an array. Commented Feb 21, 2015 at 17:12

2 Answers 2

2

Only checked checkboxes are sent, so use isset:

if (isset($_POST['mango']) || isset($_POST['apple']) || isset($_POST['orange'])) {
    $query = 'INSERT INTO table VALUES (orange, apple, mango) VALUES (';
    $query .= (isset($_POST['orange']) ? 1 : '') . ',';
    $query .= (isset($_POST['apple'])  ? 1 : '') . ',';
    $query .= (isset($_POST['mango'])  ? 1 : ''); 
    $query .= ')';
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hm... After the way I got grilled by the OP, I wish you all the best Panther ;-)
there are some errors in your code , I fixed it but not working the way it supposed to!
@monir009: what errors you mean? What my code do and what should it do?
well its basic syntax error though : syntax error, unexpected ')' you haven't close parenthesis properly in few lines like so isset($_POST['orange']) ? 1 : '' . ',';
and wrote values twice :)
|
0

It worked for me.

My Code:

HTML

 <form  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="checkbox" name="fruit[]" value="orange" >Orange
<input type="checkbox" name="fruit[]" value="apple" >Apple
<input type="checkbox" name="fruit[]" value="mango" >Mango
<input type="submit" value="Insert into Database">
</form>

PHP

 <?php 

// define variables and set to empty values
 $fruits = array();
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
  $fruits = test_input($_POST);
}
function test_input($data) 
 {
    $servername = "";// localhost, server name
    $username = "";// username
    $password = "";//password
    $dbname = "";//database name

   // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    for($i =0; $i < sizeof($data["fruit"]);  $i++)
    {
        //select query where count > 0 and fruitname = $data["fruit"]
        //If its first time, Select query returns false
        // write Insert into db table values(fruitname = $data["fruit"] , count =1)

    // if its exist value, select query returns count value,
    // update query set value = value+ 1 where   fruitname = $data["fruit"]
   }
}

?>

1 Comment

Try this code, give your database query and execute the query, If you using any other database and want how to write query Follow this links, Select last id

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.