Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. <?php
  2.  
  3. //Connect to server and database
  4.  
  5. $host = "localhost"; // Mysql hostname
  6. $username = "root"; // Mysql username
  7. $password = ""; // Mysql password
  8. $database = "events"; // Name of mysql database
  9. $table ="events";
  10.  
  11. $dbc = mysqli_connect($host, $username, $password);
  12. mysqli_select_db($dbc, $database);
  13.  
  14.  
  15. if (!$dbc) {
  16.     trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
  17. }
  18.  
  19. if(isset($_POST['submit'])) //ask if the form has been submitted
  20. {
  21.  
  22. $submit = $_POST['submit'];
  23. $title = $_POST['title'];
  24. $where = $_POST['where'];
  25. $date = $_POST['date'];
  26. $time = $_POST['time'];
  27.  
  28. //define the variables used in the query to add the data to the database
  29.  
  30. if (($submit) && ($title) && ($where) && ($date) && ($time)){
  31.  
  32. $q = "INSERT INTO $table (title, where, date, time) VALUES ('$title', '$where', '$date', '$time')";
  33.  
  34. $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
  35. echo "Posted!";
  36. //If everything ran well then run the INSERT INTO query and show 'Posted'.
  37.  
  38. }
  39. } else { //if the form has not yet been submitted, show the form
  40. echo "<br />";
  41. echo "<form method='post' action='index.php'>";
  42. echo "<input type='text' name='title' value='Event Title' class='styledtextbox'>";
  43. echo "<input type='text' name='where' value='Where?' class='styledtextbox'>";
  44. echo "<input type='text' name='date' value='Date (dd/mm/yy)' class='styledtextbox'>";
  45. echo "<input type='text' name='time' value='Time' class='styledtextbox'>";
  46.  
  47. echo '<p>Now press the Submit button to have your event immediately added to the Home Page!</p>';
  48.  
  49. echo "<input type='submit' value='Add Event' name='submit'>";
  50.  
  51. echo 'Thank you for adding your event!';
  52.  
  53. echo "</form>";
  54.  
  55. echo "</p>";
  56.  
  57.  
  58.  
  59. // The error says:
  60.  
  61. // Notice: Query: INSERT INTO events (title, where, date, time) VALUES ('Event Title', 'Where?', 'Date (dd/mm/yy)', 'Time')
  62. MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where, date, time) VALUES ('Event Title', 'Where?', 'Date (dd/mm/yy)', 'Time')' at line 1 in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 34
  63.  
  64. //However then posts:
  65. // Posted!
  66.  
  67. // Meaning it thinks it has added to data to the db, however it hasn't. :/
  68.  
  69. }
  70.  
  71. ?>
  72.  
  73. <!--End Main Content for admin.php-->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement