这是我的代码
$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>";
}
它应该更新表行,但它不会。输入值正在发送正常。请给我一个解决方案..
哪部分代码错了?????
答案 0 :(得分:0)
我不知道这会解决您的问题(是的,我没有时间对此进行测试),但我很高兴能让您的代码更具可读性。
将来,如果你 1会更容易回答。使您的代码可读和 2。给你的mysql数据库转储。
创建classes.php
文件并在其中添加此代码。如果需要,请更改主机,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());
}
}
然后将这些内容放入文件更新内容:
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>";
}