Page 1 of 1

[solved] Can't insert values into database :(

Posted: Thu Mar 30, 2006 9:10 am
by katd
I hope someone can help i've been working on this all day and i can't move on until i've can insert the values entered in my online form into the database. If I use the same insert statement in phpmyadmin (replacing the variables with actual values) it works fine and i've tried hardcoding the values into the insert statment but they still won't go into the database. Uploading an image is also fine. I'm getting the error. 'Your Submission could not be processed due to a system error' Which is the set statement for when the query did not run ok.

I really hope someone can help it's been driving me nuts, the code I have is below in a file called add_home.php.

Thanks in advance

Kat

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Add a Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php # Script - add_home.php
// This page allows the administrator to add a home (product).

require_once ('../../mysql_connect.php'); // conect to the database.

if (isset($_POST['submit'])) { // Handle the form.
	
	// Validate the home_name, category, year, berths, dimensions, axle, price and general_details.
	
	//Check for a home name.
	if (!empty($_POST['home_name'])) {
		$hn = escape_data($_POST['home_name']);
	} else {
		$hn = FALSE;
		echo '<p><font color="red">Please enter the home\'s name!</font></p>';
	}
	
	// Check for an image (not required).
	if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
		if (move_uploaded_file($_FILES['image']['tmp_name'], 
		"../../uploads/{$_FILES['image']['name']}")) { // Move the file over.
		
			echo '<p>The file has been uploaded!</p>';
			
		} else { // Couldn't move the file over.
			echo '<p><font color="red">The file could not be moved.</font></p>';
			$i = '';
		}
		$i = $_FILES['image']['name'];
	} else {
		$i = '';
	}
	
	// Check for category
	if(!empty($_POST['category'])) {
		$c = escape_data($_POST['category']);
	} else {
		$c = FALSE;
		echo '<p><font color="red">Please enter the home\'s category!</font></p>';
	}
	
	// Check for a year
	if(!empty($_POST['year'])) {
		$y = escape_data($_POST['year']);
	} else {
		$y = FALSE;
		echo '<p><font color="red">Please enter the home\'s year</font></p>';
	}
	
	// Check for berths
	if(!empty($_POST['berths'])) {
		$b = escape_data($_POST['berths']);
	} else {
		$b = FALSE;
		echo '<p><font color="red">Please enter the home\'s berth</font></p>';
	}
	
	// Check for dimensions
	if(!empty($_POST['dimensions'])) {
		$d = escape_data($_POST['dimensions']);
	} else {
		$d = FALSE;
		echo '<p><font color="red">Please enter the home\'s dimensions</font></p>';
	}
	
	// Check for axle type
	if(!empty($_POST['axle'])) {
		$a = escape_data($_POST['axle']);
	} else {
		$a = FALSE;
		echo '<p><font color="red">Please enter the home\'s axle type</font></p>';
	}
	
	// Check for a price
	if(is_numeric($_POST['price'])) {
		$p = $_POST['price'];
	} else {
		$p = FALSE;
		echo '<p><font color="red">Please enter the home\'s axle type</font></p>';
	}
	
	// Check for a general description (not required).
	if (!empty($_POST['general_details'])) {
		$g = escape_data($_POST['general_details']);
	} else {
		$g = '<i>No description available.</i>';
	}
	
	if ($hn && $c && $y && $b && $d && $a && $p) {
		// Add the home to the database.
		$query = "INSERT INTO homes(exterior_name, home_name, category, year, berths, dimensions, axle, price, general_details)
		VALUES('$i', '$hn', '$c', '$y', '$b', '$d', '$a', '$p', '$g')";
		if ($result = @mysql_query ($query)) { // Worked.
			echo '<p>The home has been added.</p>';
		} else { // If the query did not run OK.
			echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
		}
	} else { // Failed a test.
			echo '<p><font color="red">Please click "back" and try again.</font></p>';
	}
	
} else { // Display the form.
?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="524288">

<fieldset><legend>Fill out the form to add a home to the catalog:</legend>

<p><strong>Home Name:</strong> <input type="text" name="home_name" size="30" maxlength="40"></p>

<p><strong>Image:</strong> <input type="file" name="image"></p>

<p><strong>Category:</strong> 
  <select name="category" id="category">
    <option value="New Home" selected>New Home</option>
    <option value="Used Home">Used Home</option>
  </select>
</p>

<p><strong>Year:</strong> <input type="text" name="year" size="4" maxlength="4"></p>

<p><strong>Berths:</strong> <input type="text" name="berths" size="2" maxlength="2"></p>

<p><strong>Dimensions:</strong> <input type="text" name="dimensions" size="30" maxlength="100"></p>

<p><strong>Axle:</strong> <input type="text" name="axle" size="20" maxlength="20"></p>

<p><strong>Price:</strong> <input type="text" name="price" size="10" maxlength="10"><br><small>Do not include the pound sign or comma.</small></p>

<p><strong>General Details:</strong> <textarea name="general_details" cols="40" rows="5"></textarea></p>

</fieldset>

<div align="center"><input type="submit" name="submit" value="Submit"></div>

</form><!-- End of Form -->
<?php
} //End of main conditional.
?>	

