HTML表单数据未插入

时间:2019-03-15 09:15:12

标签: php html sql

我正在努力将表单数据插入SQL数据库。选择提交按钮后,我将重定向到一个页面,该页面显示了process.php文件的源代码。

代码如下:

html文件

<!DOCTYPE html>
<html>
<head>
    <title>Insert Open Times</title>
    <link rel="stylesheet" type="text/css" href="phpstyle.css">
</head>
<body>
    <center>Welcome</center>
    <center>Please enter your opening times for this week.</center>
    <center class="warning_text">Please remember to use 24hr time format!</center><br>
    <center>
    <form action="process.php" method="post" class="form">
        <label>Monday Open:</label><br><input type="text" name="monop"><br>
        <label>Monday Close:</label><br><input type="text" name="monclo"><br>
        <label>Tuesday Open:</label><br><input type="text" name="tueop"><br>
        <label>Tuesday Close:</label><br><input type="text" name="tueclo"><br>
        <label>Wednesday Open:</label><br><input type="text" name="wedop"><br>
        <label>Wednesday Close:</label><br><input type="text" name="wedclo"><br>
        <label>Thursday Open:</label><br><input type="text" name="thuop"><br>
        <label>Thursday Close:</label><br><input type="text" name="thuclo"><br>
        <label>Friday Open:</label><br><input type="text" name="friop"><br>
        <label>Friday Close:</label><br><input type="text" name="friclo"><br>
        <label>Saturday Open:</label><br><input type="text" name="satop"><br>
        <label>Saturday Close:</label><br><input type="text" name="satclo"><br>
        <label>Sunday Open:</label><br><input type="text" name="sunop"><br>
        <label>Sunday Close:</label><br><input type="text" name="sunclo"><br>
        <input type="submit" name="submit" value="Submit">
    </form>
</center>
</body>
</html>

和process.php文件

<?php 

    $con = mysqli_connect('localhost', 'root', 'root');

    if(!$con){
        echo "Not connected to server";
    }

    if (!mysqli_select_db($con,'opentimes')) {
        echo "Databse not selected";
    }

    $monop = $_POST['monop'];
    $monclo = $_POST['monclo'];
    $tueop = $_POST['tueop'];
    $tueclo = $_POST['tueclo'];
    $wedop = $_POST['wedop'];
    $wedclo = $_POST['wedclo'];
    $thuop = $_POST['thuop'];
    $thuclo = $_POST['thuclo'];
    $friop = $_POST['friop'];
    $friclo = $_POST['friclo'];
    $satop = $_POST['satop'];
    $satclo = $_POST['satclo'];
    $sunop = $_POST['sunop'];
    $sunclo = $_POST['sunclo'];



    $sql = "INSERT INTO weekdata (monop, monclo, tueop, tueclo, wedop, wedclo, thuop, thuclo, friop, friclo, satop, satclo, sunop, sunclo) VALUES ('$monop', '$monclo', '$tueop', '$tueclo', '$wedop', '$wedclo', '$thuop', '$thuclo', '$friop', '$friclo', '$satop', '$satclo', '$sunop', '$sunclo')";

    if (!mysqli_query($con,$sql)) {
        echo "Not successful";  
    }

?>

我在SQL数据库中进行了三次检查,所有变量都被正确命名。

我还应该注意,我对php很陌生,所以请原谅大多数已犯的错误。

2 个答案:

答案 0 :(得分:0)

确保您的本地主机正常工作并执行php文件,好像XAMPP或WAMP配置不正确

答案 1 :(得分:0)

<?php 

    $con = mysqli_connect('localhost', 'root', 'root');

    if(!$con){
        echo "Not connected to server";
    }

    if (!mysqli_select_db($con,'opentimes')) {
        echo "Databse not selected";
    }

    $monop = $_POST['monop'];
    $monclo = $_POST['monclo'];
    $tueop = $_POST['tueop'];
    $tueclo = $_POST['tueclo'];
    $wedop = $_POST['wedop'];
    $wedclo = $_POST['wedclo'];
    $thuop = $_POST['thuop'];
    $thuclo = $_POST['thuclo'];
    $friop = $_POST['friop'];
    $friclo = $_POST['friclo'];
    $satop = $_POST['satop'];
    $satclo = $_POST['satclo'];
    $sunop = $_POST['sunop'];
    $sunclo = $_POST['sunclo'];



    $sql = "INSERT INTO weekdata (monop, monclo, tueop, tueclo, wedop, wedclo, thuop, thuclo, friop, friclo, satop, satclo, sunop, sunclo) VALUES ('$monop', '$monclo', '$tueop', '$tueclo', '$wedop', '$wedclo', '$thuop', '$thuclo', '$friop', '$friclo', '$satop', '$satclo', '$sunop', '$sunclo')";

$dsql = mysqli_query($con,$sql); //For Executing Your query

    if($dsql){
echo "Successful";
}else{
echo mysqli_error($con); //For Checking Error in insertion
}

?>