使用PHP_SELF和php进程PHP到MYSQL表单验证和数据库插入

时间:2016-09-21 22:31:48

标签: php mysql forms validation

我正在尝试使用保存在下面的artistRegister.php上的php表单,并使用底部名为processArtist.php的文件处理它。

如果出现错误,我找不到填充字段的正确方法,当我将其提交到数据库时,我在数据库中得到一条空记录。

我在header.php的导航栏上填充了一个下拉列表,并使用相同的代码连接到数据库,因此这不是数据库通信问题,因为我能够在提交时创建新的空记录按下按钮。

尝试了几种使其正常工作的方法,但希望能够帮助调试手头的问题。

谢谢你提前。

***artistRegister.php***

<?php require('includes/config.php');


//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); } 

//define page title
$title = 'Members Page';

//include header template
require('layout/header.php'); 
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?php if(isset($title)){ echo $title; }?></title>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="style/main.css">
</head>
<body>
    <div class="container-fluid">
        <div class="col-xs-3">
        </div>
        <div class="col-xs-6">
            <?php
                // define variables and initialize with empty values
                $artistName = $artistLabel = $websiteAddress = $facebookAddress = $youtubeAddress ="";



                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    if (empty($_POST["artistName"])) {
                        $nameErr = "Enter Artist name";
                    }
                    else {
                        $artistName = $_POST["artistName"];
                    }

                    if (empty($_POST["artistLabel"])) {
                        $labelErr = "Enter Label name";
                    }
                    else {
                        $artistLabel = $_POST["artistLabel"];
                    }

                }
            ?>
            <form class="form" method="POST" action="processArtist.php">
                <div class="form-group">
                    <label for="artistName">Artist Name:</label>
                    <input type="text" class="form-control" id="artistName" value="<?php echo htmlspecialchars($artistName);?>">
                    <span class="error" style="color: red"><?php echo $nameErr;?></span><br>

                    <label for="artistLabel">Artist Label:</label>
                    <input type="text" class="form-control" id="artistLabel" value="<?php echo htmlspecialchars($artistLabel);?>">
                    <span class="error"><?php echo $LabelErr;?></span><br>
                    <label for="websiteAddress">Artist Website:</label>
                    <input type="text" class="form-control" id="websiteAddress" value="<?php echo htmlspecialchars($websiteAddress);?>">
                    <label for="facebookAddress">Artist Facebook:</label>
                    <input type="text" class="form-control" id="facebookAddress" value="<?php echo htmlspecialchars($facebookAddress);?>">
                    <label for="youtubeAddress">Artist Youtube:</label>
                    <input type="text" class="form-control" id="youtubeAddress" value="<?php echo htmlspecialchars($youtubeAddress);?>">
                    <br>
                    <button type="submit" class="btn btn-default">Submit</button>
                </div>
            </form>
        </div>
        <div class="col-xs-3">
        </div>
    </div>

</body>


    ***processArtist.php***

    <?php
        $host="localhost";
        $username="some_user_with_access";
        $password="the_user_password";
        $db_name="artist_management";

        $con=mysqli_connect("$host","$username","$password","$db_name");
        // Check connection
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        var_dump($_POST);

        $artistName=$_POST['artistName'];
        $artistLabel=$_POST['artistLabel'];
        $websiteAddress=$_POST['websiteAddress'];
        $facebookAddress=$_POST['facebookAddress'];
        $youtubeAddress=$_POST['youtubeAddress'];

        $sql="INSERT INTO artists (artistName, artistLabel, websiteAddress, facebookAddress, youtubeAddress)
        VALUES 
        ('$artistName', '$artistLabel', '$websiteAddress', '$facebookAddress', '$youtubeAddress')";

        if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
        mysqli_close($con);

0 个答案:

没有答案