繁体   English   中英

SQL 更新失败 - 具有“只读”属性的输入

[英]SQL update failing - inputs with “read-only” attr

对不起。 简单的复制/粘贴错误。 没有编程问题。

我有一个指向PHP file的表格,该文件正在执行SQL UPDATE 由于我的表单有一些read only输入字段 - (不可更改)。

我的问题是表单有一些字段 - POST 和 PHP 并不期望这些,基本上它们从未被使用过。

当我提交所有字段时,我的 SQL 查询失败(没有进行更新)。 为什么?

如何告诉表单不要 POST 某些字段。 只有读/写字段(那些没有 readonly="readonly")?

如何告诉 PHP 不包括从表单收到的所有值?

我是否应该在更新只读字段中包含(更新不会发生,因为这些字段没有更改。)?

任何建议都非常感谢。

请注意:

  • 当没有使用“只读”时,一切正常

更新

我的表格 PHP(HTML 结果较低):

<form action="update-news.php?updateID='.$id.'" class="form note-form" style="display: block;" method="post">
    <label>ID</label>
    <input name="id" type="text" value="'.$id.'" readonly="readonly" />
    <br class="clr">

    <label>Create by</label>
    <input name="id" type="text" value="'.$usr.'" readonly="readonly" />
    <br class="clr">

    <label>Updated by</label>
    <input name="id" type="text" value="'.$never_update_user.'" readonly="readonly" />
    <br class="clr">

    <label>Created</label>
    <input name="id" type="text" value="'.$created.'" readonly="readonly" />
    <br class="clr">

    <label>Last Update</label>
    <input name="id" type="text" value="'.$never_update.'" readonly="readonly" />
    <br class="clr">
    <br /><br />

    <label>Live</label>
    <input name="live" type="checkbox" ',($live ? 'checked="checked"':''),'/>
    <br class="clr">

    <label>Title</label>
    <input name="title" type="text" value="'.$title.'" />
    <br class="clr">

    <label>Content</label>
    <textarea id="content" name="content" type="text" rows="5" cols="75">'.$content.'</textarea>
    <br class="clr">

    <input type="submit" class="button" value="Update" id="submit" />
</form>

表格 HTML:

<form method="post" style="display: block;" class="form note-form" action="update-news.php?updateID=6">
                            <label>ID</label>
                            <input type="text" readonly="readonly" value="6" name="id">
                            <br class="clr">

                            <label>Create by</label>
                            <input type="text" readonly="readonly" value="user@gmail.com" name="id">
                            <br class="clr">

                            <label>Updated by</label>
                            <input type="text" readonly="readonly" value="user@gmail.com" name="id">
                            <br class="clr">

                            <label>Created</label>
                            <input type="text" readonly="readonly" value="August 15, 2011, 2:24 pm" name="id">
                            <br class="clr">

                            <label>Last Update</label>
                            <input type="text" readonly="readonly" value="August 15, 2011, 2:25 pm" name="id">
                            <br class="clr">
                            <br><br>

                            <label>Live</label>
                            <input type="checkbox" checked="checked" name="live">
                            <br class="clr">

                            <label>Title</label>
                            <input type="text" value="How to compare two dates in php and echo newer one?" name="title">
                            <br class="clr">

                            <label>Content</label>
                            <textarea cols="75" rows="5" type="text" name="content" id="content">123</textarea>



                            <input type="submit" id="submit" value="Update" class="button">
                    </form>

UPDATE-NEWS.php

<?php

session_name('users');
session_set_cookie_params(2*7*24*60*60);
session_start();

define('INCLUDE_CHECK',true);

require 'connect.php';
require 'functions.php';

if(!$_SESSION['id']) {
    header ("Location: index.php"); 
}


        //Function to sanitize values received from the form. Prevents SQL injection
        function clean($str) {
            $str = @trim($str);
            if(get_magic_quotes_gpc()) {
                $str = stripslashes($str);
            }
            return mysql_real_escape_string($str);
        }

        //Sanitize the POST values
        $id = clean($_POST['id']);
        $usr2 = $_SESSION['usr'];
        $live = (isset($_POST['live']))?1:0;
        $updated = date("F j, Y, g:i a",time()+60*60);
        $title= clean($_POST['title']);
        $content = clean($_POST['content']);

        //Create INSERT query
        $qry = "UPDATE news SET usr2 = '$usr2', live = '$live', updated = '$updated', title = '$title', content = '$content' WHERE id='".mysql_real_escape_string($_POST['id']). "'  ";
        $result = mysql_query($qry);
        echo mysql_error();

        //Check whether the query was successful or not
        if($result) {
            header("location: notes.php");
            exit();
        }else {
            die("Query failed");

        }
    ?>

当然,一个问题是您有几个带有name="id"的字段。 只有最后一个将作为$_POST['id']发布到服务器,这很可能是一个问题。

是的,您可以简单地设置您不想提交的字段的disabled="disabled"属性。

暂无
暂无

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

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