0

I'm trying to use a php form saved on the artistRegister.php below and process it with the file at the bottom named processArtist.php .

I can't find the proper way to populate the fields in case there's an error and when I submit it to the database, I get an empty record on the database.

I'm populating a dropdown list on the navigation bar at header.php and using the same code to connect to the database, so it's not a database communication problem, since I'm able to create new empty records on the submit button press.

Tried several ways to make it work but would appreciate any help debugging the problem at hand.

Thank you advance.

***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);
4
  • In your first file you are not reading the vars from $_POST also, when something is wrong you should be able to redirect with post data (populate invisible form and then post using js)... I strongly suggest you use a mvc framework or separate your forms from your actions then include them if you don't have a templating engine yet... (github.com/spark-cruz/elpho < my framework) Commented Sep 21, 2016 at 22:38
  • Sorry, @SparK. Didn't understand what you meant with "not reading the vars from $_POST". I tried several different ways to get the form data into POST and then process it and handle it to the database. In plain PHP this should be an easy thing to d. I just can't figure out where I'm going wrong. Commented Sep 21, 2016 at 22:46
  • 1
    Well, what I would do (and most procedural php people do) is use a single file but check if $_POST is present, so you know if the user wants the form or to perform an action, and call the mysqli functions (again 1 file 2 responsabilities is bad but they have the same url for the post trick to work). But that's the old way, read about OOP and MVC, you'll really need it in the future. Commented Sep 22, 2016 at 14:14
  • Thank you, @SparK. Haven't done anything with OOP and MVC. Looks like a better way to achieve what I want to do, but still new at this methods. Guess I'm back to square 1 to make this application. Commented Sep 26, 2016 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.