2016-03-15 27 views
2

所以目前本地服務器不工作,這是我的代碼有

的index.php:

 <form action="insert.php" method="post"> 
      Comments: 
      <input type="text" name="comment"> 
      <input type="submit"> 
     </form> 

insert.php:

<?php 

include ('index.php'); 

$user = 'x'; 
$password = ''; 
$db = 'comment_schema'; 
$host = 'localhost'; 
$port = 3306; 

/* Attempt MySQL server connection. Assuming you are running MySQL 
server with default setting (user 'root' with no password) */ 

$link = mysqli_connect("localhost", "x", "", "comment_schema"); 

// Check connection 
if($link === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

// Escape user inputs for security 
$comment = mysqli_real_escape_string($link, $_POST["comment"]); 
$sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')"; 

// attempt insert query execution 
if(mysqli_query($link, $sql)){ 
    echo $comment; 
} else{ 
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
} 

// close connection 
mysqli_close($link); 

當我做echo $ comment時,沒有任何東西被打印出來。但是,如果我做了像回聲「嗨」這樣的作品。我認爲出於某種原因,$ _POST未被識別。任何建議,使這項工作,如果我做錯了。

我的目標是接受用戶輸入並將其插入到phpmyadmin的數據庫中。目前,它能夠插入,但是它插入一個空值。我的數據庫中只有兩列。一個ID和一個評論欄。該ID是自動遞增的。評論是我從用戶那裏得到的。

+1

你爲什麼要包括的index.php一遍嗎? – sudhakar

+0

看看'var_dump()'打印什麼 –

+0

什麼都不打印出來。它是空的。 –

回答

0

檢查一次數據庫中的評論數據類型(我更喜歡Varchar())。 試試這個,因爲它是: -

<form action="insert.php" method="POST"> 
       Comments: 
       <input type="text" name="comment"/> 
       <input type="submit" name="submit" value="submit"> 
      </form> 

<?php 

include ('index.php'); 

$user = 'x'; 
$password = ''; 
$db = 'comment_schema'; 
$host = 'localhost'; 
$port = 3306; 


$link = mysqli_connect("localhost", "x", "", "comment_schema"); 


if($link === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 


$comment = mysqli_real_escape_string($link, $_POST["comment"]); 
$sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')"; 


if(mysqli_query($link, $sql)){ 
    echo $comment; 
} else{ 
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
} 

// close connection 
mysqli_close($link); 
+0

你的代碼和OP的代碼沒有區別。 –

+0

嘿,謝謝你的建議,但不幸的是它仍然無法正常工作。沒有值被插入。評論的數據類型是varchar –

0

試試這個

<?php 
    $user = 'x'; 
    $password = ''; 
    $db = 'comment_schema'; 
    $host = 'localhost'; 

    $link = mysqli_connect($host, $user, $password, $db); 

    if($link === false){ 
     die("ERROR: Could not connect. " . mysqli_connect_error()); 
    } 

    // Escape user inputs for security 
    $comment = mysqli_real_escape_string($link, $_POST["comment"]); 
    $sql = "INSERT INTO parentComment(ID, Comment) VALUES(NULL,'$comment')"; 

// attempt insert query execution 
if(mysqli_query($link, $sql)){ 
    echo $comment; 
} else{ 
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
} 

// close connection 
mysqli_close($link); 
+0

:(仍然不工作 –

0

一些調試建議:// mysqli_real_escape_string 後

  • var_dump($_POST); // mysqli_real_escape_string
  • var_dump($comment);

    mysqli api可能無法正常工作!修正像

    • $sql = "INSERT INTO parentComment(ID, Comment) VALUES('','".$comment."')";
    • echo $sql;查詢//檢查您的SQL語法
    • echo mysqli_error($link); //你有錯誤
    • 看看你的.htaccess文件看你是否有<Limit POST>標籤
    • 刪除這條線include ('index.php');。據推測,這兩個文件在一個文件夾中。所以只需運行index.php。試過你的代碼沒有這條線,它爲我工作。
+0

所以我嘗試了所有這些建議。我從中得到的是我的POST是空的,我的註釋也是如此。基本上,一切都是空的,我不知道爲什麼。你指的是@bill更正是什麼? –

+0

這是我添加到我的答案查詢更正! –

0

使用下面的代碼: -

include ('index.php'); 

$user = 'x'; 
$password = ''; 
$db = 'comment_schema'; 
$host = 'localhost'; 
$port = 3306;  

$link = mysqli_connect("localhost", "x", "", "comment_schema"); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

if(!empty($_POST["comment"])){ 
    $comment = mysqli_real_escape_string($link, $_POST["comment"]);  
    // Escape user inputs for security 
    $sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')"; 
    $result = mysqli_query($link, $sql); 
    // attempt insert query execution 
    if($result){ 
     echo $comment; 
    } else{ 
     echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
    } 
    // close connection 
    mysqli_close($link); 

}else{ 
    die('comment is not set or not containing valid value'); 
} 

希望它會幫助你:-)

+0

:(它不工作。它直接到else語句並打印出'註釋未設置或不包含有效值'謝謝你的幫助,雖然,但仍不知道是什麼問題。 –

+0

你是否在評論字段中傳遞有效值? –

+0

是的,我只是試圖評論這個詞應該是有效的,我在我的數據庫中設置了註釋爲varchar –