Jump to content

is this update script correct


jeppers

Recommended Posts

i am not sure if the update query i am using is correct as i am not able to find the error any chance you coul have a look

 

$result = mysql_query($query)or die(mysql_error());;
if (mysql_num_rows($result) == 0) {

// make query
$query = "UPDATE menu SET title='$t', description='$t', price='$p' AS f,
		  food_associations AS fa WHERE f.food_id = fa.food_id AND food_cat_id=$id";
		  $result = @mysql_query($query) or die(mysql_error());
		  if (mysql_affected_rows()==1) {// if ran ok 
		  
			//print message
			echo '<h1> Edit Food</h1>
			<p>The food has been updatted</p><p><br /><br /></p>';

all help welcome

Link to comment
Share on other sites

i have 2 tables which need to be updated 1 called food and another food_associations. i want to update the fields in both so when the user views the info they will see there update. sorry i am very tired i will show you the whole script

<?php
require_once ('mysql_connect.php'); //connect to the database 

// check the form has been submitted 
if (isset($_POST['submitted'])) {

//check for title
if (!empty($_POST['title'])) {
	$t = ($_POST['title']);
} else {
	$t = FALSE;
	echo '<p><font color="red">Please enter a food name.</font></p>';
}

//check for food description 
if (!empty($_POST['description'])) {
	$d =($_POST['description']);
} else {
	$d = FALSE;
	echo '<p><font color="red">Please enter a description of the food.</font></p>';
}

//check for food category 
if (isset($_POST['types']) && (is_array($_POST['types']))) {
	$type = TRUE;
} else {
	$type = FALSE;
	echo '<p><font color="red"> Please select one category.</font></p>';
}

//check food price
if (!empty($_POST['price'])) {
	$p = ($_POST['price']);
} else { 
	$p = FALSE;
	echo '<p><font color="red">Please enter a price</font></p>';
}

$result = mysql_query($query)or die(mysql_error());;
if (mysql_num_rows($result) == 0) {

// make query
$query = "UPDATE menu SET title='$t', description='$t', price='$p' AS f,
		  food_associations AS fa WHERE f.food_id = fa.food_id AND food_cat_id=$id";
		  $result = @mysql_query($query) or die(mysql_error());
		  if (mysql_affected_rows()==1) {// if ran ok 
		  
			//print message
			echo '<h1> Edit Food</h1>
			<p>The food has been updatted</p><p><br /><br /></p>';

		} else { //if it did not run ok
			echo '<h1> System error</h1>
			<p>Sorry there has been a system error and the food could not be updated. sorry</p>';
			echo '<p>' . mysql_error() .'<br /><br />Problem:' . $query . '</p>';// debugging message
			//include ('footer.php')
			exit();
		}

	} else { //report the errors
		echo '<h1>Error!</h1>
		<p>The follow errors have occurred:<br />';
		foreach ($errors as $msg){//print each message 
			echo "-$msg<br />\n";
		}
		echo'</p><p>Please try again.</p><p><br /></p>';
	}//end of if
}

//always show the form 

//retrive the users information 
$query = "SELECT f.food_id, title, description, price FROM foods AS f,
food_associations AS fa WHERE f.food_id = fa.food_id AND fa.food_cat_id=$type";
$result = @mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) == 1){//valid id show form 

	//get the users information
	$row = mysql_fetch_array($result, MYSQL_NUM);

	//create the form 
	echo '<h2> Edit Food.
	<form action="edit_food.php" method="post">
	<p>Title: <input type="text" name="title" size="20" maxlength="20" value="". $row[0] ."" /></p>
	<p>Description: <input type="text" name="description" size="15" maxlength="30" value="' . $row[1] . '" /></p>
	<p>Price: <input type="text" name="price" size="20" maxlength="40" value="' . $row[2] . '"  /> </p>
	<p><input type="submit" name="submit" value="Submit" /></p>
	<input type="hidden" name="submitted" value="TRUE" />
	<input type="hidden" name="id" value="' . $id . '" />
	</form>';

} else { // Not a valid user ID.
echo '<h1>Page Error</h1>
<p>This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close the database connection.


?>

 

i want it to display the info that they are tring to change and then once its changed to update the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.