0

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?????

4

1 回答 1

0

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

将来,如果您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>";
}
于 2013-06-17T09:30:45.117 回答