![](/img/trans.png)
[英]I have error:Notice: Undefined offset: 1 in C:\wamp\www\index.php on line 32,on line 34,on line 36,on line 38
[英]Php error Notice: Undefined index: UserID in C:\wamp\www\tweetball\classes\word.class.php on line 35
我一直收到這個錯誤。 我不知道為什么......謝謝
注意:未定義的索引:第35行的C:\\ wamp \\ www \\ tweetball \\ classes \\ word.class.php中的UserID
<?php
session_start();
class Word
{
private $m_sWord;
private $m_sMessage;
private $link;
public function __set($p_sProperty, $p_vValue)
{
switch($p_sProperty)
{
case "Message":
$this->m_sMessage = $p_vValue;
break;
}
}
public function __get($p_sProperty)
{
$vResult = null;
switch($p_sProperty)
{
case "Message":
$vResult = $this->m_sMessage;
break;
}
return $vResult;
}
public function Save()
{
include("Connection.php");
$sSql = "insert INTO words (word, FK_UserID) VALUES ('$_POST[message]','$_POST[UserID]')";
if ($rResult = mysqli_query($this->link, $sSql))
{
echo "";
}
else
{
throw new Exception('We could not save your word!');
}
mysqli_close($this->link);
}
public function CreateNew()
{
$m_sHost = "localhost";
$m_sUser = "root";
$m_sPassword = "";
$m_sDatabase = "tweetball";
$link = mysqli_connect($m_sHost,$m_sUser,$m_sPassword,$m_sDatabase);
if($link!=null){
$this->link=$link;
}
else
{
throw new Exception("There seems to be a database problem. Try again later");
}
}
public function getWord()
{
$m_sHost = "localhost";
$m_sUser = "root";
$m_sPassword = "";
$m_sDatabase = "tweetball";
$sSql = "select * from tweetball where WordID = 1;";
if ($rResult = mysqli_query($this->link, $sSql))
{
$singleRecord = mysqli_fetch_assoc($rResult);
$message = $singleRecord['Word'];
}
else
{
throw new Exception('We could get your word!');
}
mysqli_close($link);
return($message);
}
}
?>
您必須在類中定義您的id,要么使用$_REQUEST
,要么使用$_REQUEST
$_POST
,確保數據庫中還有您要發送的值的列。
您收到此錯誤是因為您引用了未為該數組定義的數組鍵“UserID”(在本例中為$ _POST)。 快速查看一下您的SQL查詢,我注意到您的密鑰不是引號,而是它們需要的引號。
我不會評論輸入未選中是多么不安全。 用戶數據到您的數據庫中
這不是一個真正的錯誤,而是更多的通知。 它告訴您,您正在嘗試使用未手動設置的變量。 如果設置不當,可能導致可變噴射。
您可以通過關閉PHP錯誤通知(如果您確定您的PHP設置已安全設置,在嘗試使用它們之前定義所有或變量或在調用它之前使用if(isset($variable))
此問題。
$sSql = "insert INTO words (word, FK_UserID)
VALUES ('$_POST[message]','$_POST[UserID]')";
沒有$_POST['UserID']
值。 請注意,數組鍵(例如UserID
)區分大小寫。
另請注意,您在此處執行的操作容易受到SQL注入的影響,應立即更改。 關於SQL注入是什么以及如何在PHP中預防它,SO有很多問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.