Jump to content

Recommended Posts

Hi,

I'm a newbie and need help for my PHP/MYSQL project. I have created a form but when I click submit, it does not work or update my database. I've included the code below. Can anyone help me fix it? Thanks.

 

<?php 

$page_title = 'Edit Race Information';
include ('./includes/header.html');

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

require_once ('mysqlconnection.php'); // Connect to mysql.

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

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

// Check for a course name.
if (empty($_POST['CourseName'])) {
	$errors[] = 'Please enter the Course name.';
} else {
	$cn = escape_data($_POST['CourseName']);
}

// Check for a race name.
if (empty($_POST['RaceName'])) {
	$errors[] = 'Please enter the Race name.';
} else {
	$rn = escape_data($_POST['RaceName']);
}

// Check for race sponsor.
if (empty($_POST['RaceSponsor'])) {
	$errors[] = 'Please enter the Race sponsor.';
} else {
	$rs = escape_data($_POST['RaceSponsor']);
}

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

	//  Test for unique race name.
	$query = "SELECT ID FROM jc_race WHERE RaceName='$rn' AND ID != $id";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0) {

		// Make the query.
		$query = "UPDATE jc_race SET CourseName='$cn', RaceName='$rn', RaceSponsor='$rs' WHERE ID=$id";
		$result = @mysql_query ($query); // Run the query.
		if (mysql_affected_rows() == 1) { // If it ran OK.

			// Print a message.
			echo '<h1 id="mainhead">Edit Race Information</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 ('./includes/footer.html'); 
			exit();
		}

	} else { // Already registered.
		echo '<h1 id="mainhead">Error!</h1>
		<p class="error">The email address has already been registered.</p>';
	}

} 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 CourseName, MeetingDate, MeetingStartTime, RaceName,
RaceTime, RaceSponsor, RacePrizeMoney, RaceGoing, ID
FROM jc_race WHERE ID=$id";
$result = @mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 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 Race Information</h2>
<form action="edit_user.php" method="post">
<p>Course Name: <input type="text" name="CourseName" size="15" maxlength="15" value="' . $row[0] . '" /></p>
<p>Race Name: <input type="text" name="RaceName" size="15" maxlength="15" value="' . $row[3] . '" /></p>
<p>Race Sponsor: <input type="text" name="RaceSponsor" size="15" maxlength="15" value="' . $row[5] . '"  /> </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 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 ('./includes/footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/
Share on other sites

To better help you, are you getting any kind of error?

 

You have an external file connecting to MySQL, are you sure all those values are updated with the corresponding ones from your database?

 

I would use

require_once ('mysqlconnection.php');

only when you actually need the database. You opened that up too early. But is nothing being written to the database, check out that file and make sure all your values are correct.

 

It should look something like:

 $connection = mysql_connect($host, $user, $password) or die ("Unable to connect to MySQL server");
  		mysql_select_db($database,$connection) or die ("Unable to select database");

With the values for $host, $user and $password matching your server/db.

 

 

Hi,

The code in the external file is as below:

 

<?php 

DEFINE ('DB_USER', '48307');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', '48307');

// Make the connnection.
$dbc = @mysql_connect (DB_HOST, DB_USER) OR die ('Could not connect to MySQL: ' . mysql_error() );

// Select the database.
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>

 

The error I received is "Fatal error: Call to undefined function escape_data() in D:\wamp\www\edit_user.php on line 29"

 

I've also check the access to the database and all the information is correct. Anything else I'm looking out for?

Hi,

Tried applying mysql_real_escape_string() and it update my database but when i refresh the page where the info is displayed, it does not display the updated info. Why is that?

 

Btw, it is urgent as I need to hand up this assignment by Friday.

 

Thanks.

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.