简体   繁体   中英

HTML form data not inserting into MySQL using xampp

I know this question has been asked before, and I looked at many different solutions. But please still help. I will cut to the chase, I am trying to insert data taken from a survey into my SQL database. EDIT 1: I figured out how to prevent the unidentified array. I had just specified where data should go to. See the php below. EDIT 2: Thank you all so much for the feedback. I only have one problem left.

Say if a user selects multiple checkboxes. How can I get the values from check buttons to appear within the same column?

Ex: yes

20-40

Pfizer

Pain, Swelling

Chills, Fever, Nausea

I'm not sure where the problem is as I am just beginning this project after following some tutorials and going through w3schools. Here's the HTML form

<body>
<h1>Covid Vaccine Experience Survey</h1>

<!-- Create Form  "action" will send the submit button to the result page-->
<form id="form" action="site.php" method="post"> 
    
    <div class="form-control">
        <label>
            1. *This Survey is Only for Vaccinated Particapants*
            Are you Vaccinated?
        </label>

        <!-- Input Type Radio Button -->
        <label for="answer-1">
            <input type="radio"
                   id="answer-1"
                   name="answer">Yes</input>
        </label>
        <label for="answer-2">
            <input type="radio"
                   id="answer-2"
                   name="answer">No</input>
        </label>
    </div>

    <div class="form-control">
        <label>
            2. What is you're Age Group?
        </label>

        <!--Input Type Radio Button -->
        <label for="agegroup-1">
            <input type="radio"
                   id="agegroup-1"
                   name="agegroup" required>5-12</input>
        </label>
        <label for="agegroup-2">
            <input type="radio"
                   id="agegroup-2"
                   name="agegroup">12-19</input>
        </label>
        <label for="agegroup-3">
            <input type="radio"
                   id="agegroup-3"
                   name="agegroup">20-40</input>
        </label>
        <label for="agegroup-4">
            <input type="radio"
                   id="agegroup-4"
                   name="agegroup">50-69</input>
        </label>
        <label for="agegroup-5">
            <input type="radio"
                   id="agegroup-5"
                   name="agegroup">70+</input>
        </label>
    </div>
    
    <div class="form-control">
        <label for="Vaccine">
            3. Select Vaccine type:
            <small>(Click the Dropdown icon for more vaccines)</small>
        </label>

        <!--Input Type Dropdown -->
        <select name = "vaccines" id="vaccines">
            <option value="pfizer">Pfizer</option>
            <option value="maderna">Maderna</option>
            <option value="johnsone">Johnsone</option>
        </select>
    </div>

    <div class="form-control">
        <label>
        4. Physical Side Effects Reported
        <small>(Check all that apply)</small>
        </label>
        <!-- Input Type Checkbox -->
        <label for="inp-1">
            <input type="checkbox"
                   name="inp" value="Pain" onclick="return PhysicalSideEffects();">Pain</input>
                </label>
        <label for="inp-2">
            <input type="checkbox"
                   name="inp"value="Swelling" onclick="return PhysicalSideEffects();">Swelling</input>
                </label>
        <label for="inp-3">
            <input type="checkbox"
                   name="inp"value="Redness" onclick="return PhysicalSideEffects();">Redness</input>
                </label>
        <label for="inp-4">
            <input type="checkbox"
                   name="inp"value="Rash" onclick="return PhysicalSideEffects();">Rash</input>
                </label>
        <label for="inp-5">
            <input type="checkbox"
                   name="inp"value="None" onclick="return PhysicalSideEffects();">None</input>
                </label>
    </div>
    <div class="form-control">
        <label>
            5. Illness Side Effects Reported
            <small>(Check all that apply)</small>
        </label>
        <!--Input Type Checkbox -->
        <label for="inp-1">
            <input type="checkbox"
                   name="inps" value="Headache" onclick="return IllnessSideEffects();">Headache</input>
                </label>
        <label for="inp-2">
            <input type="checkbox"
                   name="inps"value="Chills" onclick="return IllnessSideEffects();">Chills</input>
                </label>
        <label for="inp-3">
            <input type="checkbox"
                   name="inps"value="Dirrahea" onclick="return IllnessSideEffects();">Dirrahea</input>
                </label>
        <label for="inp-4">
            <input type="checkbox"
                   name="inps"value="Tiredness" onclick="return IllnessSideEffects();">Tiredness</input>
                </label>
        <label for="inp-5">
            <input type="checkbox"
                   name="inps"value="Fever" onclick="return IllnessSideEffects();">Fever</input>
                </label>
        <label for="inp-6">
            <input type="checkbox"
                   name="inps"value="Dizziness" onclick="return IllnessSideEffects();">Dizziness</input>
                </label>
        <label for="inp-7">
            <input type="checkbox"
                   name="inps"value="Nausea" onclick="return IllnessSideEffects();">Nausea</input>
                </label>
        <label for="inp-8">
            <input type="checkbox"
                   name="inps"value="None" onclick="return IllnessSideEffects();">None</input>
                </label>
    </div>
    <input type="submit" value="Submit" onclick="return confirm('6. Are you sure what you have is true to your knowledge?')">
    <!-- This function is *figure this out*(to check whether or  not the user selected an illness) & *works* make sure that they do not select another ilness if they selected "None"-->
    <form>
        <script type="text/javascript">  
        
            function PhysicalSideEffects()  
            {  
                var checkboxes = document.getElementsByName("inp");  // Gets the the different checkboxes 
                var numberOfCheckedItems = 0;  //Holder for number of checked items
                for(var i = 0; i < checkboxes.length; i++)  //Loops through counting how many are checked
                {  
                    if(checkboxes[i].checked)  
                        numberOfCheckedItems++;  
                }  
                if(checkboxes[4].checked && (checkboxes[0].checked || checkboxes[1].checked || checkboxes[2].checked || checkboxes[3].checked))  
                {  
                    alert("You can't select another Illness if you selected None!");  
                    return false;  
                }
               
            } 
            
            function IllnessSideEffects()
            {
                var checkboxes= document.getElementsByName("inps");
                var num = 0;
                for(var j = 0; j < checkboxes.length; j++)
                {

                    if(checkboxes[j].checked)
                        num++;
                }
                if(checkboxes[7].checked && (checkboxes[0].checked || checkboxes[1].checked || checkboxes[2].checked || checkboxes[3].checked
                || checkboxes[4].checked || checkboxes[5].checked || checkboxes[6].checked))
                {
                    alert("You can't select another Illness if you selected None!");  
                    return false;
                }
            }
        
            </script>
           

