2015-12-16 68 views
0


我有一個問題與PHP。
問題:
如果您在看到代碼之前可能會看到兩個查詢函數插入不同的表中,第一個查詢插入沒有任何問題,但第二個查詢不起作用的問題,我不不知道爲什麼請幫助我。
在第一個查詢中,我將一個新聞數據插入到表中,第二個查詢需要插入一張圖片,以便爲讀者提供更多詳細信息。
這是代碼:我需要個人主頁功能幫助

<?php 
    ob_start(); 
    session_start(); 
    include("../includes/config.php"); 
    if(!$_SESSION['admin']){ 
     header("Location: index.php"); 
    } 
?> 
<html> 
<body align="center"> 
<?php 
    if(isset($_POST['add'])){ 
     $title = $_POST['title']; 
     $topic = $_POST['topic']; 
     if(empty($topic) || empty($title)){ 
      echo"please don't leave the feilds empty"; 
     }else{ 
      //file vars 
      $url = $_FILES["upload"]["tmp_name"]; 
      $path = mysqli_real_escape_string($connect,file_get_contents($url)); 
      $name = mysqli_real_escape_string($connect,$_FILES["upload"]["name"]); 
      $type = mysqli_real_escape_string($connect,$_FILES["upload"]["type"]); 
      if(substr($type,0,5) == "image"){ 
       $sql = "INSERT INTO news(title,topic) VALUES('$title','$topic')"; 
       $query = mysqli_query($connect,$sql); 
       $id = mysqli_insert_id($connect); 
       $sqll = "INSERT INTO pic(name,type,content,for) VALUES('$name','$type','$path','$id')"; 
       $queryy = mysqli_query($connect,$sqll); 
       if($query && $queryy){ 
        echo"Worked"; 
       }else{ 
        echo"error"; 
       } 
      }else{ 
       echo"it's not an image"; 
       echo $type; 
      } 
     } 
    } 
?> 
<form method="post" action="addnew.php" enctype="multipart/form-data"> 
    <input type="text" name="title" style="text-align:center" placeholder="title"/><br/> 
    <textarea name="topic" placeholder="topic" style="width:800px;height:500px;resize:none;text-align:right;font-size:23"> 
    </textarea> 
    <br/> 
    <input type="file" name="upload"/><br/> 
    <input type="submit" name="add" value="sed"> 
</form> 
</body> 
</html> 
+2

**不起作用**,不給太多的信息。請在問題中發佈錯誤日誌。 –

回答

2

對於第二個查詢請逃脫列名:

$sqll = "INSERT INTO pic(`name`,`type`,`content`,`for`) VALUES('$name','$type','$path','$id')"; 

,因爲這樣的名字for等被保留MySQL的關鍵字,你顯然對這個越來越錯誤

+1

感謝您的完美答案。 –