简体   繁体   English

没有在数据库中更新

[英]not updating in database

This is my code 这是我的代码

$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>";
}

It's should update table row, but it dont. 它应该更新表行,但不更新。 The input value are sending ok. 输入值发送正常。 Please give me a solution.. 请给我一个解决方案。

Which part of code is wrong????? 哪一部分代码是错误的????

I don't know will this fix your problem (yes, I don't have time to test this), but be happy about that I made your code much more readable. 我不知道这是否可以解决您的问题(是的,我没有时间进行测试),但是对于我使您的代码更具可读性感到高兴。

In the future, it would be much easier answer if you 1. make your code readable and 2. give your mysql database dump . 将来,如果1.使代码可读2.使mysql数据库转储 ,它将是一个更简单的答案。

Create classes.php file and add this code inside it. 创建classes.php文件,并将此代码添加到其中。 Change your host, dbname, username and password if needed. 如果需要,请更改主机,dbname,用户名和密码。

// 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());
    }
}

And then throw these into file what updates things: 然后将这些内容放入文件中,以更新内容:

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>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM