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!
I hope someone can help I have no problem adding new homes to my database but when I try and edit a home it takes me to the form with the boxes filled in with the data but when I change one and submit so that it updates the database it does not update the database. It doesn't bring up and error message the page just appears blank. I think it might be something to do with my if statements.
if (isset($HTTP_POST_VARS['home_id']) && $HTTP_POST_VARS['home_id']!='')
{ // It's an update
$home_id = $HTTP_POST_VARS['home_id'];
$query = "update homes
set exterior name = '$i',
home_name = '$hn',
category = '$c',
year = '$y',
berths = '$b',
dimensions = '$d',
axle = '$a',
price = '$p',
general_details = '$gn'
where
home_id = $home_id";
}
I believe your problem is here "set exterior name = '$i',". Should that be set exterior_name = '$i'????
Additionally it is very difficult to understand your code for an outsider as most of you variables names are one or two letter... it may be worthwhile instead of using $p, using $price especially if you plan to get anyone on the boards to actually bother reading your code
Once you know where the error is, reply back with it. Then make sure to take those two lines back out of your code.
Another thing to try is using some else statements when executing your queries and using a die(mysql_error()) in the else. This helps identify mysql errors (like the one you are probably getting, but not showing because you are not throwing any errors to the browser).
Right I've got a bit further now once I pressed submit the message saying it has been uploaded comes up indicating that the query worked but when I view the data it hasn't actually been updated in the database???
if ($result = mysql_query($query)) { // Worked.
echo '<p>The home has been added.</p>';
} else { // If the query did not run OK.
die('<p><font color="red">Your submission could not be processed due to a system error: ' . mysql_error() . '.</font></p>');
}
The mysql_error() will display the reason that the INSERT didn't work.
and then copy and paste the query from the screen and try and run it manually in the database. The error that you get from the database is the same error that the mysql_error() will give you, but you get the added bonus of seeing the query.
Obviously make sure you don't do this kind of thing in a production system - printing SQL to the screen is suicide.
Sorry to be such a pain but this didn't change anything it just says "It has been uploaded!" it doesn't say the confirmation which is for both editing and updating "The home has been added" but then it doesn't bring up any errors either.
if ($result = mysql_query($query)) { // Worked.
echo '<p>The home has been added.</p>';
} else { // If the query did not run OK.
die('<p><font color="red">Your submission could not be processed due to a system error: ' . mysql_error() . '.</font></p>');
}
You have to add an image at the moment I have got the current image name to display in teh input box but I'm not worried about that.
Then upload the new script. I want to test it to see what is happening. Also, make sure you have die statement as else's to each of your mysql_query sets.
I've added what you suggested bit it still doesn't cause anything else to show other than "It has been uploaded!" The image has been uploaded to the uploads folder but it can't be updating to the database otherwise it would display "The house has been added" but then it isn't returning the error message either?!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<!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 and update a home (product).
require_once ('../../mysql_connect.php'); // conect to the database.
function get_home_record($home_id)
{
$query = "select * from homes where home_id = '$home_id'";
if (!$result = mysql_query($query))
{
die("There was a problem with query::$query - " . mysql_error());
}
return(mysql_fetch_array($result));
}
if (isset($HTTP_GET_VARS['home_id']))
$h = get_home_record($HTTP_GET_VARS['home_id']);
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 = $_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 (isset($HTTP_POST_VARS['home_id']) && $HTTP_POST_VARS['home_id']!='')
{ // It's an update
$home_id = $HTTP_POST_VARS['home_id'];
$query = "update homes set
exterior_name = '$i',
home_name = '$hn',
category = '$c',
year = '$y',
berths = '$b',
dimensions = '$d',
axle = '$a',
price = '$p',
general_details = '$g'
where home_id = $home_id";
} else { // It's a new story
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')";
} else { // Failed a test.
/******************************************
* THIS SHOULD BE DIE SEEING AS QUERY DOES
* NOT EXIST HERE
*****************************************/
die('<p><font color="red">Please click "back" and try again.</font></p>');
}
/**********************************************
* CLOSE THE ISSET CHECK HERE AND RUN THE QUERY
* OUTSIDE OF IT
*********************************************/
}
/**************************************************
* SETTING YOUR RESULT HERE AND TESTING FOR
* AFFECTED ROWS IS A GOOD IDEA
*************************************************/
if ($result = mysql_query($query)) { // Worked.
if (mysql_affected_rows($result)) {
echo '<p>The home has been added.</p>';
} else {
echo 'THERE WAS A PROBLEM HERE';
}
} else { // If the query did not run OK.
die('<p><font color="red">Your submission could not be processed due to a system error: ' . mysql_error() . '.</font></p>');
}
} else { // Display the form.
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="home_id" value="<?php print $HTTP_GET_VARS['home_id'];?>">
<input type="hidden" name="MAX_FILE_SIZE" value="77524288">
<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" value="<?php print $h['home_name'];?>"></p>
<p><strong>Image:</strong> <input type="file" name="image" value="<?php print $h['exterior_name'];?>"></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" value="<?php print $h['year'];?>"></p>
<p><strong>Berths:</strong> <input type="text" name="berths" size="2" maxlength="2" value="<?php print $h['berths'];?>"></p>
<p><strong>Dimensions:</strong> <input type="text" name="dimensions" size="30" maxlength="100" value="<?php print $h['dimensions'];?>"></p>
<p><strong>Axle:</strong> <input type="text" name="axle" size="20" maxlength="20" value="<?php print $h['axle'];?>"></p>
<p><strong>Price:</strong> <input type="text" name="price" size="10" maxlength="10" value="<?php print $h['price'];?>"><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"><?php print $h['general_details'];?></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>
Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/vhost/swctesting.co.uk/html/4dm1n/add_home.php on line 148
THERE WAS A PROBLEM HERE