endoprocta Posted May 22, 2007 Share Posted May 22, 2007 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/ Share on other sites More sharing options...
colombian Posted May 22, 2007 Share Posted May 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259079 Share on other sites More sharing options...
endoprocta Posted May 23, 2007 Author Share Posted May 23, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259483 Share on other sites More sharing options...
trq Posted May 23, 2007 Share Posted May 23, 2007 escape_data() is not a native php function, you need to define it, or not use it. Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259486 Share on other sites More sharing options...
endoprocta Posted May 23, 2007 Author Share Posted May 23, 2007 Hi, If I do not use escape_data(), how do I re-write the codes for the form to work? If I use the escape_data(), how do I define it? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259527 Share on other sites More sharing options...
trq Posted May 23, 2007 Share Posted May 23, 2007 Where exactly did you get this code from? A simple work around might be to replace all calls to escape_data() with mysql_real_escape_string(), chances are they do much the same thing anyways. Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259559 Share on other sites More sharing options...
endoprocta Posted May 23, 2007 Author Share Posted May 23, 2007 I got this code from a book. Thabnks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259691 Share on other sites More sharing options...
MadTechie Posted May 23, 2007 Share Posted May 23, 2007 doesn't seam urgent to me! Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259695 Share on other sites More sharing options...
endoprocta Posted May 23, 2007 Author Share Posted May 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52498-need-help-with-my-php-formurgently/#findComment-259757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.