2017-06-03 104 views
0

我試圖讓後廣告表單將數據添加到數據庫。該頁面不斷重新加載並要求填寫所有細節。我似乎無法找到該錯誤,並且我在Google和YouTube上進行了大量搜索,但都無濟於事。請幫忙!!!將錯誤發佈到數據庫的表單數據

<?php 
    session_start(); 
    include'db.php'; 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $phoneNumber = $_POST['mobile-num']; 
    $photos = $_POST['fileselect']; 
    $town = $_POST['location']; 
    $category = $_POST['category']; 
    $adTitle = $_POST['title']; 
    $adDescription = $_POST['description']; 

    if(isset($_SESSION['email'])) 
    { 
     if($email != "" && $name != "" && $phoneNumber != "" && $photos != "" && $town != "" && $category != "" && $adTitle !="" && $adDescription != "") 
     { 
      $name = stripslashes($name); 
      $email = stripslashes($email); 
      $phoneNumber = stripslashes($phoneNumber); 
      $photos = stripslashes($photos); 
      $town = stripslashes($town); 
      $adTitle = stripslashes($adTitle); 
      $category = stripslashes($category); 
      $adDescription = stripslashes($adDescription); 

      $name = mysqli_real_escape_string($connection,$name); 
      $email = mysqli_real_escape_string($connection,$email); 
      $phoneNumber = mysqli_real_escape_string($connection,$phoneNumber); 
      $photos = mysqli_real_escape_string($connection,$photos); 
      $town = mysqli_real_escape_string($connection,$town); 
      $adTitle = mysqli_real_escape_string($connection,$adTitle); 
      $category = mysqli_real_escape_string($connection,$category); 
      $adDescription = mysqli_real_escape_string($connection,$adDescription); 
      $imagePath = "images/".basename($_FILES['fileselect']['MAX_FILE_SIZE']); 
      $photo = $_FILES['fileselect']['MAX_FILE_SIZE']; 
      $date = date("j F Y"); 



      if(filter_var($email,FILTER_VALIDATE_EMAIL)) 
      { 
       mysqli_query($connection, "SELECT email,ad-title,ad-category,ad-description,Photos,Name,Mobile-Num,Town,date from ads"); 
       $insertQuery = mysqli_query($connection, "INSERT INTO ads(email,ad-title,ad-category,ad-description,Photos,Name,Mobile-Num,Town,date) 
                  VALUES('$email','$adTitle','$category','$adDescription','$photo','$name','$phoneNumber','$town','$date')"); 
       header("Location: /profile.php"); 
      } 
      else 
       $_SESSION['errorMessage'] = "Please check email pattern"; 
       header("Location: /post-ad.php"); 
     } 
     else 
      $_SESSION['errorMessage'] = "Please input all the required details"; 
      header("Location: /post-ad.php"); 
    } 
    else 
     header("Location: /login.php"); 

?> 

這就是PHP代碼。 因爲我對Stackoverflow不太好,所以我遇到了格式化我想在這裏發佈的html表單代碼的問題。我將附上一張圖片。 Html form code for the post-ad form

+1

使用準備好的語句和你的代碼會更整潔和更短。要找出問題所在,請刪除這些重定向! – e4c5

+0

所以錯誤是驗證的權利? – dekts

+0

^這。只需註釋掉所有'header()'函數並查找任何錯誤。要顯示SQL錯誤,你可以在'mysqli_query()'末尾添加'或die(mysqli_error());'。 – StuntHacks

回答

1

不知道你爲什麼要運行SELECT,因爲你似乎什麼都不做,也沒有參數。但INSERT應該是...

$insertQuery = mysqli_query($connection, "INSERT INTO ads(email,`ad-title`,`ad-category`,`ad-description`,`Photos`,`Name`,`Mobile-Num`,`Town`,`date`) 
                  VALUES('$email','$adTitle','$category','$adDescription','$photo','$name','$phoneNumber','$town','$date')"); 

當你與他們連字符應該包含在反單引號的列名,要麼是的,我會建議(如果不是tooo下旬),除去連字符和改用下劃線。

您還應該在運行任何SQL時檢查錯誤並對它們進行某種處理。

+0

我刪除了選擇語句,因爲它沒有用處,從代碼和數據庫本身的數據庫列名稱中刪除了連字符。還是行不通。我只是得到一個空白的白頁。 – Jpriest

0

謝謝你們的幫助。抱歉讓你們全部承受壓力。我檢查了我的數據庫結構,發現一個錯誤類型的列阻止了sql插入查詢。我的歉意......

相關問題