</body>
</html>
my field names in the table homes are:

Code: Select all

Field  	Type   	Null  	Default
home_id  	int(3) 	No  	 
exterior_name  	varchar(60) 	Yes  	NULL 
home_name  	varchar(40) 	No  	 
category  	varchar(20) 	No  	 
year  	year(4) 	No  	0000 
berths  	smallint(2) 	No  	0 
dimensions  	varchar(100) 	No  	 
axle  	varchar(20) 	No  	 
price  	decimal(6,2) 	No  	0.00 
general_details  	text 	Yes  	NULL 
interior_name  	varchar(60) 	Yes  	NULL 
interiordetail_name  	varchar(60) 	Yes  	NULL

Indexes:

Code: Select all

Keyname 	Type 	Cardinality 	Field
PRIMARY 	PRIMARY 	0  	home_id
home_name 	INDEX 	None  	home_name
category

Posted: Thu Mar 30, 2006 9:33 am
by JayBird
Does this statement...

Code: Select all

if ($hn && $c && $y && $b && $d && $a && $p) {
...definately equate to 'TRUE'?

Re: Can't insert values into database :(

Posted: Thu Mar 30, 2006 11:35 am
by Benjamin
Try this..

Code: Select all

if ($hn && $c && $y && $b && $d && $a && $p) {
		// Add the home to the database.
		$query = "INSERT INTO `homes` (`exterior_name`, `home_name`, `category`, `year`, `berths`, `dimensions`, `axle`, `price`, `general_details`)
		VALUES('$i', '$hn', '$c', '$y', '$b', '$d', '$a', '$p', '$g')";
		if ($result = @mysql_query ($query)) { // Worked.
			echo '<p>The home has been added.</p>';
		} else { // If the query did not run OK.
			echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
		}
	} else { // Failed a test.
			echo '<p><font color="red">Please click "back" and try again.</font></p>';
	}

Still not working

Posted: Fri Mar 31, 2006 1:25 am
by katd
Pimptastic
Does this statement...
PHP:
if ($hn && $c && $y && $b && $d && $a && $p) {

...definately equate to 'TRUE'?
They are all required fields and i'm entering data into each of them so they should all be true.

Agtlewis thanks for the post but that didn't work either, I tried a number of combinations of '' but with no luck. Could I be going wrong elsewhere in the code?

Has anyone else got any ideas?

Thanks

Kat

Posted: Fri Mar 31, 2006 1:29 am
by feyd
var_dump() them all and find out. :)

Posted: Fri Mar 31, 2006 1:36 am
by katd
Hi

Once I submitted it again while using var_dump() it came back with bool(true) but still with the error message

Posted: Fri Mar 31, 2006 1:39 am
by feyd
remove the @ from the mysql_query() call. Change the error to output mysql_error() (temporarily)

I think you need some backticks.

Posted: Fri Mar 31, 2006 2:10 am
by katd
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


It came back with 

'No database selected' 

This is my connection code but the form wouldn't show up if this wasn't working so it must be a problem in the add_home.php script.

Code: Select all

<?php # mysql_connect.php

//This file contains the database access information for the database. This file also establishes a connection to MySQL and selects the database.

//Set the database access information as constants.
define ('DB_USER', 'username');
define ('DB_PASSWORD', 'password');
define ('DB_HOST', 'localhost');
define ('DB_NAME', 'holiday');

//Make the connection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());

// Function for escaping and trimming form data.

function escape_data ($data) {
	global $dbc;
	if (ini_get('magic_quotes_gpc')) {
		$data = stripslashes($data);
	}
	return mysql_real_escape_string (trim ($data), $dbc);
} // End of escape_data() function.
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Mar 31, 2006 3:45 am
by ed209
The reason why it says 'No database selected' is because you haven't selected a database !!

Code: Select all

// This bit you have
mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

// This bit you're missing
mysql_select_db(DB_NAME);

Posted: Fri Mar 31, 2006 3:58 am
by katd
God don't I feel like a right idiot. I checked the one code but didn't bother to completely check my connection code I kept seeing what i wanted to see. Thanks for pittying such a stupid person. Everything works fine now what a waste of a day.

THANKS EVERYONE