Jump to content

Recommended Posts

I am new to PHP and learning a lot as I go, but there is something I'm stuck on and hoping you can all help with.

 

In short, the code below is for a page on my site that allows users who are logged in to create an event. When I upload all necessary files to my website to test them live, everything loads fine but the 'Invalid Event' error is continuously being returned when I am entering proper information.. I don't know quite what's wrong with the code so hopefully if any of you can spot something it'd greatly help.

 

<?php 
require_once('config.php');
if ( !isset($_SESSION['user']) ) { include('login.php'); exit; }
if ( isset($_POST['Submit']) ) {
if ( trim($_POST['EventName']) == '' ) {
	$error['eventname'] = 1;
}
if ( trim($_POST['Venue']) == '' ) {
	$error['venue'] = 1;
}
$sql = 'select * from `events` where `id` = \'' . mysql_real_escape_string($_POST['EventName']) . '\'';
$res = mysql_query($sql);
if ( mysql_num_rows($res) == 0 ) {
	$error['event'] = 1;
}
if ( !isset($error) ) {
	$sql = 'INSERT INTO `events` (`eventname`,`sport`,`state`,`venue`,`month`,`day`,`year`) values (\'' . mysql_real_escape_string($_POST['EventName']) . '\',\'' . mysql_real_escape_string($_POST['Sport']) . '\',\'' . mysql_real_escape_string($_POST['State']) . '\',\'' . mysql_real_escape_string($_POST['Venue']) . '\',\'' . mysql_real_escape_string($_POST['Month']) . '\',\'' . mysql_real_escape_string($_POST['Day']) . '\',\'' . mysql_real_escape_string($_POST['Year']) . '\',\'';
	$res = mysql_query($sql) or die(mysql_error());
	?><script>alert('You have successfully created an event.');</script><?php
}
else { 
	if ( isset($error['eventname']) ) { ?>
		<script>alert('You must enter a name for your event!');</script>
	<?php }
	if ( isset($error['venue']) ) { ?>
		<script>alert('You must enter a venue for your event!');</script>
	<?php }
	if ( isset($error['event']) ) { ?>
		<script>alert('Invalid Event');</script>
<?php	}
}
}
?>

 

If it helps, the code for the entry form is below (apologies for the annoying indenting, that step has yet to come):

 

<form name="form1" method="post" action="">
	    <label>
	    Event Name:<br>
	    <input type="text" name="EventName" id="EventName">
	    </label>
	    <br>
	    <br>
		<label>
            Sport:<br>
	  <select name="Sport" id="Sport">
<option value="Baseball">Baseball</option>
<option value="Basketball">Basketball</option>
<option value="Football">Football</option>
<option value="Hockey">Hockey</option>
      </select>
	  </label>
            <br>
            <br>
	  	State:<br>
	  <label>
	  <select name="State" id="State">
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DC">DC</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="IA">IA</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="MA">MA</option>
<option value="MD">MD</option>
<option value="ME">ME</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MO">MO</option>
<option value="MS">MS</option>
<option value="MT">MT</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="NE">NE</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NV">NV</option>
<option value="NY">NY</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="VA">VA</option>
<option value="VT">VT</option>
<option value="WA">WA</option>
<option value="WI">WI</option>
<option value="WV">WV</option>
<option value="WY">WY</option>
      </select>
	  </label>
	  <br>
	  <br>
	  Venue / Location:<br>
	  <label>
	  <input type="text" name="Venue" id="Venue">
	  </label>
	  <br>
	  <br>
	  Event Date:<br>
	  <label>
	  <select name="Month" id="Month">
          <option value="January">January</option>
          <option value="January">February</option>
          <option value="January">March</option>
          <option value="January">April</option>
          <option value="January">May</option>
          <option value="January">June</option>
          <option value="January">July</option>
          <option value="January">August</option>
          <option value="January">September</option>
          <option value="January">October</option>
          <option value="January">November</option>
          <option value="January">December</option>
          </select>
           / 
          <select name="Day" id="Day">
          <option value="January">1</option>
          <option value="January">2</option>
          <option value="January">3</option>
          <option value="January">4</option>
          <option value="January">5</option>
          <option value="January">6</option>
          <option value="January">7</option>
          <option value="January">8</option>
          <option value="January">9</option>
          <option value="January">10</option>
          <option value="January">11</option>
          <option value="January">12</option>
          <option value="January">13</option>
          <option value="January">14</option>
          <option value="January">15</option>
          <option value="January">16</option>
          <option value="January">17</option>
          <option value="January">18</option>
          <option value="January">19</option>
          <option value="January">20</option>
          <option value="January">21</option>
          <option value="January">22</option>
          <option value="January">23</option>
          <option value="January">24</option>
          <option value="January">25</option>
          <option value="January">26</option>
          <option value="January">27</option>
          <option value="January">28</option>
          <option value="January">29</option>
          <option value="January">30</option>
          <option value="January">31</option>
          </select>
           / 
          <select name="Year" id="Year">
          <option value="January">2010</option>
          <option value="January">2011</option>
          <option value="January">2012</option>
          <option value="January">2013</option>
          <option value="January">2014</option>
          <option value="January">2015</option>
          </select>
	  </label>
          <br>
          <br>
	  <input type="submit" name="Submit" id="Submit" value="Create Event">
	  </label>
	  </form>

 

I do have a MySQL database setup with the correct table and fields entered, and all fields have proper values/attributes.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/207753-new-to-php-not-understanding-this-error/
Share on other sites

I believe the following block of code is responsible for that error

	$sql = 'select * from `events` where `id` = \'' . mysql_real_escape_string($_POST['EventName']) . '\'';
$res = mysql_query($sql);
if ( mysql_num_rows($res) == 0 ) {
	$error['event'] = 1;
}

Here you're checking to see if an event with the same name already exists. The problem part is with your if statement. You're checking to see if no results have been returned and setting the error ($error['event'] = 1). Which is why you're receiving the 'Invalid Event' error. What you should be doing is checking to see 1 or more results have been returned instead.

 

Corrected if statement

	if ( mysql_num_rows($res) > 0 ) {
	$error['event'] = 1;
}

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.