2015-11-12 33 views
-1
<?php 

    include "config.php"; 

    /* 
    CREATE TABLE `addnews` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , 
    `auther` VARCHAR(255) NOT NULL , 
    `title` VARCHAR(255) NOT NULL , 
    `content` LONGTEXT NOT NULL 
    ) ENGINE = MYISAM ; 
    */ 

    $a = $_POST['author']; 
    $t = $_POST['title']; 
    $c = $_POST['content']; 

    if(isset($_POST["add"]) && $_POST["add"] == "news"){ 
     $insert = mysql_query('INSERT INTO addnews 
     (author,title,content) 
     VALUES 
     ("$a","$t","$c")') or die("error"); 
     if (isset($insert)){ 
      echo "<h3>Done</h3>"; 
     } 
    }; 

    echo " 
    <form action='".$_SERVER['PHP_SELF']."' method='post'> 
    Author : <input type='text' name='author' /><br> 
    Title : <input type='text' name='title' /><br> 
    Content : <textarea name='content'></textarea> 
    <input type='submit' value='Add news' /> 
    <input type='hidden' name='add' value='news' /> 
    </form> 
    "; 


    if(gettype($connectdb) == "resource") { 
     mysql_close($connectdb); 
    } 
    ?> 

我是從這個statment得到錯誤,我認爲不能添加任何內容到MySQL數據庫

if(isset($_POST["add"]) and $_POST["add"] == "news"){ 
    $insert = mysql_query('INSERT INTO addnews 
    (author,title,content) 
    VALUES 
    ("$a","$t","$c")') or die("error happend while trying to add information to database"); 
    if (isset($insert)){ 
     echo "<h3>Done</h3>"; 
    } 
}; 

輸出爲:錯誤happend試圖將信息添加到數據庫中並沒有問題config.php文件(連接到 數據庫的文件)我使用的是phpmyadmin

http://prntscr.com/91uj3m

http://prntscr.com/91ujhe

+2

錯誤是什麼? – Andrius

+0

在您的config.php中,您連接到mySQL,選擇數據庫,然後立即關閉與mySQL的連接? –

+0

當依賴第一張圖片是由一個錯誤引起的,你不知道我幾個小時沒有出現解決方案 – user4158643

回答

0

變化die("error happend while trying to add information to database");die("Mysql error: " . mysql_error());以獲取有關錯誤的詳細信息。

有可能您的查詢失敗,因爲您沒有轉義參數。

修改代碼以: $a = mysql_real_escape_string($_POST['author']); $t = mysql_real_escape_string($_POST['title']); $c = mysql_real_escape_string($_POST['content']);

編輯#1:其實你的問題是,您使用的是單引號字符串變量。 將其更改爲有關報價$insert = mysql_query("INSERT INTO addnews (author,title,content) VALUES ('$a','$t','$c')") or die("error: " . mysql_error());

的更多信息:http://php.net/manual/en/language.types.string.php

編輯#2:如果你還在學習如何使用MySQL在PHP中工作,你應該學習如何使用PDO來代替。 Mysql擴展名已死(在PHP 5.5中已棄用,並從PHP 7.0中刪除)

+0

謝謝,但我有另一個問題,你非常多http: //prntscr.com/91w294 – user4158643

+0

更新了我的答案。 – Naktibalda

+0

現在完成http://prntscr.com/91w7r7,非常感謝非常非常非常非常naktibalda – user4158643

0

使用下面的代碼作爲查詢插入。

$insert = mysql_query('INSERT INTO addnews (author,title,content) VALUES ("$a","$t","$c")'); 
+0

哦,以及如果他的變量是字符串呢? – Epodax

+0

@Epodax我已編輯我的答案,請檢查。 – sandeepsure

+1

@sandeepsure它和user4158643一樣 –