Jump to content

[SOLVED] PHP Error, but data goes into table


karatekid36

Recommended Posts

Howdy All,

I am revcieveing the following error, but my data is entering the table(the code follows the error): 

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/carrigan/public_html/basecamp/enter_events.php on line 99

 

Warning: Cannot modify header information - headers already sent by (output started at /home/carrigan/public_html/basecamp/enter_events.php:99) in /home/carrigan/public_html/basecamp/enter_events.php on line 120

 

 

<?php 

session_name ('YourVisitID');
session_start(); // Start the session.

// If no session value is present, redirect the user.
if (!isset($_SESSION['user_id'])) {

// 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.
}
$url .= '/index.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}

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

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

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

// Check for an event name.
if (empty($_POST['name'])) {
	$errors[] = 'You forgot to enter an event name.';
} else {
	$n = escape_data($_POST['name']);
}

// Check for a meeting type.
if (empty($_POST['type'])) {
	$errors[] = 'You forgot to enter a meeting type.';
} else {
	$t = escape_data($_POST['type']);
}

// Check for a location.
if (empty($_POST['location'])) {
	$errors[] = 'You forgot to enter a location.';
} else {
	$l = escape_data($_POST['location']);
}

	// Check for a meeting description.
if (empty($_POST['description'])) {
	$errors[] = 'You forgot to enter a meeting description.';
} else {
	$d = escape_data($_POST['description']);
}


	// Check for a meeting month.
if (empty($_POST['month'])) {
	$errors[] = 'You forgot to enter a meeting month.';
} else {
	$mm = escape_data($_POST['month']);
}

	// Check for a meeting day.
if (empty($_POST['day'])) {
	$errors[] = 'You forgot to enter a meeting day.';
} else {
	$md = escape_data($_POST['day']);
}

	// Check for a meeting year.
if (empty($_POST['year'])) {
	$errors[] = 'You forgot to enter a meeting year.';
} else {
	$my = escape_data($_POST['year']);
}

	// Check for a meeting hour.
if (empty($_POST['hour'])) {
	$errors[] = 'You forgot to enter a meeting hour.';
} else {
	$mh = escape_data($_POST['hour']);
}

	// Check for a meeting min.
if (empty($_POST['mins'])) {
	$errors[] = 'You forgot to enter a meeting time.';
} else {
	$mmin = escape_data($_POST['mins']);
}


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

	// Enter the meeting in the database.

	// Check for previous registration.
	$query = "SELECT id FROM calendar WHERE ev_date='$my-$mm-$md' AND ev_locn='$l'";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0) {

		// Make the query.
$query = "INSERT INTO calendar (ev_dat, ev_title, ev_locn, ev_desc, ev_time, ev_type) VALUES ('$my-$mm-$md', '$n', '$l', '$d', '$mh:$mmin:00', '$t')";		
		$result = @mysql_query ($query); // Run the query.
		if ($result) { // If it ran OK.

			// Send an email, if desired.

			// Redirect the user to the view_events.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_events.php';

			header("Location: $url");
			exit();

		} else { // If it did not run OK.
			$errors[] = 'Your event could not be added.'; // Public message.
			$errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.
		}

	} else { // Email address is already taken.
		$errors[] = 'The event has already been registered.';
	}

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

// Close the database connection.
mysql_close(); 

} else { // Form has not been submitted.

$errors = NULL;

} // End of the main Submit conditional.

// Begin the page now.
$page_title = 'Add an Event';

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


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 an Event</h2>
<form action="enter_events.php" method="post">
<p>Event Name: <input type="text" name="name" size="30" maxlength="30" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>


<p>Meeting Date: <?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="month">';
foreach ($months as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';


// Make the days pull-down menu.
echo '<select name="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="year">';
$year = 2008;
while ($year <= 2010) {
echo "<option value=\"$year\">$year</option>\n";
$year++;
}
echo '</select>';

?>

  <p>Time: 
<?php
  // Make the days pull-down menu.
echo '<select name="hour">';
for ($hour = 0; $hour <= 23; $hour++) {
echo "<option value=\"$hour\">$hour</option>\n";
}
echo '</select>';
?>

<select name="mins">
<option>00</option>
<option>15</option>
<option>30</option>
<option>45</option>
</select>

  </p>
  
  </p>
  
  <p>Type:
      <label>
    <input type="radio" name="type" value="Mandatory" />
Mandatory</label>
    <label>
    <input type="radio" name="type" value="Not Mandatory" />
Not Mandatory</label>
  
  </p>


<p>Location: <input type="text" name="location" size="15" maxlength="30" value="<?php if (isset($_POST['location'])) echo $_POST['location']; ?>" /></p>


<p>Meeting Description:
<br />
<TEXTAREA NAME="description" COLS=40 ROWS=4 value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"></TEXTAREA> 
  </p>
  


<p><input type="submit" name="submit" value="Enter Event" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>

The issue is with these lines here:

 

$query = "SELECT id FROM calendar WHERE ev_date='$my-$mm-$md' AND ev_locn='$l'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {

 

Basically, your select query is failing. mysql_num_rows() is returning false and the above error. The data is being inserted because 0 is equal to false, unless you also check for type (===).

 

So, add an or die statement to your query, and find out what the problem is:

 

$query = "SELECT id FROM calendar WHERE ev_date='$my-$mm-$md' AND ev_locn='$l'";
$result = mysql_query($query) or die(mysql_error().'<br />Query:'.$query);
if (mysql_num_rows($result) == 0) {

 

The header errors result from the above mysql error - fix that, and you'll fix the header errors.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.