Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9066761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:47:35+00:00 2026-06-16T16:47:35+00:00

I built a song request form for my wedding website and would like to

  • 0

I built a song request form for my wedding website and would like to check if the variables I am storing the form input in is empty before POST’ing to the database. My goal is simple prevent blank rows from being added to mysql db when the for is fired off.

 <?php 
// extract data from form; store in variable
$artist =  $_POST["artist"];
$song = $_POST["song"];

// connect to server 
$conn = mysql_connect('host', 'user', 'pass');


// check if you can connect; if not then die

if (!$conn) {
    echo "<center>";
    die('Could Not Connect: ' . mysql_error());
    echo "</center>";
    }

// check if you can select table; in not then die

$db = mysql_select_db('database', $conn);

if (!$db) {
    echo "<center>";
    die('Database Not Selected: ' . mysql_error());
    echo "</center>";
    }

// Define the query to inser the song request
$insert = mysql_query("INSERT INTO Songs (Artist, Song) VALUES ('$artist', '$song')");  

// check if above variables are empty 
if (!empty($artist) and !empty($song)) {
    echo "<center>";
    echo "Insert was succesful<br>";
    echo "<a href='index.html' target='_self' >Back</a>";
    echo "</center>";
}
else {
    echo "<center>";
    die("Please fill in at least the artist name");
    echo "</center>";
}

// close the connection to the server
mysql_close($conn);
?>

I have the above in a file called insert.php which is fired off when form on the index page is submitted. Form is submitting using POST and works just fine, however I would like to prevent blank submissions from happening.

Very new to programming and want to learn how to do this right.

Thanks for your patience.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:47:36+00:00Added an answer on June 16, 2026 at 4:47 pm

    You are so close! All you had to do was put the insert after you do a check if the artist and song are filled in!

    <?php 
        // extract data from form; store in variable
        $artist =  $_POST["artist"];
        $song = $_POST["song"];
    
        // connect to server 
        $conn = mysql_connect('host', 'user', 'pass');
    
        // check if you can connect; if not then die
    
        if (!$conn) {
            echo "<center>";
            die('Could Not Connect: ' . mysql_error());
            echo "</center>";
        }
    
        // check if you can select table; in not then die
    
        $db = mysql_select_db('database', $conn);
    
        if (!$db) {
            echo "<center>";
            die('Database Not Selected: ' . mysql_error());
            echo "</center>";
        }
    
        // check if above variables are empty 
        if (!empty($artist) and !empty($song)) {
            // Define the query to inser the song request
            $insert = mysql_query("INSERT INTO Songs (Artist, Song) VALUES ('$artist', '$song')");  
    
            if($insert) {
              echo "<center>";
              echo "Insert was succesful<br>";
              echo "<a href='index.html' target='_self' >Back</a>";
              echo "</center>";
            }
        }
        else {
            echo "<center>";
            die("Please fill in at least the artist name");
            echo "</center>";
        }
    
        // close the connection to the server
        mysql_close($conn);
    

    That’s it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We built a iphone app and now we would like to export it to
I would like to build a program which lets lyrics of a song run
I built a website and then deploy it on the server. But when i
Ive built a little jquery sliding div on my website that when you hover
Manually built: [btnRun addTarget:self action:@selector(RunApp:) forControlEvents:UIControlEventTouchUpOutside]; Programmatically built: something of the following like ??
I would like to build an audio multitrack sequencer similar to a drum machine.
I built an app that streams a music library song from one iOS device
I built an online news portal before which is working fine for me but
We built a .NET server application that hosts data (contacts, email, etc.). We'd like
Thanks for any help in advance. I would like to set id3 tag data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.