我尝试使用id这样的交换机案例顺序更新表格
的index.php
<?php $action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "index";
$user = new User();
switch ($action) {
case "editenewsaction":
$main_content = "/main_content/Dashboard/edit_new_news";
require(DIR_VIEWS . 'layouts' . DS . "default.php");
if (isset($_POST)) {
$user_id = $_GET['news_id'];
$user->uid = $user_id;
$title = trim($_POST['title']);
$content = trim($_POST['content']);
$link = $_POST['link'];
$user->title = $title;
$user->content = $content;
$user->link = $link;
$user->editnews();
$main_content = "/main_content/Dashboard/mainIndex";
require(DIR_VIEWS . 'layouts' . DS . "default.php");
} else {
$main_content = "main_content/errors/edit_error_msg";
require(DIR_VIEWS . 'layouts' . DS . "default.php");
}
break;
}
?>
user.php的
require(DIR_MODELS . 'database.php');
$database = new MySQLDatabase();
class User {
protected static $table_data = "data";
public $uid;
public $title;
public $content;
public $link;
public function editnews() {
global $database;
$newsid = $this->uid;
$utitle = $this->title;
$ucontent = $this->content;
$ulink = $this->link;
$sql = $database->connect->prepare("UPDATE " . self::$table_data . " SET title = :title, text = :text,links=:links WHERE id = :id");
$sql->bindParam(':title', $utitle);
$sql->bindParam(':links', $ucontent);
$sql->bindParam(':text', $ulink);
$sql->bindParam(':id', $newsid);
$sql->execute();
}
public static function showTable() {
global $database;
$sql = "SELECT * FROM " . self::$table_data . " ";
$stmt = $database->connect->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
echo"<table border=1>";
foreach ($result as $row) {
echo "<tr>";
echo "<td><a href=index.php?action=newsview&news_id=" . $row['id'] . ">" . $row['title'] . " </a></td>";
echo "<td><a href=index.php?action=newsview&news_id=" . $row['id'] . "><img src='{$row['links']}' /> </a></td>";
echo "<td><a href=index.php?action=editenewsaction&news_id=" . $row['id'] . "><pre>edit</pre></a></td>";
echo "<td><a href=index.php?action=newsview&news_id=" . $row['id'] . "><pre>delete</pre></a></td>";
}
echo "</tr>";
echo "</table>";
}
}
editnews()
方法中的问题是它不起作用。 ShowTable()
运行良好,并且create方法在更新中也运行良好,但对我来说不起作用。我希望我没有犯太多错误。
答案 0 :(得分:1)
在您的SQL查询中:
UPDATE ... SET title = :title, text = :text,links=:links WHERE id = :id
TEXT
是mysql reserved keyword,使用后退标记来逃避它。
UPDATE ... SET title = :title, `text` = :text,links=:links WHERE id = :id
在mysql服务器中,它用于定义用于保存大量文本的数据类型。