Member Avatar for anmol.raghuvanshi1

here is my code i want that what ever value is checked that value is inserted into database
i have created database with name doctor table name diseases with 3 field id dis_name and Ans

plz help its urgent

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

    require 'db.php';

       if ( !empty($_POST)) {
        // keep track validation errors
        $disError = null;
        $ansError = null;

        // keep track post values
        $dis_name = $_POST['name'];
        $ans=isset($_POST['ans']);


        // validate input
        $valid = true;
        if (empty($dis_name)) {
            $disError = 'Please enter Diseases Name';
            $valid = false;
        }

        if (empty($ans)) {
            $ansError = 'Please check one of option';
            $valid = false;
        }


        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO diseases (dis_name,Ans) values(?,?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($dis_name,$ans));
            Database::disconnect();
            //header("location: diseases.php"); 
        }
    }
?>

// some html code

<div class="control-group <?php echo !empty($ansError)?'error':'';?>">
                          <label class="check">Have you suffered pain preiviously:</label>
                          <div class="controls">
                          <input type="radio" name="choice[]"<?php if (isset($ans) && $ans=="Yes") echo "checked";?>
                            value="Yes">Yes
                              <input type="radio" name="choice[]"<?php if (isset($ans) && $ans=="No") echo "checked";?>
                                 value="No">No
                                 <?php include 'loaded.php'; ?>
                    </div>
                     </div>

// loaded.php






<?php
if(isset($_POST['submit'])){
if(!empty($_POST['choice[]'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['choice[]']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['choice[]'] as $selected) {
echo "<p>".$selected ."</p>";
$qry="insert into diseases (Ans) values ($selected)";
}
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}
?>

Recommended Answers

All 5 Replies

If you want to save multiple values then use checkboxes not radio buttons, from a group of radio buttons you must get only one value, assign the same name but different value:

<input type="radio" name="pain" value="1"> Headache
<input type="radio" name="pain" value="2"> Acidity
<input type="radio" name="pain" value="3"> It's lupus

Then in your PHP side simply use $_POST['pain'] to access the selected value.

A part this, in your code I see the query statement at line 73, but I do not see the query execution, so where is that part?

Member Avatar for anmol.raghuvanshi1

sir what is missing this is only i have plz help what is missing

I've already wrote what is missing. From line 71 to 74, you wrote:

foreach($_POST['choice[]'] as $selected) {
    echo "<p>".$selected ."</p>";
    $qry="insert into diseases (Ans) values ($selected)";
}

Basically you have to repeat the same code you have already used between lines 33 and 38, i.e. you have to prepare and execute the query:

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO diseases (Ans) values(?)";
$q   = $pdo->prepare($sql);

$q->execute(array($_POST['pain']));

Database::disconnect();

If you want to use a radio button to choose the value then you do not need an array to save the selected values, because with radio you can choose only one, so you don't even need a loop.

If instead you wanted to perform multiple inserts then the loop would go around $q->execute(array($selected)):

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO diseases (Ans) values(?)";
$q   = $pdo->prepare($sql);

foreach($_POST['choice[]'] as $selected)
{
    $q->execute(array($selected));
}

Database::disconnect();

But at that point convert your radio buttons to checkboxes or a multi-select list.

Member Avatar for anmol.raghuvanshi1

sir,problem is nearly solved thnks for that but problem occuring is it is putting value 1 crossponding to yes or no in ans coloumn ..

You can do:

$ans = array_key_exists('choice', $_POST) && $_POST['choice'] == 'Yes' ? 1 : 0;

so in case of Yes the value of $ans will be 1, in all other cases will be 0. If you need something more specific then use IF/ELSEIF statements:

Best solution, for me, is to use a checkbox, because if this is not checked then it's not sent with the POST request, so you have only to verify if it's received, without dealing with the value:

<p>Have you suffered pain preiviously:</p>
<input type="checkbox" name="choice" value="1"> <label for="choice">Yes</label>

Then in PHP you simply check if you've got the checkbox:

$ans = array_key_exists('choice', $_POST) ? 1 : 0;

Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.