2012-05-29 83 views
0

我有一個非常惱人的問題。每當我編輯帖子,編輯第一個時,它都會丟失所有信息。我無法弄清楚,我已經爲此工作了2天。PHP編輯帖子

赫雷什形式的代碼:

<?php 
$post = htmlspecialchars($_GET["id"]); 


$name = $_SESSION['Username']; 
if (in_array($name, $allowedposters)) { 
$results = mysql_query("SELECT * FROM tool WHERE id = $post"); 
    while($row = mysql_fetch_array($results)){ 


$title= $row['title']; 
$details= $row['details']; 
$date= $row['date']; 
$author= $row['author']; 
$id= $row['id']; 



echo "<a href=story.php?id="; 
echo $post; 
echo ">Cancel edit</a> <br><br><b>"; 
echo $title; 
echo "</b> <br><br>"; 
echo ' 
<form action="edit-new.php?story='; 
echo $id; 
echo '" method="post" enctype="multipart/form-data"> 
<textarea rows="1" cols="60" name="title" wrap="physical" maxlength="100">'; 
echo $title; 
echo '</textarea><br>'; 
?> 



<textarea rows="30" cols="60" name="details" wrap="physical" maxlength="10000"> 
<?php 
echo $details; 
echo '</textarea><br>'; 

echo '<label for="file">Upload featured image:</label><br> 
     <input type="file" name="file" id="file" />'; 
echo'<br><input type="submit" />'; 
} 

} else { 
    echo "Not enough permissions."; 
} 
?> 

。 。

下面是實際的php代碼,將信息插入數據庫:

。 。

<?php 
    $post = $_GET['story']; 
    $title = $_POST['title']; 
    $details = $_POST['details']; 
     echo 'Updated.'; 
    $dbtype  = "mysql"; 
    $dbhost  = "localhost"; 
    $dbname  = "x"; 
    $dbuser  = "xx"; 
    $dbpass  = "xxx"; 
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); 
    $sql = "UPDATE tool SET title=:title, details=:details WHERE id = '$post'"; 
     $q = $conn->prepare($sql); 
     $q->execute(array(
     ':details'=>$details, 
     ':title'=>$title, 
     )); 
?> 

同樣,我想提一提,唯一的問題是,在第一一次我編輯的職位,它就會失去它的信息。那之後它絕對不會發生在那個特定的職位上。

之後,編輯工作完美無瑕。

+0

我已經重新寫了實際的方法,我插入信息數據庫2次 –

+0

你有更多的1錯誤,你的整個代碼都是錯誤的。啓用'error_reporting(E_ALL);' –

+0

@LawrenceCherone當我這樣做時我沒有收到任何錯誤 –

回答

0

這並不直接回答你的問題 - 知道你實際獲得什麼$_GET['id'],你的數據庫上運行了哪些實際的SQL以及你的表單的生成內容是什麼樣子會很有用。

但是:

$post = htmlspecialchars($_GET["id"]); 

是錯誤的。 htmlspecialchars輸出的數據轉義給客戶端。爲SQL編碼特殊字符不會保護您免受SQL注入攻擊。

由於$post有望成爲一個整數(大概),這就足夠了:

$post = (int)$_GET['id']; 

但一定要檢查返回一個有效的整數,並引發相應的錯誤,如果沒有。

不要忘記在edit-new.php中再次這樣做 - 更好的是,使用參數化值,就像您爲titledetails所做的那樣。

最後,你應該使用htmlspecialchars逃脫形式$details$title。否則像</textarea>內容將不會正確顯示,你很容易受到XSS瑕疵(例如後面<script>alert("I'm going to do bad things now");</script>

+0

$ _GET ['id']獲取帖子ID 。例如:edit.php?id = 137 –

+0

好的,那是一個回答...... – Hamish