2016-04-10 15 views
0
$street_address = NULL; 
    $price = NULL; 
    $number_bedrooms = NULL; 
    $number_baths = NULL; 
    $sq_ft = NULL; 
    $year_built = NULL; 
    $featured = NULL; 
    $pImage = NULL; 
    //create storage for the checkbox values for featured house items. 
    $pool = 0; 
    $finished_basement = 0; 
    $fenced_yard = 0; 

    if (isset($_POST['submit'])) {//checks to see if the user submit the form. 
    // print_r ($_POST); // This will show you what array information is being sent to post on the screen 
    //print_r ($_FILES); //can be used to view the contents of an array 

    // Gather the home listing data from the POST 
    if(isset($_POST['pImage'])) { 
$pImage = $_POST['pImage']; 
    } 
    $street_address = $_POST['street_address']; 
    $price = $_POST['price']; 
    $number_bedrooms = $_POST['number_bedrooms']; 
    $number_baths = $_POST['number_baths']; 
    $sq_ft = $_POST['sq_ft']; 
    $year_built = $_POST['year_built']; 
    $pDesc = $_POST['pDesc']; 

    if (isset($_POST['featured'])) { //used to check whether the user selected if the home was selected as featured. If they didn't the item is not passed to the POST. 
     $featured = $_POST['featured']; 
     } else { 
     $featured = NULL; 
    } 
    if (isset($_POST['pool'])) { 
     $pool = $_POST['pool']; 
     } else { 
     $pool = 0; 
    } // this end bracket is "attached" to the process checking to see if the checkbox was 'ticked'. 
//process checked finished basement box. 
    if (isset($_POST['finished_basement'])) { 
     $finished_basement = $_POST['finished_basement']; 
     } else { 
     $finished_basement = 0; 
    } // end of finished_basement checkbox check 
    //process checked fenced yard box. 


    if (isset($_POST['fenced_yard'])) { // Fenced Yard Checkbox Process Check 
     $fenced_yard = $_POST['fenced_yard']; 
     } else { 
     $fenced_yard = 0; 

    } // end of finished_basement checkbox check 

    } // end of $_POST submission check (starts on line #40) 
    // Street Address Validation Check - See if address was entered into the form. 
    if (empty($street_address)) { 
     echo "You didn't fill in a streetaddress. <br />"; 
     $output_form = true; // will print form. 
    } // end of street_address check 
    // Image , Price & Image validation check 

    // Price Validation Check - See if Price was entered into the form as a number. 
    if (!is_numeric($price)) { //is not a number function. 
     echo "You didn't fill in price as a number."; 
     $output_form = true; // will print form. 
    } 

    // Used to store information for uploading images associative array. 
$pImage = $_FILES['pImage']['name']; //array variable. Code pulled from 02fileglobaladd.txt document. $_FILES used to store file information from uploa 
$pImage_type = $_FILES['pImage']['type']; 
$pImage_size = $_FILES['pImage']['size']; 

