数据未插入数据库?

时间:2016-11-02 10:46:14

标签: php html mysql database webpage

当我点击“添加问题”时按钮,数据不会放入我的数据库。屏幕仅显示:'数据无法插入'从功能。

将数据插入数据库的功能(来自User类):

public function AddMultiQuestion($question,$answer,$type){

        $sql="INSERT INTO question SET Type='$type', Question='$question', Answer='$answer'";
        $result = mysqli_query($this->db,$sql) or die(mysqli_connect_errno()."Data cannot be inserted");
        return $result;

    }

网页中的PHP代码:

<?php

include_once 'include/class.user.php';
$user = new User();

if (isset($_REQUEST['addMulti'])){
    extract($_REQUEST);

    if ($AnswerNo = '1'){
        $answer = $Answer1;
    } elseif ($AnswerNo = '2') {
        $answer = $Answer2;
    } elseif ($AnswerNo = '3') {
        $answer = $Answer3;
    } elseif ($AnswerNo = '4') {
        $answer = $Answer4;
    }

    $type = 'Multi';

    $addMultiQ = $user->AddMultiQuestion($question,$answer,$type);

    if ($addMultiQ) {
        // Registration Success
        echo 'Question added.';
    } else {
        // Registration Failed
        echo 'Question failed to be added.';
    }
}

if (isset($_GET['q'])){
    $user->user_logout();
    header("location:login.php");
}

if (isset($_REQUEST['SSButton'])) {
    header("location:sentenceAnswer.php");
}

if (isset($_REQUEST['PreviewButton'])) {
    header("location:quizPreview.php");
}
?>

如果您有兴趣,可以访问网页的HTML代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width = device-width, initial-scale = 1">
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    <style>
        body{
            font-family:Arial, Helvetica, sans-serif;
        }
        #container{width:650px; margin: 0 auto;}
    </style>
    <script language="javascript" type="text/javascript">
        function submitQ() {
            var form = document.reg;
            if(form.Username.value == ""){
                alert( "Enter username." );
                return false;
            }
            else if(form.Password.value == ""){
                alert( "Enter password." );
                return false;
            }
            else if(form.type.value == "") {
                alert( "Enter type." );
                return false;
            }
        } 
    </script>
</head>

<body background = "assets/background.jpg">
    <div id="container" class="container">
        <div id="header">
            <a href="login.php?q=logout">LOGOUT</a>
        </div>
        <div class="page-header">
            <h1>Quiz Maker</h1>
        </div>
        <div id="main-body" class="jumbotron">
            <form action="makeQuiz.php" method="post">
                <tr>
                    <div class="btn-group">
                        <td><button class="btn btn-warning btn-lg" type="submit" name="addMulti" value="AddMultiQ">Add Question</button></td>
                        <td><button class="btn btn-warning btn-lg">Delete Question</button></td>
                    </div>
                </tr>
                <tr>
                    <div id="multiChoice">
                        <table> 
                            <h4>Multiple Choice</h4>
                            <tr>
                                <th>Insert Question:</th>
                            </tr>
                            <tr>
                                <td><input class="form-control" type="text" name="question" maxlength="100"></td>   
                            </tr>
                            <tr>    
                                <td>Answer 1:<input type="text" size="20" maxlength="100" name="Answer1"></td>
                            </tr>
                            <tr>
                                <td>Answer 2:<input type="text" size="20" maxlength="100" name="Answer2"></td>
                            </tr>
                            <tr>
                                <td>Answer 3:<input type="text" size="20" maxlength="100" name="Answer3"></td>
                            </tr>
                            <tr>
                                <td>Answer 4:<input type="text" size="20" maxlength="100" name="Answer4"></td>
                            </tr>
                            <tr>
                                <th>Correct Answer</th>
                            </tr>                   
                        </table>
                    </div>
                </tr>
                <tr>
                    <div class="form-group" id="dropdown">
                        <Select class="form-control" type="text" name="AnswerNo" required>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                        </Select>
                    </div>                                  
                </tr>
                <tr>
                    <div>
                        <td><button class="btn btn-warning btn-lg" type="submit" name="SSButton">Add Single-Sentence Question</button></td>
                    </div>
                </tr>
                <tr>
                    <div>
                        <td><button class="btn btn-warning btn-lg" type="submit" name="PreviewButton">Quiz Preview</button></td>
                    </div>
                </tr>
                <tr>
                    <div>
                        <td><button class="btn btn-warning btn-lg" type="submit" name="CreateButton">Create Quiz</button></td>
                    </div>
                </tr>
            </form>
        </div>
        <div id="footer"></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

2 个答案:

答案 0 :(得分:-1)

你有语法错误,你已经传递单引号与$ varriable ,这是不需要的
尝试以下代码

/prg.php

答案 1 :(得分:-1)

更改此

$sql="INSERT INTO question SET Type='$type', Question='$question', Answer='$answer'";

进入这个

$sql="INSERT INTO question SET Type='".$type."', Question='".$question."', Answer='".$answer."'";