toxi Posted May 17, 2009 Share Posted May 17, 2009 I'm trying to add the following set of data to my database $query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES ('$_POST[booktitle]','$_POST[date]','$_POST[bookStart]','$_POST[bookEnd]','$_POST[bookLanes]')"; these all correspond to a booking with the following types: bookTitle: varchar(100) date: date bookStart: Time bookEnd: Time bookLanes: varchar(10) Also, there's another field in my table, called id that's automatically incremented (the first column in my table) After asking around I was told to use addslash but I don't see where that is needed. Before adding this values to my database I've added the following code to verify that some input is indeed entered: if(((isset($_POST['bookTitle']) && !empty($_POST['bookTitle'])) && (isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['bookStart']) && !empty($_POST['bookStart'])) && ((isset($_POST['bookEnd']) && !empty($_POST['bookEnd'])) && ((isset($_POST['bookLanes']) && !empty($_POST['bookLanes']))); any help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/ Share on other sites More sharing options...
RussellReal Posted May 17, 2009 Share Posted May 17, 2009 in your query.. mysql_query(....) or die(mysql_error()); that should explain your problem Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835605 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 yes, but I have the following: $alert = "New Event successfully added"; else $alert = "Something is wrong. Try Again."; } the page doesn't even turn up Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835609 Share on other sites More sharing options...
RussellReal Posted May 17, 2009 Share Posted May 17, 2009 show the whole code, star out your passwords Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835610 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 here it is <?php //Database connection details $host = "**"; $mysql_user = "**"; $mysql_password = "**"; $mysql_db = "**"; //make connection with mysql and select the database $mysql_connect = mysql_connect($host, $mysql_user, $mysql_password); $db_select = mysql_select_db($mysql_db); //will be used to show alert message for success or error $alert = ""; //check if the form is submitted if(isset($_POST['add'])) { //check for empty inputs if(((isset($_POST['bookTitle']) && !empty($_POST['bookTitle'])) && (isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['bookStart']) && !empty($_POST['bookStart'])) && ((isset($_POST['bookEnd']) && !empty($_POST['bookEnd'])) && ((isset($_POST['bookLanes']) && !empty($_POST['bookLanes']))); { //add new event to the database $query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES ('$_POST[booktitle]','$_POST[date]','$_POST[bookStart]','$_POST[bookEnd]','$_POST[bookLanes]')"; $result = mysql_query($query); //check if the insertion is ok if($result) $alert = "New Event successfully added"; else $alert = "Something is wrong. Try Again."; } else { //alert message for empty input $alert = "No empty input please"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835612 Share on other sites More sharing options...
RussellReal Posted May 17, 2009 Share Posted May 17, 2009 idk.. maybe all the data isn't filled out Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835620 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 Hum... Here's the rest with the HTML code...does it make any sense ? <html> <head> <title>Add New Events</title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script> <link rel="stylesheet" href="datepick/jquery.datepick.css" type="text/css" media="screen" charset="utf-8" /> <script type="text/javascript" src="datepick/jquery.datepick.pack.js"></script> <script type="text/javascript"> $(document).ready(function(){ //configure the date format to match mysql date $('#date').datepick({dateFormat: 'yy-mm-dd'}); }); </script> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <table align="center"> <tr> <td colspan="2"> <h2>Add a New Booking</h2> </td> </tr> <tr> <td>Booking Title : </td> <td><input id="bookTitle" name="bookTitle" size="50"></td> </tr> <tr> <td>Date : </td> <td><input id="date" name="date" size="30"></td> </tr> <tr> <td>Start Time : </td> <td><input id="bookStart" name="bookStart" size="10"></td> </tr> <tr> <td>End Time : </td> <td><input id="bookEnd" name="bookEnd" size="10"></td> </tr> <tr> <td>Booked Lanes : </td> <td><input id="bookLanes" name="bookLanes" size="5"></td> </tr> <tr> <td colspan="2"><input type="submit" value="Add Event" name="add"></td> </tr> </table> </form> <?php //check if there is any alert message set if(isset($alert) && !empty($alert)) { //message alert echo '<script type="text/javascript">alert("'.$alert.'");</script>'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835622 Share on other sites More sharing options...
ch1326 Posted May 17, 2009 Share Posted May 17, 2009 Have you connected and selected the database before adding data? mysql_connect(); mysql_select_db(); and don't forget mysql_query(); I hope this might help. Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835623 Share on other sites More sharing options...
RussellReal Posted May 17, 2009 Share Posted May 17, 2009 try doing this before the if statement do echo "<br /><br /><br /><br /><br /> HI!! <br /><br /><br /><br />"; and after the if do echo "<br /><br /><br /><br /> BYE!! <br /><br /><br /><br />"; Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835624 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 nope, it still doesnt load. and i tried it in both statements. when i visit the page a blank one comes up Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835627 Share on other sites More sharing options...
Philip Posted May 17, 2009 Share Posted May 17, 2009 You need to double check you parenthesis on your if statement, they don't line up (error_reporting would have found this ) Hopefully color coding the parenthesis will help you find your errors (and yes, the ones that don't line up are bolded and underlined): if(((isset($_POST['bookTitle']) && !empty($_POST['bookTitle'])) && (isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['bookStart']) && !empty($_POST['bookStart'])) && ((isset($_POST['bookEnd']) && !empty($_POST['bookEnd'])) && ((isset($_POST['bookLanes']) && !empty($_POST['bookLanes']))) Also, on your query: $query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES ('$_POST[booktitle]','$_POST[date]','$_POST[bookStart]','$_POST[bookEnd]','$_POST[bookLanes]')"; Make sure to use curly brackets { } or concatenate the variables with a period: $query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES ('".$_POST['booktitle']."','".$_POST['date']."','".$_POST['bookStart']."','".$_POST['bookEnd']."','".$_POST['bookLanes']."')"; Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835634 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 I've scraped the if statement and the file loaded fine, but it gave me an error of no input the moment it loaded! So there's a prblem with my if statement I'll be looking at. Also, the query doesn't seem to work as it'll refuse to add data on to my database. and I even tried this code: $query = "INSERT INTO bookings (bookTitle, date, bookStart, bookEnd, bookLanes) VALUES ('". addslashes($_POST['booktitle'])."','".$_POST['date']."','". addslashes($_POST['bookStart']) ."','". addslashes($_POST['bookEnd']) ."','". addslashes($_POST['bookLanes']) ."')"; Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835644 Share on other sites More sharing options...
Philip Posted May 17, 2009 Share Posted May 17, 2009 What's your current code then? Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835654 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 the only thing I modified was my query. the rest was the same Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835779 Share on other sites More sharing options...
waynew Posted May 17, 2009 Share Posted May 17, 2009 Could you give us all a screenshot of the structure of your table? Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835781 Share on other sites More sharing options...
toxi Posted May 17, 2009 Author Share Posted May 17, 2009 here you go - if that makes sense to you! Quote Link to comment https://forums.phpfreaks.com/topic/158444-adding-data-doesnt-work-help-needed/#findComment-835783 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.