Please help me with my PHP form


#1

Hi,
I’m a newbie and doing my class project. I got this script from a book and tried modifying if for use in my project. Once I hit “Submit”, the form doesn’t work. Please help. I’ve attached the script below:

[php]

<?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 '

Page Error

This page has been accessed in error.



'; 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 '

Edit Race Information

The user has been edited.



'; } else { // If it did not run OK. echo '

System Error

The user could not be edited due to a system error. We apologize for any inconvenience.

'; // Public message. echo '

' . mysql_error() . '

Query: ' . $query . '

'; // Debugging message. include ('./includes/footer.html'); exit(); } } else { // Already registered. echo '

Error!

The email address has already been registered.

'; } } else { // Report the errors. echo '

Error!

The following error(s) occurred:
'; foreach ($errors as $msg) { // Print each error. echo " - $msg
n"; } echo '

Please try again.


'; } // 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 '

Edit Race Information

Course Name:

Race Name:

Race Sponsor:

'; } else { // Not a valid user ID. echo '

Page Error

This page has been accessed in error.



'; } mysql_close(); // Close the database connection. include ('./includes/footer.html'); ?>

[/php]

ADMIN EDIT: Added [php] code tags for readability. Please refer to http://phphelp.com/guidelines.php for posting guidelines.


#2

Any Errors? Is this page the ACTION Page? Are you sure that THIS page is the problem or perhaps it’s the FORM itself.

What about it “doesn’t work”? What have you done to resolve it?

Help us help you and provide a bit more detail/background.


#3

Have you tried Debugging?


#4

Hi,
I am using an external file linking to the database. The codes are as below:

[php]

<?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() ); ?>

[/php]

When I click submit, I get this message “Fatal error: Call to undefined function escape_data() in D:wampwwwedit_user.php on line 29”.

The code I pasted earlier belonged to the edit_user.php.

Thanks for the help.

ADMIN EDIT: Added [php] code tags for readability.
[size=99px]Please refer to http://phphelp.com/guidelines.php for posting guidelines.[/size]


#5
Fatal error: Call to undefined function escape_data() in D:wampwwwedit_user.php on line 29

So what’s not clear about that?

  • Fatal Error -> means PHP has encountered a problem and can’t continue executing your script
  • Call to undefined function -> you’re calling an undefined function
  • escape_data() -> the function you’re calling which is undefined
  • in D:…edit_user.php on line 29 -> where the error occurred

Just for the record, what should you add to your code to not get this error?


#6

Hi,
I’ve figured out that if I use mysql_real_escape_string() then the problem doesn’t appear but another problem comes up. Now the database gets updated but the page where the info is displayed does show the updated info. Any ideas?

Thanks.


#7