0

I'm creating a form wiith more than 20 variables to take the input and add all at one place in database so in the table the all the added value are inserted in only one column $total variable is the variable I took to add all the values as you can see in the php file

HTML code

<!DOCTYPE html>
<html>
<head>
    <title>Sleep Assessment Sccorecard</title>
</head>
<body>
    <style>
        .col-md-7{
            float: left;
            width: 50%;
            padding: 10px;
        }
        .column{
            padding: 10px;
        }
    </style>
    <div class="container">
        <h2 style="text-align: center;">Sleep Assessment Scorecard</h2>
        <hr>
        <form action="insert.php" method="POST">
            <div class="form-row">
                <div class="col form-group">
                    <p>Name:</p>
                    <input type="text" name="name">
                </div>
                <div class="col form-group">
                    <p>Date:</p>
                </div>
            </div>
            <hr>
            <div class="row">
                <div class="col-md-7">
                    <h3>Sleep Part 1</h3>
                </div>
                <div class="column"><h3>Chose From The Following</h3></div>
            </div>
            <hr>
            <div class="row">
                <div class="col-md-7">
                        <h4>On Average, how many hours do you sleep each night?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="hours" value="0">8 or more <br>
                    <input type="radio" name="hours" value="1">Between 7 and 8<br>
                    <input type="radio" name="hours" value="2">Between 5 and 7<br>
                    <input type="radio" name="hours" value="3">Less than 5
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>What time do you usually go to bed? (Recommended: Bedtime before 10pm)</h4>
                </div>
                <div class="column">
                    <input type="radio" name="time" value="0">Before 10 pm<br>
                    <input type="radio" name="time" value="1">Between 10 and 11 pm<br>
                    <input type="radio" name="time" value="2">Between 11 and 12 am<br>
                    <input type="radio" name="time" value="3">After midnight
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>How easily do you fall asleep?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="time_1" value="0">Very easy: As soon as my head hits the pillow<br>
                    <input type="radio" name="time_1" value="1">Easy: Usually within about 10-15 minutes<br>
                    <input type="radio" name="time_1" value="2">Moderate: Usually with 30-45 minutes<br>
                    <input type="radio" name="time_1" value="3">Difficult: Takes more than 1 hour
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>How often do you wake up at night?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="wake_night" value="0">Never<br>
                    <input type="radio" name="wake_night" value="1">Once per Night<br>
                    <input type="radio" name="wake_night" value="2">Twice per Night<br>
                    <input type="radio" name="wake_night" value="3">Never
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>What’s the usual time you get up in the morning?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="wake_morning" value="0">6am or earlier<br>
                    <input type="radio" name="wake_morning" value="1">Between 6 and 7:30am<br>
                    <input type="radio" name="wake_morning" value="2">Between 7:30 and 9am<br>
                    <input type="radio" name="wake_morning" value="3">after 9am
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>How do you feel when you wake up in the morning?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="feel" value="0">Fantastic! I jump up without the alarm, eager to face the day<br>
                    <input type="radio" name="feel" value="1">Good to go, after hitting the snooze button once or so<br>
                    <input type="radio" name="feel" value="2">Kind of groggy, still tired, and needing more sleep<br>
                    <input type="radio" name="feel" value="3">Like I want to go into hibernation for a week
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>How often do you vividly remember your dreams?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="dream" value="0">Every night or most of the time<br>
                    <input type="radio" name="dream" value="1">A few times per week<br>
                    <input type="radio" name="dream" value="2">A few times per month<br>
                    <input type="radio" name="dream" value="3">Rarely or never
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>How many naps do you need to take throughout the day?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="naps" value="0">None<br>
                    <input type="radio" name="naps" value="1">One Nap<br>
                    <input type="radio" name="naps" value="2">Two Naps<br>
                    <input type="radio" name="naps" value="3">Three or more Naps
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>On average, how would you rate the overall quality of your sleep?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="rate" value="0">Excellent<br>
                    <input type="radio" name="rate" value="1">Good<br>
                    <input type="radio" name="rate" value="2">Okay<br>
                    <input type="radio" name="rate" value="3">Poor
                </div>
                <hr>
            </div>
            <div class="row">
                <div class="col-md-7">
                    <h3>Sleep Part 2</h3>
                </div>
                <div class="column"><h3>Chose From The Following</h3></div>
            </div>
            <div class="row">
                <div class="col-md-7">
                    <h4>Do you have an established evening routine/set of habits?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="habits" value="3">No
                    <input type="radio" name="habits" value="0">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you sleep in a darkened room?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="dark" value="3">No
                    <input type="radio" name="dark" value="0">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you have any electronic devices in the room where you sleep?</h4>
                </div>
                <div class="column">
                    <input type="radio" name="device" value="0">No
                    <input type="radio" name="device" value="3">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you awaken to an alarm clock? </h4>
                </div>
                <div class="column">
                    <input type="radio" name="alarm" value="0">No
                    <input type="radio" name="alarm" value="3">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you watch TV or use the computer within an hour before going to sleep? </h4>
                </div>
                <div class="column">
                    <input type="radio" name="computer" value="0">No
                    <input type="radio" name="computer" value="3">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you eat anything within 3 hours of bedtime? </h4>
                </div>
                <div class="column">
                    <input type="radio" name="bedtime" value="0">No
                    <input type="radio" name="bedtime" value="3">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you have problems with insomnia? </h4>
                </div>
                <div class="column">
                    <input type="radio" name="insomnia" value="0">No
                    <input type="radio" name="insomnia" value="3">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>DO you snore? </h4>
                </div>
                <div class="column">
                    <input type="radio" name="snore" value="0">No
                    <input type="radio" name="snore" value="3">Yes
                </div>
                <hr>
                <div class="col-md-7">
                    <h4>Do you use a C-Pap machine? </h4>
                </div>
                <div class="column">
                    <input type="radio" name="machine" value="0">No
                    <input type="radio" name="machine" value="3">Yes
                </div>
                <hr>
            </div>
            <input type="submit" name="Submit">
        </form>
    </div>
