0

我正在我们的常见问题页面底部设置一个调查,我是 PHP 新手,这是我第一次尝试连接到数据库(没有教程)。

我在 MySQL 中设置了 HTML 页面、PHP 设置和表格。

每次我提交时,数据库都会创建一个新行,但所有行都有“0”而不是分配给输入/div的值。请帮忙!

编辑:我将 HTML 更新为现在的表单,但是,当我提交时,我得到一个 404(并且行根本没有更新。可能出了什么问题?

这是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>

这是php文档:

<?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();

}?>
4

1 回答 1

2

这些点是您问题的一部分。

这个:

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

应该是<form而不是div

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

和这个:

    <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>

我不确定您是否想使用下拉菜单select或按钮checkboxesradio输入,但是这些divs不是有效的表单元素

我还想指出,强烈建议您使用MySQLi_和/或PDO而不是弃用的,MySQL_因为您的(发布的)代码对注入开放

于 2013-10-28T02:31:08.823 回答