// Image File Size Validation check -- checks to be sure that the uploaded image is no larger than 1 MB 
    if (!empty($pImage)) { // empty is for name of file. 
     if ((($pImage_type == 'image/gif') || ($pImage_type == 'image/jpeg') || ($pImage_type == 'image/pjpeg') || ($pImage_type == 'image/png')) 
     && ($pImage_size > 0) && ($pImage_size <= GW_MAXFILESIZE)) { // check to make sure that the file type is valid and that the file is larger than 0 but less than 1meg 
     if ($_FILES['pImage']['error'] == 0) { // test to be sure file gets uploaded. 

      $target = GW_UPLOADPATH . $pImage; // try to move file to images folder // FILE UNIQUE time function added from book (page 252) that duplicates and chages file name if there 
      // is more than one version 
      if (move_uploaded_file($_FILES['pImage']['tmp_name'], $target)) { 

      // Connect to the database 
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
      // Write the data to the database 
     $query = "INSERT INTO homes (street_address, price, number_bedrooms, number_baths, sq_ft, year_built, pool, finished_basement, fenced_yard, featured, pDesc, pImage) " . 
     "VALUES ('$street_address', '$price', '$number_bedrooms', '$number_baths', '$sq_ft', '$year_built', '$pool', '$finished_basement', '$fenced_yard', '$featured', '$pDesc','$pImage')"; 

     $result = mysqli_query($dbc, $query) 
     or die('Error querying database.'); 
/* Testing echos 
echo $query; 
echo '<br />' 
/**/ 

      // Confirmation for the user that will be displayed once the user clicks the submit button (As long as there are no errors with the PHP code) 
      echo '<p>Thank you for Your Submission. The home has been added to the database.</p>'; 
     echo 'Street Address: ' . $street_address . '<br />'; 
     echo 'Price: ' . $price . '<br />'; 
     echo 'Number of Bedrooms: ' . $number_bedrooms . '<br />'; 
     echo 'Number of Baths: ' . $number_baths . '<br />'; 
     echo 'Square Feet: ' . $sq_ft . '<br />'; 
     echo 'Year Built: ' . $year_built . '<br />'; 
     // featured if statement 
     echo 'Featured:' ; 
        if ($featured = 1) {echo ' Yes <br />'; } else {echo ' No <br /> ';} 
     //example of using ternary format of if/else -- additional items of home (pool, finished basement, fenced in yard) checkboxes 
     echo 'Pool: ' . (($pool) ? 'YES' : 'NO'). '<br />' ; 
     echo 'Finished Basement: ' . (($finished_basement) ? 'YES' : 'NO'). '<br />' ; 
     echo 'Fenced Yard: ' . (($fenced_yard) ? 'YES' : 'NO'). '<br />' ; 

       echo 'Desc:<span class="desc"> ' . ($pDesc). '</span><br />'; 
       echo 'Image File: '; 
      echo '<img src= " ' . GW_UPLOADPATH . $pImage . ' " alt="Homes Image" /></p>'; 
      echo '<p><a href="index.php">&lt;&lt; Back to Listings Page.</a></p>'; 

      // Clear the data in the form 
      $street_address = ""; 
      $price = ""; 
      $number_bedrooms = ""; 
      $number_baths = ""; 
      $sq_ft = ""; 
      $year_built = ""; 
      $pool = ""; 
      $finished_basement = ""; 
      $featured = ""; 
      $pImage = ""; 
      $pDesc =""; 

      mysqli_close($dbc); 
      } //movefile worked.... 
      else { //movefile didn't work... error message is printed out 
      echo '<p class="error">Sorry, there was a problem loading your product image.</p>'; 
      } 
     } 
     } // no file type or size error/ENDING BRACKET 
     else { // there was a file type or size error/ENDING BRACKET 
     echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE/1024) . ' KB in size.</p>'; 
     } // type size error message printed./ENDING BRACKET 


     @unlink($_FILES['pImage']['tmp_name']); // unlink is php command to delete the file. Its getting rid of the temporary file. @ symbol ignores any error message. 
    } // data validated 
    else { // data didn't validate, show error message. 
     echo '<p class="error">Please enter all the required product information.</p>'; 
    } 

?> 

Bir ekledi. Bu sorunu giderdikten sonra kodumu çok düzeltmem gerektiğini biliyorum. Profesörüm Tanımsız endeksi: hem GET & POST açıklamalara isset komutu, birisi bana yardımcı olabilir pImage hatları 92,93,94

Ben sorunu bu ifadesinde yatmaktadır

+0

Yankılanmaya çalışın $ _POST ['pImage'] ':' echo $ _POST ['pImage']; ' – Panda

+0

Bunu koda koyabilir miyim? –

+0

Bunu daha önce koyabilirsiniz: if (isset ($ _ POST ['pImage'])) {$ pImage = $ _POST ['pImage']; } '. Bu, değerin – Panda

cevap

0

PHP bir acemi değilim ... herhangi bir yardım için teşekkür ederim ... Baskı-r ifadeleri kullanma hakkında söz:

if(isset($_POST['pImage'])) { 
    $pImage = $_POST['pImage']; 
} 

$_POST['pImage'] görüntü olarak, $_POST kullanamazsınız. Video yüklendikten olup olmadığını kontrol edin için, sadece kullanın:

if($_FILES['pImage']['name') { 

Bu görüntünün adı için kontrol edecek kullanılabilir true dönecektir.


Bu ifade yanlıştır:

if ($featured = 1) {echo ' Yes <br />'; } else {echo ' No <br /> ';} 

Eğer $featured1 eşittir ise karşılaştırdığınız olarak, PHP'nin karşılaştırma operatörü == kullanmanız gerekir. Atama için = kullanılır.

Böylece, bu olmalıdır: PHP Operatörleri üzerinde

if ($featured == 1) {echo ' Yes <br />'; } else {echo ' No <br /> ';} 

fazla bilgi: http://php.net/manual/en/language.operators.comparison.php. Hata raporlamayı etkinleştirmek için

@unlink($_FILES['pImage']['tmp_name']); 

Kaldır @:


Ayrıca, hata iletileri bastırmak gerekir. Sözdizimi hatalarının teşhisinde çok kullanışlıdır.

+0

almalıyım $ pImage = $ _POST ['pImage']; yanı sıra? –

+0

@JasonQuinn Yup – Panda

+0

Değiştirildi. Etrafında kod hareket ettikten sonra çalıştı. Teşekkür ederim! –

İlgili konular