</body>
</html>

insert.php

<?php
$name = $_POST['name'];
$hours = $_POST['hours'];
$time = $_POST['time'];
$time_1 = $_POST['time_1'];
$wake_night = $_POST['wake_night'];
$wake_morning = $_POST['wake_morning'];
$feel = $_POST['feel'];
$dream = $_POST['dream'];
$naps = $_POST['naps'];
$rate = $_POST['rate'];
$habits = $_POST['habits'];
$dark = $_POST['dark'];
$device = $_POST['device'];
$alarm = $_POST['alarm'];
$computer = $_POST['computer'];
$bedtime = $_POST['bedtime'];
$insomnia = $_POST['insomnia'];
$snore = $_POST['snore'];
$machine = $_POST['machine'];
echo "$name";
$total = $_POST['hours'] + $_POST['time'] + $_POST['time_1'] + $_POST['wake_night'] + $_POST['wake_morning'] + $_POST['feel'] + $_POST['dream'] + $_POST['naps'] + $_POST['rate'] + $_POST['habits'] + $_POST['dark'] + $_POST['device'] + $_POST['alarm'] + $_POST['computer'] + $_POST['bedtime'] + $_POST['insomnia'] + $_POST['snore'] + $_POST['machine'];

 if (!empty($hours) || !empty($time) || !empty($time_1) || !empty($wake_night) || !empty($wake_morning) || !empty($feel) || !empty($dream) || !empty($naps) || !empty($rate) || !empty($name) || !empty($habits) || !empty($dark) || !empty($device) || !empty($alarm) || !empty($computer) || !empty($bedtime) || !empty($insomnia) || !empty($snore) || !empty($machine)) {

$host = 'localhost';
$user = 'root';
$password = '';
$db = 'sleep_assessment';

$con = new mysqli($host, $user, $password, $db);

if ($con -> connect_error) {
    die("Connectoion Failed: " . $con -> connect_error);
}

//$sql = "INSERT into mydb('name', 'total') VALUES($name, $total)";
$sql = "INSERT INTO `mydb` (`name`, `total`) VALUES ('".$name."', '".$total."')";

if ($con -> query($sql) == TRUE) {
    echo "new record inserted";
}else{
    echo "error: " . $sql . "<br>" . $con -> error;
}

$con ->close();
}
else{
    echo "All options required";
    die();
}

?>

I want to use as less as possible variables to take the inputs I can work on javascript if it helps any solution is welcome thankyou in advance

Bhushan Patil
  • 125
  • 2
  • 12
  • 1
    Don’t worry so much about the number of variables. That’s not a very important metric in most scenarios. Worry more about the fact that you are wide open to [**SQL injection**](https://www.owasp.org/index.php/SQL_Injection). You need to use prepared statements, rather than concatenating variables into your query. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). – elixenide Jul 06 '18 at 06:07
  • 1
    Each variable seems to represent a separate concept and hence deservingly a separate attribute. Do you have any particular concern? – The Lyrist Jul 06 '18 at 06:08
  • If you want to use less variables then why do you extract the variables from the array? Keep them in the POST array and use it from there – Andreas Jul 06 '18 at 06:09
  • I'm unsure about the line starting with `$total` are all those values you add together really integers/numbers? Some of them appear to me to be strings. You can add strings together with `+` – Andreas Jul 06 '18 at 06:11
  • I want to use less variables only to make the php code shorter – Bhushan Patil Jul 06 '18 at 06:13
  • all the values added in `$total` are numbers – Bhushan Patil Jul 06 '18 at 06:14
  • `if (!empty($hours) || !empty($time) || !empty($time_1) || !empty($wake_night) || !empty($wake_morning) || !empty($feel) || !empty($dream) || !empty($naps) || !empty($rate) || !empty($name) || !empty($habits) || !empty($dark) || !empty($device) || !empty($alarm) || !empty($computer) || !empty($bedtime) || !empty($insomnia) || !empty($snore) || !empty($machine)) {` will succeed if ANY option is selected - – mplungjan Jul 06 '18 at 06:17

1 Answers1

2

Here you have my approach of massively reduce the number of variables in your code. This iterates over all post parameters, so if you have a lot more parameters that should not add up total (unlike in your example) better post all numbers as array with html-input name property array notation. You even have some kind of sql-injection prevention for the $total variable because only integers will add up $total so you better sanitize and validate the name property / use prepared statemets

$total = 0;
$name = $_POST["name"];
unset($_POST["name"]);
foreach ($_POST as $key => $value) {
    if(intval($value) > 0){
        $total += intval($value);
    }
}

// insert total to db
john Smith
  • 17,409
  • 11
  • 76
  • 117
  • the value of `$value` will be changing after every radio button the user will be selecting so will be added to the total so will your given method work on the given approach? thankyou in advance for solving my query – Bhushan Patil Jul 06 '18 at 06:21
  • I´m not quite sure what you mean but pretty sure it works with your approach of submitting a form, give it a try ;) – john Smith Jul 06 '18 at 06:24
  • updated the answer, i oversaw your name parameter you also save to db – john Smith Jul 06 '18 at 06:32
  • 1
    Why not just `array_sum($post);` – Andreas Jul 06 '18 at 08:08