简体   繁体   中英

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 .

Create classes.php file and add this code inside it. Change your host, dbname, username and password if needed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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