duanmao2774 2008-09-25 21:31
浏览 49

PHP / MySQL更新数据库问题中的新闻记录

I have this PHP code that I am trying to use to let a user edit a news record in a form and then when they hit the submit button, it will update the record in a database. The problem is that everything works but the record is not actually updated in the database.

Could someone look at my code and see where a problem could occur?

<?php
    $title = "Edit News";
    include("../includes/header.php");
    include("../includes/database.php");
    $done = false;
    $expected = array('newstitle', 'newscontent', 'id');
    if ($_GET && !$_POST) { 
        if (isset($_GET['id']) && is_numeric($_GET['id'])) { 
            $id = $_GET['id']; 
        } 
        else { 
            $id = NULL; 
        } 
        if ($id) { 
            $sql = "SELECT * FROM news WHERE id = $id"; 
            $result = mysql_query($sql) or die ("Error connecting to database..."); 
            $row = mysql_fetch_assoc($result);
        } 
        // if form has been submitted, update record 
        if (array_key_exists('update', $_POST)) { 
            // prepare expected items for insertion into database 
            foreach ($_POST as $key => $value) { 
                if (in_array($key, $expected)) { 
                    ${$key} = mysql_real_escape_string($value); 
                } 
            } 
            // abandon the process if primary key invalid 
            if (!is_numeric($id)) { 
                die('Invalid request'); 
            }
            // prepare the SQL query 
            $query = "UPDATE news SET title = '$title', content = '$content' WHERE id = $id"; 
            // submit the query 
            $done = mysql_query($query) or die("Error connecting to database..."); 
        }
    }
    // redirect page if $id is invalid 
    if ($done) { 
        header("Location: $ROOT/admin/listnews.php"); 
        exit; 
    }
?>
  • 写回答

6条回答 默认 最新

  • dtz88967 2008-09-25 21:35
    关注

    If you run that UPDATE from the mysql cli with the same data the user sends does it update?

    If not check for escaping characters.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题