0

I am setting up a survey to go at the bottom of our FAQ page, I am new to PHP, and this is my first time trying to connect to the database (without a tutorial).

I have the HTML page set up, a PHP set up, and a table set up in MySQL.

The database is creating a new row every time I submit, but all of the rows have "0" instead of the values assigned to the inputs/divs. Please help!

EDIT: I updated the HTML to now be a form, however, when I submit I get a 404 (and the rows do not update at all. What could be wrong?

Here is the HTML:

<?php
    //error_reporting(0);
    require 'db/connect.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
    <head>
        <title>State Requirements Feedback</title>
        <!--<link rel='stylesheet' type='text/css' href='stylesheet.css'/>-->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script language="javascript">

            $(document).ready(function() {
                $('.rating').click(function() {
                $('.rating').removeClass('selected');
                ratingClick(this);
            });
            });

            function ratingClick(that) {
                console.log(that.id);
                    if (that.id == 'rating4' || that.id == 'rating5') {
                        $('#questions').fadeOut('slow');
                        $('#thankYou').fadeIn('slow');
                    } else {
                        $('#getMore').fadeIn();
                        $(that).toggleClass('selected');
                    }
            }

            $(document).ready(function() {
                $('#submit').click(function(){
                    $('#questions').fadeOut('slow');
                    $('#thankYou').fadeIn('slow');
                });
            });

        </script>
        <style>

            .ratings {
                float: left;
                width: 100%;
            }

            .rating { 
                margin: 7px;
                font-weight: bold;
                background-color: aliceblue;
            }

            .rating:hover {
                background-color:#990000;
                color: white;
            }

            #getMore {
                display:none;
                clear:both;
                background-color:aliceblue;
                border:solid black 1px;
                padding:0px 5px 5px 10px;
                margin:0px 0px 0px 7px;
            }

            #thankYou {
                display:none;
                font-weight: bold;
            }

            .selected {
                background-color: #990000;
                color: white;
            }

            textarea {
                resize: none;
            }

            body {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
            }

            h2 {
                margin-bottom: 5px;
                font-size: 12px
            }

        </style>
    </head>
    <body>
        <form id="questions" action="connect.php" method="post">
        <h2>How helpful is this article?</h2>
        <div class="ratings">
            <input type="radio" name="Q1" class="rating" id="rating1" value="1">Not at all helpful
            <input type="radio" name="Q1" class="rating" id="rating2" value="2">Not very helpful
            <input type="radio" name="Q1" class="rating" id="rating3" value="3">Somewhat helpful
            <input type="radio" name="Q1" class="rating" id="rating4" value="4">Very helpful
            <input type="radio" name="Q1" class="rating" id="rating5" value="5">Extremely helpful
        </div>
        <div id="getMore">
            <h2>Please tell us why you didn't find this article helpful:</h2>
                <input type='checkbox' name="Q2_1" value="1">Not related to my issue<br/>
                <input type='checkbox' name="Q2_2" value="1">Too complicated explanations<br/>
                <input type='checkbox' name="Q2_3" value="1">Too much information<br/>
                <input type='checkbox' name="Q2_4" value="1">Incorrect information<br/>
                <input type='checkbox' name="Q2_5" value="1">Unclear information<br/>
                <input type='checkbox' name="Q2_6" value="1">Incomplete information<br/>
                <h2>Do you have any other feedback about this article?</h2>
                <p><input type="text" name="Q3" /><p>
            <div id = "submit"><input type='submit' value="Submit" /></div>
        </div>
        </form>
        <div id="thankYou">
            Thanks for your feedback!
        </div>
    </body>
</html>

Here is the php document:

<?php

    define('DB_NAME', 'staterequirements');
    define('DB_USER', 'myuser');
    define('DB_PASSWORD', 'mypass');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if(!$link) {
        die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if(!$db_selected) {
        die('Can\'t use ' . DB_NAME . ' : ' . mysql_error());
    }

    $Q1 = (isset($_POST['Q1']) ? $_POST['Q1'] : null);
    $Q2_1 = (isset($_POST['Q2_1']) ? $_POST['Q2_1'] : null);
    $Q2_2 = (isset($_POST['Q2_2']) ? $_POST['Q2_2'] : null);
    $Q2_3 = (isset($_POST['Q2_3']) ? $_POST['Q2_3'] : null);
    $Q2_4 = (isset($_POST['Q2_4']) ? $_POST['Q2_4'] : null);
    $Q2_5 = (isset($_POST['Q2_5']) ? $_POST['Q2_5'] : null);
    $Q2_6 = (isset($_POST['Q2_6']) ? $_POST['Q2_6'] : null);
    $Q3 = (isset($_POST['Q3']) ? $_POST['Q3'] : null);

   $sql = "INSERT INTO response (Q1, Q2_1, Q2_2, Q2_3, Q2_4, Q2_5, Q2_6) VALUES ('$Q1', '$Q2_1', '$Q2_2', '$Q2_3', '$Q2_4', '$Q2_5', '$Q2_6')";

    if (!mysql_query($sql)) {
        die('Error: ' . mysql_error());
    }

    mysql_close();

}?>
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Alex
  • 25
  • 1
  • 3
  • 10
  • 3
    If you're new to PHP, you should really get off on the right foot and use `mysqli` or `PDO`. It's much easier to avoid attacks in those, especially for beginners. http://php.net/manual/en/mysqlinfo.api.choosing.php – Evan Oct 28 '13 at 01:57

1 Answers1

2

These points are parts of your problem.

This:

<div id="questions" action="connect.php" method="post">

should be <form and not a div:

<form id="questions" action="connect.php" method="post">

And this:

    <div class="ratings" name="Q1">
        <div class="rating" id="rating1" value="1">Not at all helpful</div>
        <div class="rating" id="rating2" value="2">Not very helpful</div>
        <div class="rating" id="rating3" value="3">Somewhat helpful</div>
        <div class="rating" id="rating4" value="4">Very helpful</div>
        <div class="rating" id="rating5" value="5">Extremely helpful</div>
    </div>

I'm not sure whether you wanted to use a dropdown menu select or checkboxes or radio buttons or inputs, however those divs are not valid form elements.

I would also like to point out that it is highly recommended that you use MySQLi_ and/or PDO instead of the deprecated MySQL_ because your (posted) code is open to injection.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks for your response. The first question is supposed to be a "radio" type. However, I am trying to use jQuery and Divs to make it more visually appealing than radio buttons. Since they are not valid "form" elements, what would be the best way to get around this issue instead of changing this into an actual "form"? – Alex Oct 28 '13 at 02:44
  • @Alex You're welcome. Unfortunately you need to use the standard form elements. There are certain things you can leave out when using jQuery/Ajax however the standard forms protocol must be followed. – Funk Forty Niner Oct 28 '13 at 02:46
  • @Alex As an added note and from my experience so far with MySQL is that, when one of the form elements hasn't been properly set or named, the database won't insert/update etc. the rest of the form elements. – Funk Forty Niner Oct 28 '13 at 02:51
  • That makes sense. I updated it to a form, now it is giving me a 404. (I will worry about the MySQLi/PDO once I can actually get this thing running). – Alex Oct 28 '13 at 03:31
  • @Alex Are you trying to run the whole code on the same page? If so, then you need to set your action to this `action=""` unless if your handler is in fact in a seperate file called `connect.php` then if it doesn't exist, then that's the reason for the `404` – Funk Forty Niner Oct 28 '13 at 03:50
  • @Alex Am still thinking, but for now why do you have `require 'db/connect.php';` in your HTML? You shouldn't need that unless you have your entire code in the same page, which I doubt is the case. – Funk Forty Niner Oct 28 '13 at 04:15
  • @Alex Great, am glad to hear that. And you're very much welcome, cheers. – Funk Forty Niner Oct 28 '13 at 04:17