karatekid36 Posted May 12, 2007 Share Posted May 12, 2007 This error is being produced with the following code. I know that the header code is messing with it and causing it not to work, but why is it not redirecting like it should? The code in question is right after the insert query. Thanks in advance. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/carrigan/public_html/php_attend/register.php on line 136 Warning: Cannot modify header information - headers already sent by (output started at /home/carrigan/public_html/php_attend/register.php:136) in /home/carrigan/public_html/php_attend/register.php on line 157 <?php # Script 8.7 - register.php // Send NOTHING to the Web browser prior to the header() line! // Check if the form has been submitted. if (isset($_POST['submitted'])) { require_once ('mysql_connect.php'); // Connect to the db. $errors = array(); // Initialize error array. // Check for a first name. if (empty($_POST['first_name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = escape_data($_POST['first_name']); } // Check for a last name. if (empty($_POST['last_name'])) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = escape_data($_POST['last_name']); } // Check for an email address. if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $e = escape_data($_POST['email']); } // Check for a cellphone. if (empty($_POST['cell'])) { $errors[] = 'You forgot to enter your cellphone number.'; } else { $cp = escape_data($_POST['cell']); } // Check for a screen name. if (empty($_POST['sn'])) { $errors[] = 'You forgot to enter your screen name.'; } else { $sn = escape_data($_POST['sn']); } // Check for an birthday month. if (empty($_POST['b_month'])) { $errors[] = 'You forgot to enter your birthday month.'; } else { $bm = escape_data($_POST['b_month']); } // Check for a birthday day. if (empty($_POST['b_day'])) { $errors[] = 'You forgot to enter your birthday day.'; } else { $bd = escape_data($_POST['b_day']); } // Check for an birthday year. if (empty($_POST['b_year'])) { $errors[] = 'You forgot to enter your birthday year.'; } else { $by = escape_data($_POST['b_year']); } // Check for an hometown address. if (empty($_POST['home_town'])) { $errors[] = 'You forgot to enter your hometown.'; } else { $ht = escape_data($_POST['home_town']); } // Check for a state if (empty($_POST['state'])) { $errors[] = 'You forgot to enter your state.'; } else { $st = escape_data($_POST['state']); } // Check for an tshirt size. if (empty($_POST['tshirt'])) { $errors[] = 'You forgot to enter your tshirt size.'; } else { $ts = escape_data($_POST['tshirt']); } // Check for an grad year. if (empty($_POST['grad_year'])) { $errors[] = 'You forgot to enter your graduation year.'; } else { $gy = escape_data($_POST['grad_year']); } // Check for an pledge semester. if (empty($_POST['pledge_semester'])) { $errors[] = 'You forgot to enter your pledge semester.'; } else { $ps = escape_data($_POST['pledge_semester']); } // Check for an pledge year. if (empty($_POST['pledge_year'])) { $errors[] = 'You forgot to enter your pledge year.'; } else { $py = escape_data($_POST['pledge_year']); } // Check for an pledge class. if (empty($_POST['pledge_class'])) { $errors[] = 'You forgot to enter your pledge class.'; } else { $pc = escape_data($_POST['pledge_class']); } // Check for a password and match against the confirmed password. if (!empty($_POST['password1'])) { if ($_POST['password1'] != $_POST['password2']) { $errors[] = 'Your password did not match the confirmed password.'; } else { $p = escape_data($_POST['password1']); } } else { $errors[] = 'You forgot to enter your password.'; } if (empty($errors)) { // If everything's OK. // Register the user in the database. // Check for previous registration. $query = "SELECT user_id FROM users WHERE email='$e'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { // Make the query. $query = "INSERT INTO brothers (first_name, last_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class, password, registration_date) VALUES ('$fn', '$ln', '$e', '$cp', '$sn', '$by-$bm-$bd', '$ht', '$st', '$ts', '$gy', '$ps', '$py', '$pc', SHA('$p'), NOW() )"; $result = @mysql_query ($query); // Run the query. if ($result) { // If it ran OK. // Send an email, if desired. // Redirect the user to the thanks.php page. // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } // Add the page. $url .= '/view_users.php'; header("Location: $url"); exit(); } else { // If it did not run OK. $errors[] = 'You could not be registered due to a system error. We apologize for any inconvenience.'; // Public message. $errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message. } } else { // Email address is already taken. $errors[] = 'The email address has already been registered.'; } } // End of if (empty($errors)) IF. mysql_close(); // Close the database connection. } else { // Form has not been submitted. $errors = NULL; } // End of the main Submit conditional. // Begin the page now. $page_title = 'Add a Brother'; include ('./includes/header.html'); if (!empty($errors)) { // Print any error messages. 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>'; } // Create the form. ?> <h2>Add a Brother </h2> <form action="register.php" method="post"> <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p> <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p> <p>Cellphone Number: <input type="text" name="cell" size="12" maxlength="10" value="<?php if (isset($_POST['cell'])) echo $_POST['cell']; ?>" /> </p> <p>Screen Name: <input type="text" name="sn" size="20" maxlength="30" value="<?php if (isset($_POST['sn'])) echo $_POST['sn']; ?>" /> </p> <p>Birthday: <?php // Make the months array. $months = array (1=> 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Make the days and years arrays. $days = range (1, 31); $years = range (2005, 2015); // Make the months pull-down menu. echo '<select name="b_month">'; foreach ($months as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } echo '</select>'; // Make the days pull-down menu. echo '<select name="b_day">'; for ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\">$day</option>\n"; } echo '</select>'; // Make the years pull-down menu. echo '<select name="b_year">'; $year = 1980; while ($year <= 2007) { echo "<option value=\"$year\">$year</option>\n"; $year++; } echo '</select>'; ?> </p> <p>Hometown: <input type="text" name="home_town" id="home_town" size="20" maxlength="40" value="<?php if (isset($_POST['home_town'])) echo $_POST['home_town']; ?>" /></p> <p>State: <select name="state" id="state" value="<?php if (isset($_POST['state'])) echo $_POST['state']; ?>" /> <option value="AL">AL</option> <option value="AK">AK</option> <option value="AZ">AZ</option> <option value="CA">CA</option> <option value="CO">CO</option> <option value="CT">CT</option> <option value="DE">DE</option> <option value="FL">FL</option> <option value="GA">GA</option> <option value="HI">HI</option> <option value="ID">ID</option> <option value="IL">IL</option> <option value="IN">IN</option> <option value="IA">IA</option> <option value="KS">KS</option> <option value="KY">KY</option> <option value="LA">LA</option> <option value="ME">ME</option> <option value="MD">MD</option> <option value="MA">MA</option> <option value="MI">MI</option> <option value="MN">MN</option> <option value="MS">MS</option> <option value="MO">MO</option> <option value="MT">MT</option> <option value="NE">NE</option> <option value="NV">NV</option> <option value="NH">NH</option> <option value="NJ">NJ</option> <option value="NM">NM</option> <option value="NY">NY</option> <option value="NC">NC</option> <option value="ND">ND</option> <option value="OH">OH</option> <option value="OK">OK</option> <option value="OR">OR</option> <option value="PA">PA</option> <option value="RI">RI</option> <option value="SC">SC</option> <option value="SD">SD</option> <option value="TN">TN</option> <option value="TX">TX</option> <option value="UT">UT</option> <option value="VT">VT</option> <option value="VA">VA</option> <option value="WA">WA</option> <option value="WV">WV</option> <option value="WI">WI</option> <option value="WY">WY</option> </select> </p> <p>T Shirt Size: <label> <input type="radio" name="tshirt" value="XXL" /> XXL</label> <label> <input type="radio" name="tshirt" value="XL" /> XL</label> <label> <input type="radio" name="tshirt" value="L" /> Large</label> <label> <input type="radio" name="tshirt" value="M" /> Medium</label> <label> <input type="radio" name="tshirt" value="S" /> Small</label> </p> <p>Graduation Year: <?php // Make the years pull-down menu. echo '<select name="grad_year" id="grad_year">'; $year = 2003; while ($year <= 2020) { echo "<option value=\"$year\">$year</option>\n"; $year++; } echo '</select>'; ?></p> <p>Pledge Semester: <label> <input type="radio" name="pledge_semester" value="Fall" /> Fall</label> <label> <input type="radio" name="pledge_semester" value="Spring" /> Spring</label> <?php // Make the years pull-down menu. echo '<select name="pledge_year">'; $year = 2005; while ($year <= 2020) { echo "<option value=\"$year\">$year</option>\n"; $year++; } echo '</select>'; ?> <p>Pledge Class:<?php // Make the pledge class array. $pledgeclass = array (1 => 'FF', 'Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta', 'Eta', 'Theta', 'Iota', 'Kappa', 'Lambda', 'Mu', 'Nu', 'Xi', 'Omicron', 'Pi', 'Rho', 'Sigma', 'Tau', 'Upsilon', 'Phi', 'Chi', 'Psi', 'Omega'); // Make the months pull-down menu. echo '<select name="pledge_class">'; foreach ($pledgeclass as $key => $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; ?></p> <p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p> <p>Confirm Password: <input type="password" name="password2" size="10" maxlength="20" /></p> <p><input type="submit" name="submit" value="Add Brother" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php include ('./includes/footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/51116-solved-my-first-header-error/ Share on other sites More sharing options...
AndyB Posted May 12, 2007 Share Posted May 12, 2007 The error described in line 36 causes output to the browser which in turn generates the header error. The $result returned from some query or other is failing. Hints - first don't add @ to suppress errors; second modify your queries to generate useful information on errors (which you can always remove once debugged). Prototypically: $query = "some valid SQL query string"; $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query; // generate useful error message Quote Link to comment https://forums.phpfreaks.com/topic/51116-solved-my-first-header-error/#findComment-251649 Share on other sites More sharing options...
karatekid36 Posted May 12, 2007 Author Share Posted May 12, 2007 The error in fact was caused my a mistyped table reference. I could not figure out how my headers were causing a problem, so I started looking for other things. I found it! Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/51116-solved-my-first-header-error/#findComment-251654 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.