gpt4 book ai didi

php - 未在数据库中更新

转载 作者:行者123 更新时间:2023-11-30 01:33:56 25 4
gpt4 key购买 nike

这是我的代码

$id = $_POST['id'];
$category = $_POST['category'];
$title = $_POST['title'];
$short_content = $_POST['short_content'];
$long_content = $_POST['long_content'];
$date = $_POST['date'];
$lang = $_POST['language'];
//echo $id." ".$category." ".$title." ".$short_content." ".$lang." ".$date;

if(empty($id)){
echo "<h3 align=\"center\">Please fill ID</h3>";
}

if(empty($category)){
echo "<h3 align=\"center\">Please fill Category</h3>";
}

if(empty($title)){
echo "<h3 align=\"center\">Please fill Title</h3>";
}

if(empty($date)){
echo "<h3 align=\"center\">Please fill Date</h3>";
}

if(empty($lang)){
echo "<h3 align=\"center\">Please fill Lang</h3>";
}

if(!empty($_FILES['img']['name'])){
$extension = end(explode(".",$_FILES['img']['name']));
//echo "file format: ".$extension."<br>";
$name = $_FILES['img']['name'];
$size = $_FILES['img']['size'];

if(file_exists("views/admin/uploads/".$name)){
echo "<h3 align=\"center\">".$_FILES['img']['name']." exists</h3>
<a href=".$_SERVER['HTTP_REFERER']."><h3 align=\"center\">Go back</h3></a>";
return false;
}

if($extension != "jpg" && $extension != "png" && $extension != "gif" && $extension != "JPG"){
echo "<h3 align=\"center\">File with format: ".$extension." is not aviable to upload</h3>
<a href=".$_SERVER['HTTP_REFERER']."><h3 align=\"center\">Go back</h3></a>";
return false;
}
}

if(!empty($id) && !empty($category) && !empty($title) && !empty($date) && !empty($lang)){
$query = mysql_query("UPDATE `news` SET `id`='$id', category`='$category',`title`='$title',`img`='$name',`short_content`='$short_content',`content`='$long_content',`date`='$date',`lang`='$lang' WHERE `id`='$id'");
move_uploaded_file($_FILES['img']['tmp_name'],"views/admin/uploads/".$name);
echo "<h2 align=\"center\">Successfully updated!</h2>";
}

它应该更新表行,但它没有。输入值发送正常。请给我一个解决方案..

哪部分代码是错误的????

最佳答案

我不知道这会解决你的问题(是的,我没有时间测试这个),但很高兴我让你的代码更具可读性。

将来,如果您1,回答会容易得多。使您的代码可读2。提供你的 mysql 数据库转储

创建classes.php 文件并在其中添加此代码。如果需要,更改您的主机、数据库名、用户名和密码。

// Connecting to database
class mysql{
public $db;
public function connect(){
$this->db = new PDO(
"mysql:host=localhost;dbname=xxxxx;",
"root",
""
);
}
}

// Update thing
class stuff extends mysql{
public function updateThing($id,$cat,$title,$img,$shortContent,$content,$date,$lang){
$this->statement = $this->db->prepare("UPDATE `news` SET `category`= $2,`title` = $3,`img` = $4,`short_content` = $5,`content` = $6,`date` = $7,`lang` = $8 WHERE `id` = $1");
$this->statement->execute(array($id,$cat,$title,$img,$shortContent,$content,date("Y-m-d H:i:s",strtotime($date)),$lang));
print_r($this->statement->fetchAll());
}
}

然后将它们放入更新内容的文件中:

include_once("classes.php");

$id = $_POST['id'];
$cat = $_POST['category'];
$title = $_POST['title'];
$shortContent = $_POST['short_content'];
$longContent = $_POST['long_content'];
$date = $_POST['date'];
$lang = $_POST['language'];

$stuff = new stuff;
$stuff->connect();

$errors = array();

if(empty($id)){
$errors[] = "Please fill ID";
}

if(empty($cat)){
$errors[] = "Please fill Category";
}

if(empty($title)){
$errors[] = "Please fill Title";
}

if(empty($date)){
$errors[] = "Please fill Date";
}

if(empty($lang)){
$errors[] = "Please fill Lang";
}

if(!empty($_FILES['img']['name'])){
$extension = end(explode(".",$_FILES['img']['name']));

$name = $_FILES['img']['name'];
$size = $_FILES['img']['size'];

if(file_exists("views/admin/uploads/".$name)){
$errors[] = "File with this name already exists!";
}

if($extension != "jpg" && $extension != "png" && $extension != "gif" && $extension != "JPG"){
$errors[] = "Unknown file format!";
}
}

if(count($errors)==0){
$stuff = new stuff;
$stuff->connect();
$stuff->updateThing($id,$cat,$title,$img,$shortContent,$longContent,$date,$lang);

move_uploaded_file($_FILES['img']['tmp_name'],"views/admin/uploads/".$name);

echo "<h2>Successfully updated!</h2>";
}else{
print "<h3>Errors!</h3><ul><li>".join("</li><li>",$errors)."</li></ul>";
}

关于php - 未在数据库中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17143708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com