Here is the newer PHP code

<?php

    $conn = new mysqli("localhost","root","","csurv");
    //connection established

    if($conn === false){
        die("ERROR: Could not connect. " 
            . mysqli_connect_error());
    }

    $answer = $_POST['answer'];
    $agegroup = $_POST['agegroup'];
    $vaccines = $_POST['vaccines'];
    $inp = $_POST['inp'];
    $inps = $_POST['inps'];

    $sql = "INSERT INTO submisiion (qualified, agegroup, vaccinetype, physsymptoms, illsymptoms)
            VALUES('$answer','$agegroup','$vaccines','$inp','$inps')";
    
    if(mysqli_query($conn, $sql)){
        echo "<h3>data stored in a database successfully." 
            . " Please browse your localhost php my admin" 
            . " to view the updated data</h3>"; 

        echo nl2br("\n$answer\n $agegroup\n "
            . "$vaccines\n $inp\n $inps");
    } else{
        echo "ERROR: Hush! Sorry $sql. " 
            . mysqli_error($conn);
    }
    
    // Close connection
    mysqli_close($conn);

    ?>

Please help. Looks like I'm too dumb for this.

Follow the error message, then test the point. (゚∀゚) In your case, you need to check the line 20 in "C:\xampp\htdocs\site.php" if it's really get the post data "qualified". You can dump variable $_POST to check if the key is typed correct.

If no problem, it still show this error, go to html page, press key "F12", click "network", when you submit the form, you should see the network-package in there, click it and check if datas in that is in expected.

( ・ὢ・) If you don't see the the correct datas in that, then the problem probably is in html page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM