2014-04-08 82 views
-1

我的代碼和表單:PHP未聲明的變量錯誤

<?php 
include("includes/connect.php"); 
$content = "SELECT * FROM content where content_page='home'"; 
$result = mysql_query($content); 
$row = mysql_fetch_array($result); 
?> 
<form method="post" action="admin_home.php"> 
Headline:</br> 
<input type="text" name="content_title" value="<?php echo "$row[content_title]" ?>"></br> </br> 
Main content:</br> 
<textarea type="text" class="txtinput" cols="55" rows="20" name="content_text"><?php echo "$row[content_text]" ?></textarea></br></br> 
<input type="submit" name="submit" value="Save changes"> 
</form> 

代碼是我想做的發生時, '提交' 按鈕被按下:

<?php 

include("includes/connect.php"); 
if(isset($_POST['submit'])){ 

$order = "UPDATE content 
      SET content_title='$content_title', content_text='$content_text' WHERE content_page='home'"; 
mysql_query($order); 

} 

>

錯誤我得到:

Notice: Undefined variable: content_title in /Applications/XAMPP/xamppfiles/htdocs/templacreations/admin/admin_home.php on line 63 

Notice: Undefined variable: content_text in /Applications/XAMPP/xamppfiles/htdocs/templacreations/admin/admin_home.php on line 63 

63行是從我的提交按鈕代碼的下面一行:

SET content_title='$content_title', content_text='$content_text' WHERE 

這是不是宣佈他們?

+4

這些變量不存在。你怎麼會爲這個錯誤感到驚訝? –

+0

@JohnConde疲倦的打字使一個sl program的程序員;-) –

回答

0

看起來您正在尋找表格的POST值。

它們不包含在變量$content_title$content_text中。事實上,你的錯誤告訴你,你沒有分配任何東西給這些變量。

它們包含在$_POST數組中,就像提交的值一樣。

<?php 

include("includes/connect.php"); 
if(isset($_POST['submit'])){ 
    //Sanitize data and assign value to variable 
    $content_title = mysql_real_escape_string($_POST['content_title']); 
    $content_text = mysql_real_escape_string($_POST['content_text']); 

    $order = "UPDATE content 
      SET content_title='$content_title', content_text='$content_text' 
      WHERE content_page='home'"; 
    mysql_query($order); 

} 

?> 
+0

逃跑是遺漏!閱讀有關SQL注入的內容,並在將任何內容從變量添加到SQL字符串之前,調用'mysql_real_escape_string()'! – Sven

+0

非常真實。解決了你的顧慮@Sven。 – tchow002