2013-07-21 78 views
0
<?php 
include("dbinit.php"); 
$text="helow"; 
    $eadd = $_SESSION['account']; 
    $result = mysqli_query($link,"SELECT * FROM account where Eadd='". $eadd ."'"); 
     while($row = mysqli_fetch_array($result)) 
      { 
       $name = $row['Name']; 
      } 
       $ctext = $_POST['ctext']; 
       $sql = "INSERT INTO chat (By, Content, Reported) VALUES ('$name','$ctext','No')"; 
       mysqli_query($link,$sql); 
       mysqli_close($link); 
       $text=$name . $ctext; 
echo $text; 
?> 

這是我的代碼。在我的其他頁面,這工作,但..當我更改「插入」爲什麼我不能將其存儲到數據庫中的值?插入數據庫不起作用

<?php 
session_start(); 
     $link = mysqli_connect("localhost", "xxx", "xxx", "xxx"); 

     /* check connection */ 
     if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
     } 
     mysqli_select_db($link, "xxx"); 
     header('Content-type: text/html; charset=utf-8'); 
?> 

這裏是我的文件使用dbinit

+0

閱讀[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)。或者只是想想如果發佈的文本包含單引號會發生什麼。 –

+0

是的,我讀了..關於pdo .. :)但是..我要先做這個。之後我知道如何創建聊天室。我現在要把它改爲pdo ..謝謝 –

+0

你不需要pdo來避免SQL注入。爲什麼從一開始就把事情弄錯了?閱讀http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php –

回答

0

BY保留關鍵字,您應該使用反引號周圍

 $sql = "INSERT INTO chat (`By`, Content, Repo......... 
-1

裹在用雙引號你$ sql中的PHP變量,別忘了點。

$sql = "INSERT INTO chat (By, Content, Reported) VALUES ('".$name."','".$ctext."','No')"; 
+0

仍然不起作用 –

+0

這不會有什麼區別,因爲他已經使用雙引號,PHP解釋器將它們視爲變量,因此無需這樣做。請參閱http://php.net/manual/en/language.types.string.php –