0

I'm stumped. I've looked this up on multiple answers on Stackoverflow, but just can't get it. Maybe I'm just not seeing something.

I'm making a Family Feud game and using PHP and MySQL databases to store and retrieve information. For the Fast Money Round, I have a database with a Table called "FastMoney1" I'm using an HTML5 form and PHP to post the data in the form to that table, which has two columns: answer and score

I'm running my query through a for loop, but it's not posting anything to the table. I'm wondering what I'm doing wrong.

HTML:

<form method="post" class="form-horizontal">
    <div class="form-group">
        <div class="col-xs-9">
            <input type="text" class="form-control" id="question1answer" name="answer[0]" placeholder="Question 1">
        </div>
        <div class="col-xs-3">
            <input type="number" class="form-control" id="question1score" name="score[0]" placeholder="0">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-9">
            <input type="text" class="form-control" id="question1answer" name="answer[1]" placeholder="Question 2">
        </div>
        <div class="col-xs-3">
            <input type="number" class="form-control" id="question1score" name="score[1]" placeholder="0">
        </div>
    </div>
    <div class="col-xs-4 col-xs-offset-4" align="center">
        <input type="submit" class="btn btn-success" name="Submit" />
    </div>
</form>

PHP:

<?php

    if(isset($_POST['submit'])){

        require "config.php";

        for ($i = 0; $i<count($_POST); $i++){
            $answer = $_POST['answer'][$i];
            $score = $_POST['score'][$i];
            $sql = "INSERT INTO `fastMoney1`(`answer`, `score`) VALUES ('$answer','$score')";
            if ($conn->query($sql) === TRUE) {
                echo "";
            } else {
                echo $conn->error;
            }
        }

        $conn->close();
        echo "<meta http-equiv='refresh' content='0'>";

    }

?>

All of this lives on the same PHP page, which is why I do not have an action attached to the form.

The config.php is an include that calls the host, username, password and database and opens the connection

2
  • Are you getting any error? Commented Aug 5, 2017 at 6:21
  • No, I am not... Commented Aug 5, 2017 at 6:22

3 Answers 3

2

Remember that PHP variables as case sensitive you have given name Submit in form while in php you are checking if(isset($_POST['submit'])){ which never become true. change it to

if(isset($_POST['Submit'])){ //<----- S in upper case

EDIT You also need to change your loop to

for ($i = 0; $i<count($_POST['answer']); $i++){
Sign up to request clarification or add additional context in comments.

1 Comment

I actually have a total of 6 pairs of fields, answer and score - but the submit is only posting the first three to the database, not all six.
-1

see your sql statement you don't need those ampersand in table name and column names INSERT INTO fastMoney1(answer, score) VALUES ('$answer','$score')

Comments

-2
 <input type="submit" class="btn btn-success" name="submit" />

i changed

 name="Submit"

to

 name="submit"

"Submit" to "submit" ----->"S" to "s"

and it works fine

1 Comment

I stated why in the issue. because I have the isset($_POST['submit']) AND the php and html live on the same page

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.