2013-12-16 186 views
1

我對PHP很新穎(自9月份以來一直這樣做,所以我很抱歉,如果這看起來像一個愚蠢的問題,我很困難,並且不能找出答案!)並且不能解決爲什麼我的錯誤消息會當用戶在空白時提交表單時不顯示。爲什麼錯誤信息不顯示?

這是我的代碼:

<?php 
$salonid = ""; 
    if (!$db_server){ 
      die("Unable to connect to MySQL: " . mysqli_connect_error($db_server)); 
      $db_status = "not connected"; 
    }else{ 
     //Capture form data, if anything was submitted 
     if (isset($_GET['salonid']) and ($_GET['salonid'] != '')){ 
      $salonid = clean_string($db_server, $_GET['salonid']); 
      //If connected, get Salons from database and write out 
      mysqli_select_db($db_server, $db_database); 
      $query = "SELECT ID, salon_name, address, postcode, telephone, email, website FROM salon WHERE ID=$salonid"; 
      $result = mysqli_query($db_server, $query); 
      if (!$result) die("Query failed: " . mysqli_error($db_server)); 

      while($row = mysqli_fetch_array($result)){ 
       $str_result .= "<h2>" . $row[ 'salon_name'] . "</h2>"; 
       $str_result .= "<p>" . $row['address'] . "</p>"; 
       $str_result .= "<p>" . $row['postcode'] . "</p>"; 
       $str_result .= "<p>" . $row['telephone'] . "</p>"; 
       $str_result .= "<p>" . $row['email'] . "</p>"; 
       $str_result .= "<p>" . $row['website'] . "</p>"; 

      } 
      mysqli_free_result($result); 
     }else{ 
      $str_result = "<h2>No salon selected</h2>"; 

     } 

    } 
    echo $str_result; 
?> 

<?php 

if(trim($_POST['submit']) == "Submit comment"){ 

    //Get any submitted comments and insert 
    $comment = clean_string($db_server, $_POST['comment']); 
    if ($comment != '') { 
     $name=$_FILES['photo']['name']; 
     if ($name = "") $error .= "<p class='error'>You must upload an image!</p>"; 
     $originalname=$_FILES['photo']['name']; 
     $type=$_FILES['photo']['type']; 
     if ($type=="image/jpeg") $type=".jpeg"; //if true change 
     else if ($type=="image/jpg") $type=".jpg";// if not true check this one 
     else if ($type=="image/png") $type=".png"; 
     $name=uniqid() . $type; 

     $path="images/" . $name; 
     $tempname=$_FILES['photo']['tmp_name']; 
     $size=$_FILES['photo']['size']; 
     //Error checking 
     if ($size >1000000) $error .= "<p class='error'>Your image file is to big, it have to be less than 200 mb</p>"; 
     if ($error=="") { 
      if (move_uploaded_file($tempname, $path)){ 

       $uploadquery="INSERT INTO comments (comment, imagename, salonID, userID) VALUES ('$comment', '$path', $salonid, ". $_SESSION['userID'].")"; 
       mysqli_query($db_server,$uploadquery) or die ("Insert failed " . mysqli_error($db_server) . " " . $uploadquery); 
       $message= "<h2>Thanks for your comment!</h2><p>Your upload was succesful</p>"; 
      } 
     } 
    } 

} 

//Print out existing comment 
$query = "SELECT * FROM comments JOIN users ON comments.userID = users.ID WHERE salonID=$salonid"; 
$result = mysqli_query($db_server, $query); 
if (!$result) die("Database access failed: " . mysqli_error($db_server)); 
while ($row = mysqli_fetch_array($result)){ 
     $str_comments .="<h2>" . $row['Username'] ."</h2>"; 
     $str_comments .= "<p>" . $row['comment'] . "</p>"; 
     $str_comments .="<img src='" . $row['imagename'] ."' />"; 
} 

mysqli_free_result($result); 

?> 
<div id="form"> 
<table><form id='review' action='salonpage.php?salonid=<?php echo $salonid; ?>' method='post' enctype='multipart/form-data'> 
<th><h2> Do you want to review the service you recieved?</h2></th> 
<tr><td><textarea name="comment" rows="6" cols="40">Write something here!</textarea></td></tr> 
<tr><td><input type='file' name='photo' accept='image/jpg, image/jpeg, image/png'/></td></tr> 
<br/> 
<tr><td><input type='submit' id='submit' name='submit' value='Submit comment' /></td></tr> 
</form></table> 
<?php echo $message; 
    echo $str_comments; ?> 
</div> 
<?php mysqli_close($db_server); ?> 
+0

檢查''($ name =「」)''correct form' '($ name ==「」)'' - allso you use''error。=''without declare''$ error'' – ins0

+0

這不起作用,我想我可能需要做的是添加另一個if聲明說他們需要填寫所有字段 – user3095683

+0

我認爲你不應該在沒有任何知識的情況下複製粘貼,然後想知道爲什麼沒有任何工作 - 你需要首先檢查是否所有的''POST''值都存在if(!$ allpresent){//顯示錯誤}'' – ins0

回答

0
if ($comment != '') { 
     $name=$_FILES['photo']['name']; 
     if ($name = "") $error .= "<p class='error'>You must upload an image!</p>"; 
     $originalname=$_FILES['photo']['name']; 
在此代碼

您使用

$name = "" 

這是你需要使用比較操作中的賦值運算符,如果條件或者=====

+0

我已經更新了我的代碼以說明這一點,但是當所有框都保留空白時,沒有顯示錯誤消息 – user3095683

+0

您需要添加其他條件以引發異常或回顯錯誤消息以向用戶提供反饋..例如,如果($註釋!=''){..} else {$ error。=「無註釋輸入」}否則用戶將不知道它是因爲註釋爲空。 – Manquer

+1

感謝您的幫助!通過簡單地添加echo $ error來管理它 – user3095683

0

我想,你說的是$錯誤,如果我是正確,那麼你在上面提到的代碼中沒有回顯$ error變量。 「if($ comment!='')」else {$ error。=「no comment entered entered」}另外還有一件事要加上其他部分}