Upload to Database with Form -- Whats Wrong!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Phillips126
Forum Newbie
Posts: 9
Joined: Thu Feb 18, 2010 2:21 pm

Upload to Database with Form -- Whats Wrong!?

Post by Phillips126 »

Hey all,

I'm still fairly new to PHP, but I'm getting better.

I am working on a website which allows users to login and from their "member" page, they can submit a "mixed drink recipe" to the database for everyone else to see.

For the life of me, I CANNOT figure out what the issue is, and it is driving me nuts!

Here is the code for the form:

Code: Select all

<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<label class="register_label">Drink Name:</label>
<input class="register_field" name="drink_name" type="text" /><br /><br />

<label class="register_label">Author:</label>
<input class="register_field" name="drink_author" value="<?php echo $member_name ?>" type="text" /><br /><br />

<label class="register_label">Posted Date:</label>
<input class="drink_author" style="width: 100px; text-align: center;" value="<?php echo date("m/d/y"); ?>" name="date" type="text" /><br /><br />

<label class="register_label">Drink Type:</label>

<label class="drink_type_label">Mixed Drink:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="mixed_drink" />

<label class="drink_type_label">Beer Recipe:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="beer_drink" />

<label class="drink_type_label">Hot Drink:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="hot_drink" />

<label class="drink_type_label">Punch Recipe:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="punch_drink" />

<label class="drink_type_label">Other Drink:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="other_drink" /><br /><br />

<label class="register_label">Submit Drink:</label>
<input name="upload_drink" value="Upload Drink" type="submit" />
</form>
Here is my PHP:

Code: Select all

<?php

// Check Data...

$error = "";

if(isset($_POST['upload_drink'])) {
	
if(!empty($_POST['drink_name']) && !empty($_POST['drink_type'])) {
	
$drink_name =  stripslashes ($_POST['drink_name']);
$drink_creator =  stripslashes ($_POST['drink_author']);
$drink_type = stripslashes ($_POST['drink_type']);

$drink_name = mysql_real_escape_string($drink_name);
$drink_creator = mysql_real_escape_string($drink_creator);
$drink_directions = mysql_real_escape_string($drink_directions);

$date = date("m/d/y");

// Submit Data...

	$query = "INSERT INTO recipes (name, author, type, date_uploaded) 
          			   		VALUES ('$drink_name', '$drink_creator', '$drink_type', '$date')";
					   
	mysql_query($query) or die ('Error, Insert Query Failed.');
	
	$error = "<div id=\"register_complete\">Drink Successfully Added.</div><br />";
	
} else {
	
	$error = "<div id=\"register_error\">Please Ensure All Fields Are Correctly Filled Out.</div><br />";
	
}	

} else {
	
	$error = "<div id=\"register_error\">Please Ensure All Fields Are Correctly Filled Out.</div><br />";
	
}


?>
When the page loads, the div "<div id=\"register_error\">Please Ensure All Fields Are Correctly Filled Out.</div>" is displayed and when the Submit button is pressed, nothing happens.

Anyone able to figure out what the issue is before I pull all my hair out? lol...

Thanks,

Phillips126
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Upload to Database with Form -- Whats Wrong!?

Post by a94060 »

Make the second else statement contain the code for the form. The second else refers to

Code: Select all

