簡體   English   中英

通過php mysql更新表

[英]updation of table through php mysql

這是我更新表的代碼。 我的問題是,提交新記錄后,我第一次無法更新(顯示為空白),但是第二次運行良好。

還有一件事:當我刪除include語句時,它在submessage.php上運行良好,沒有任何phpcode。 [annakata:我不知道這意味着什么]

$pid = $_GET['id'];
$title = $_POST['title'];
$summary = $_POST['summary'];
$content = $_POST['content'];
$catid = $_POST['cid'];
$author = $_POST['author'];
$keyword = $_POST['keyword'];
$result1= mysql_query("update listing set catid='$catid',title='$title',
summary='$summary',content='$content', author='$author', keyword='$keyword' where pid='$pid'",$db);
    include("submessage.php");

那段代碼的錯誤之處很難列舉。 但是,至少,在查詢數據庫之前,應該建立與數據庫的連接

為什么不直接重定向到submessage.php而不是對其進行內聯? 重定向還可以防止用戶刷新頁面時重復的數據庫操作。 只需將include語句替換為:

header('Location: submessage.php?id=' . $pid);
die();

另外,在部署應用程序之前: 切勿直接在SQL QUERY中直接輸入用戶 您應該改用綁定參數。 否則,您也可以公開發布數據庫管理員密碼。 http://ie.php.net/pdo上了解有關PDO和准備好的語句的更多信息。

這是我的處理方式:

$pdo = new PDO(....); // some configuration parameters needed
$sql = "
    UPDATE listing SET
        catid=:catid, title=:title, summary=:summary,
        content=:content, author=:author, keyword=:keyword
    WHERE pid=:pid
";
$stmt = $pdo->prepare($sql);
$stmt->bindValue('catid', $_POST['catid']);
$stmt->bindValue('title', $_POST['title']);
$stmt->bindValue('summary', $_POST['summary']);
$stmt->bindValue('content', $_POST['content']);
$stmt->bindValue('author', $_POST['author']);
$stmt->bindValue('keyword', $_POST['keyword']);
$stmt->bindValue('pid', $pid = $_GET['id']);
$stmt->execute();

header('Location: submessage.php?id=' . $pid);
die();

或者實際上,我將使用一些ORM解決方案使它看起來更像這樣:

$listing = Listing::getById($pid = $_GET['id']);
$listing->populate($_POST);
$listing->save();

header('Location: submessage.php?id=' . $pid);
die();

除了通常的SQL注入警告-很可能給出您的代碼以及從何處獲取查詢參數(沒有任何類型的驗證),然后您的問題很可能與查詢無關,尤其是在工作正常的情況下在后續嘗試中。 您確定第一次調用腳本時設置了$ _GET ['id']嗎?

只是要注意,絕對沒有必要對您需要更新的每個字段執行多個更新查詢-只需將它們組合為一個查詢即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM