Page 1 of 1

SQL editing database

Posted: Thu Apr 06, 2006 7:13 pm
by mbshaf4
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] 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]


I am having trouble with this code.  I am trying to edit a database entry and it doesn't seem to be working.  I pass title through the previous page used and am sure it picks it up b/c i echoed it out after the line.  The page runs without errors but i keep getting the error output I created 'this page has been accessed in error.  I can't find the logic issue.  It doesnt edit the database either.   Any help would be greatly apprecited thanks.

Code: Select all

<?php

$page_title = 'Edit a game';
include ('header.php');

// Check for a valid user ID, through GET or POST.
if ( isset($_GET['title'])) { // Accessed through view_users.php
	$tit = $_GET['title'];
} elseif (isset($_POST['title']) ) { // Form has been submitted.
	$tit = $_POST['title'];
} else { // No valid ID, kill the script.
	echo '<h1 id="mainhead">Page Error</h1>
	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
	include ('footer.php');
	exit();
}

require_once ('connect.php'); // Connect to the db.

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

	$errors = array(); // Initialize error array.

	// Check for a first name.
	if (empty($_POST['name'])) {
		$errors[] = 'You forgot to the title.';
	} else {
		$t =$_POST['name'];
	}

	// Check for a last name.
	if (empty($_POST['price'])) {
		$errors[] = 'You forgot to enter your price.';
	} else {
		$p = $_POST['price'];
	}

	// Check for an email address.


	if (empty($errors)) { // If everything's OK.


			// Make the query.
			$query = "UPDATE gr SET title='$t', price='$p'WHERE user_id='$_COOKIE[user_id]' AND title='$tit'";
			$result = @mysql_query ($query); // Run the query.
			if (mysql_affected_rows() == 1) { // If it ran OK.

				// Print a message.
				echo '<h1 id="mainhead">Edit a User</h1>
				<p>The user has been edited.</p><p><br /><br /></p>';

			} else { // If it did not run OK.
				echo '<h1 id="mainhead">System Error</h1>
				<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
				echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.
				include ('footer.php');
				exit();
			}



	} else { // Report the errors.

		echo '<h1 id="mainhead">Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
		foreach ($errors as $msg) { // Print each error.
			echo " - $msg<br />\n";
		}
		echo '</p><p>Please try again.</p><p><br /></p>';

	} // End of if (empty($errors)) IF.

} // End of submit conditional.

// Always show the form.

// Retrieve the user's information.
$query = "SELECT title, price1 FROM gr WHERE user_id='$_COOKIE[user_id]'";
$result = @mysql_query ($query); // Run the query.

if (1 == 1) { // Valid user ID, show the form.

	// Get the user's information.
	$row = mysql_fetch_array ($result, MYSQL_NUM);

	// Create the form.
	echo '<h2>Edit a User</h2>
<form action="edit_game.php" method="post">
<p>Title: <input type="text" name="name" size="30" maxlength="30" value="' . $row[title] . '" /></p>
<p>Price: <input type="text" name="price" size="15" maxlength="30" value="' . $row[price1] . '" /></p>

<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="" value="' . $tit . '" />
</form>';

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

mysql_close(); // Close the database connection.

include ('footer.php');
?>



Thanks in Advance!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] 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: Thu Apr 06, 2006 7:14 pm
by mbshaf4
Some of the commenting is bogus. I copyied some of the code from another page.

Posted: Thu Apr 06, 2006 7:20 pm
by mbshaf4
Nevermind I figured it out....thanks tho