Advertisement
conception

error

Mar 22nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. <?php
  2. require_once('header.php'); // include header file
  3. require_once('connectvars.php'); // include connections to database
  4. ?>
  5.  
  6. <h2>Input Listing</h2>
  7. <?php
  8. $output_form = true; // declares a flag to test whether or not to show the form
  9. $address = NULL;
  10. $price = NULL;
  11. $bedroom = NULL;
  12. $bath = NULL;
  13. $sqft = NULL;
  14. $year = NULL;
  15. $pool = 0; // initialization for checkbox
  16. $basement = 0; // initialization for checkbox
  17. $fence = 0; // initialization for checkbox
  18. $featured = NULL;
  19.  
  20. if ( isset($_POST['submit']) ) {
  21. // condition based on whether or not the user submits form
  22.  
  23. // php variable assignments to names used in HTML file
  24. $address = $_POST['address']; // street address
  25. $price = $_POST['price']; // price
  26. $bedroom = $_POST['bedroom']; // number of bedrooms
  27. $bath = $_POST['bath']; // number of baths
  28. $sqft = $_POST['sqft']; // sq. ft.
  29. $year = $_POST['year']; // year built
  30.  
  31. if (isset($_POST['pool'])) {
  32. $pool = $_POST['pool']; // pool
  33. }
  34.  
  35. if (isset($_POST['basement'])) {
  36. $basement = $_POST['basement']; // finished basement
  37. }
  38.  
  39. if (isset($_POST['fence'])) {
  40. $fence = $_POST['fence']; // fenced in yard
  41. }
  42.  
  43. if (isset($_POST['featured'])) {
  44. $featured = $_POST['featured']; // featured listing
  45. } else {
  46. $featured = NULL;
  47. }
  48.  
  49. $output_form = false; // will only change to TRUE based on validation.
  50.  
  51. // Validation of inputs, minimum reqs: street address and price.
  52.  
  53. if ( empty($address) ) {
  54. echo "You must input the listing's Street Address.<br />"; // Validation to include street address.
  55. $output_form = true; // will print form
  56. }
  57.  
  58. if ( empty($price) ) {
  59. echo "You must input the listing's price.<br />"; // Validation to include the listing's price.
  60. $output_form = true; // will print form
  61. }
  62.  
  63. if ( !is_numeric($price) ) {
  64. echo "Listing price must be a numeric value.<br />"; // Validation to make numeric.
  65. $output_form = true; // will print form
  66. }
  67.  
  68. // Once everything in form is correct, run
  69. if ( !empty($address) && !empty($price) && is_numeric($price) ) {
  70.  
  71. // Connect to database
  72. $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
  73. or die('Error connecting to MySQL server.'); // terminates code if failed.
  74.  
  75. // creates the SQL query and stores as string in a PHP variable
  76. $query = "INSERT INTO details (address, price, bedroom, bath, sqft, year, pool, basement, fence, featured) " .
  77. "VALUES ('$address', '$price', '$bedroom', '$bath', '$sqft', '$year', '$pool', '$basement', '$fence', '$featured')";
  78.  
  79.  
  80.  
  81.  
  82. // issues the query using the msqli_query function that allows it to add data to the database table
  83. $result = mysqli_query($dbc, $query)
  84. or die('Error querying database.'); // terminates if failed
  85.  
  86. mysqli_close($dbc); // close database connection
  87.  
  88. // Output for data submitted, verifies that information has been received and processed by system.
  89. echo '<h3>Your listing has been input into the system.</h3><br/>';
  90. } // end of if statement that allows run if form is correct
  91. } // end of isset condition that was based on whether or not the form was submitted
  92.  
  93. if($output_form) { // only show the form if user has error or not submitted.
  94. ?>
  95.  
  96. <!-- load form and use self-referencing PHP variable. -->
  97. <br />
  98. <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  99. <label for="address">Street Address:</label>
  100. <input type="text" id="address" name="address" value="<?php echo $address; ?>" /><br />
  101. <label for="price">Price:</label>
  102. <input type="text" id="price" name="price" value="<?php echo $price; ?>" /><br />
  103. <label for="bedroom">Number of Bedrooms:</label>
  104. <input type="text" id="bedroom" name="bedroom" value="<?php echo $bedroom; ?>" /><br />
  105. <label for="bath">Number of Bathrooms:</label>
  106. <input type="text" id="bath" name="bath" value="<?php echo $bath; ?>" /><br />
  107. <label for="sqft">Sq. Footage:</label>
  108. <input type="text" id="sqft" name="sqft" value="<?php echo $sqft; ?>" /><br />
  109. <label for="year">Year Built:</label>
  110. <input type="text" id="year" name="year" value="<?php echo $year; ?>" /><br />
  111. <label>Extras:</label>
  112. <input type="checkbox" name="pool" style="width: auto" value="1" <?php if ($pool) echo "checked"; ?> > Pool
  113. <input type="checkbox" name="basement" style="width: auto" value="1" <?php if ($basement) echo "checked"; ?> > Finished Basement
  114. <input type="checkbox" name="fence" style="width: auto" value="1" <?php if ($fence) echo "checked"; ?> > Fenced-in Yard<br />
  115. <label for="featured">Featured:</label>
  116. <input type="radio" name="featured" value="yes" <?php if ($featured == "yes") echo "checked"; ?> > Yes
  117. <input type="radio" name="featured" value="no" <?php if ($featured == "no") echo "checked"; ?> > No <br />
  118. <input type="submit" value="Submit" name="submit" />
  119. </form>
  120. <?php } // closes the condition related to output_Form, before loading form
  121. require_once('footer.php');
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement