dpedroia Posted July 14, 2010 Share Posted July 14, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/207753-new-to-php-not-understanding-this-error/ Share on other sites More sharing options...
wildteen88 Posted July 14, 2010 Share Posted July 14, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/207753-new-to-php-not-understanding-this-error/#findComment-1086042 Share on other sites More sharing options...
ram4nd Posted July 14, 2010 Share Posted July 14, 2010 don't use html in php, echo where necessary Quote Link to comment https://forums.phpfreaks.com/topic/207753-new-to-php-not-understanding-this-error/#findComment-1086045 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 This don't use html in php, echo where necessary has nothing to do with the OP's problem. You can switch in & out of PHP all you want. It works fine, but makes for messy code which can be hard to debug. Ken Quote Link to comment https://forums.phpfreaks.com/topic/207753-new-to-php-not-understanding-this-error/#findComment-1086049 Share on other sites More sharing options...
dpedroia Posted July 14, 2010 Author Share Posted July 14, 2010 Thanks so much wildteen88, that fixed the problem and made perfect sense. Tested the new code and all's well. As for not using HTML in PHP, I'll look into using echo instead and see what works best. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/207753-new-to-php-not-understanding-this-error/#findComment-1086050 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.