使用OOP和switch case在pdo中更新PHP
我试着在表中按id按开关大小写顺序进行更新,如下所示 index.php使用OOP和switch case在pdo中更新PHP,php,mysql,pdo,Php,Mysql,Pdo,我试着在表中按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";
<?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;
}
?>
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>";
}
}
user.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;
}
?>
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>";
}
}
require(DIR_MODELS.'database.php');
$database=new MySQLDatabase();
类用户{
受保护的静态$table_data=“data”;
公帑$uid;
公有产权;
公共内容;
公共$link;
公共职能编辑新闻(){
全球$数据库;
$newsid=$this->uid;
$utitle=$this->title;
$ucontent=$this->content;
$ulink=$this->link;
$sql=$database->connect->prepare(“UPDATE.self::$table_data.”设置title=:title,text=:text,links=:links,其中id=:id”);
$sql->bindParam(':title',$utitle);
$sql->bindParam(':links',$ucontent);
$sql->bindParam(':text',$ulink);
$sql->bindParam(':id',$newsid);
$sql->execute();
}
公共静态函数showTable(){
全球$数据库;
$sql=“SELECT*FROM.self::$table_data.”;
$stmt=$database->connect->prepare($sql);
$stmt->execute();
$result=$stmt->fetchAll();
回声“;
foreach($结果为$行){
回声“;
回声“;
回声“;
回声“;
回声“;
}
回声“;
回声“;
}
}
editnews()
方法中的问题是它不起作用。ShowTable()
工作得很好,create方法也很好,只是在更新中它对我不起作用。我希望我没有犯太多错误。在SQL查询中:
文本
是一个,请使用反勾号将其转义
在mysql服务器中,它用于定义一种用于保存大量文本的数据类型。执行查询后尝试查找错误:检查对数据库的调用结果。打印
$sql
的内容,并尝试直接在数据库中运行查询,以再次检查查询是否正确。您根本没有任何错误检查。为什么您认为所有的数据库功能都能完美工作?捕捉错误,您就会知道发生了什么。在执行任何更新或插入操作时,您需要对数据库进行提交。我在你的代码中没有看到任何提交。检查,如果他只执行一条语句,我认为他不需要提交。