if(isset($_POST['upload_drink'])) {
. So if that isnt set,you want it to display the form


EDIT-Like this:

Code: Select all

<?php

// Check Data...

$error = "";

if(isset($_POST['upload_drink'])) {
       
if(!empty($_POST['drink_name']) && !empty($_POST['drink_type'])) {
       
$drink_name =  stripslashes ($_POST['drink_name']);
$drink_creator =  stripslashes ($_POST['drink_author']);
$drink_type = stripslashes ($_POST['drink_type']);

$drink_name = mysql_real_escape_string($drink_name);
$drink_creator = mysql_real_escape_string($drink_creator);
$drink_directions = mysql_real_escape_string($drink_directions);

$date = date("m/d/y");

// Submit Data...

        $query = "INSERT INTO recipes (name, author, type, date_uploaded)
                                                VALUES ('$drink_name', '$drink_creator', '$drink_type', '$date')";
                                           
        mysql_query($query) or die ('Error, Insert Query Failed.');
       
        $error = "<div id=\"register_complete\">Drink Successfully Added.</div><br />";
       
} else {
       
        $error = "<div id=\"register_error\">Please Ensure All Fields Are Correctly Filled Out.</div><br />";
       
}      

} else {
 ?>
<form  name="myform"  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<label class="register_label">Drink Name:</label>
<input class="register_field" name="drink_name" type="text" /><br /><br />

<label class="register_label">Author:</label>
<input class="register_field" name="drink_author" value="<?php echo $member_name ?>" type="text" /><br /><br />

<label class="register_label">Posted Date:</label>
<input class="drink_author" style="width: 100px; text-align: center;" value="<?php echo date("m/d/y"); ?>" name="date" type="text" /><br /><br />

<label class="register_label">Drink Type:</label>

<label class="drink_type_label">Mixed Drink:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="mixed_drink" />

<label class="drink_type_label">Beer Recipe:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="beer_drink" />

<label class="drink_type_label">Hot Drink:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="hot_drink" />

<label class="drink_type_label">Punch Recipe:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="punch_drink" />

<label class="drink_type_label">Other Drink:</label>
<input class="radio_drinks" name="drink_type" type="radio" value="other_drink" /><br /><br />

<label class="register_label">Submit Drink:</label>
<input name="upload_drink" value="Upload Drink" type="submit" />
</form>
<?PHP      
 
       
}


?>
Phillips126
Forum Newbie
Posts: 9
Joined: Thu Feb 18, 2010 2:21 pm

Re: Upload to Database with Form -- Whats Wrong!?

Post by Phillips126 »

Thanks for the response.

Do you mean change the PHP code to this:

Code: Select all

<?php

// Check Data...

$error = "";

session_start();

if(isset($_POST['upload_drink'])) {
	
if(!empty($_POST['drink_name']) && !empty($_POST['drink_type']) && !empty($_POST['rte1'])) {
	
} else {
	
	$error = "<div id=\"register_error\">Please Ensure All Fields Are Correctly Filled Out.</div><br />";
	
}	

} else {
	
	$drink_name =  stripslashes ($_POST['drink_name']);
	$drink_creator =  stripslashes ($_POST['drink_author']);
	$drink_directions = stripslashes ($_POST['rte1']);
	$drink_type = stripslashes ($_POST['drink_type']);

	$drink_name = mysql_real_escape_string($drink_name);
	$drink_creator = mysql_real_escape_string($drink_creator);
	$drink_directions = mysql_real_escape_string($drink_directions);

	$date = date("m/d/y");

	// Submit Data...

	$query = "INSERT INTO recipes (name, author, type, directions, date_uploaded) 
          			   		VALUES ('$drink_name', '$drink_creator', '$drink_type', '$drink_directions', '$date')";
					   
	mysql_query($query) or die ('Error, Insert Query Failed.');
	
	$error = "<div id=\"register_complete\">Drink Successfully Added.</div><br />";
	
}


?>
When I now load up the page, it states: ""<div id=\"register_complete\">Drink Successfully Added.</div>" and adds a blank entry to my Database.

Thanks,

Phillips126
Phillips126
Forum Newbie
Posts: 9
Joined: Thu Feb 18, 2010 2:21 pm

Re: Upload to Database with Form -- Whats Wrong!?

Post by Phillips126 »

Just wanted to let you know I got it working.

Thanks for all the help =)

Phillips126
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Upload to Database with Form -- Whats Wrong!?

Post by a94060 »

You are welcome, enjoy. :D
Post Reply