Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php更新数据库_Php_Html_Database - Fatal编程技术网

使用php更新数据库

使用php更新数据库,php,html,database,Php,Html,Database,我让它工作的唯一方法是使用空的。然而,这不是我想要的。如果必须的话,我希望能留下一些空的东西。有人知道我应该如何更改代码才能工作吗 编辑页面: <form name="homePage" action="update.php" method="POST"> <Strong>Change home title:</Strong> <p> <input style="width: 300px;" type="text" name="hom

我让它工作的唯一方法是使用
空的
。然而,这不是我想要的。如果必须的话,我希望能留下一些空的东西。有人知道我应该如何更改代码才能工作吗

编辑页面:

<form name="homePage" action="update.php" method="POST">
<Strong>Change home title:</Strong>
<p>
    <input style="width: 300px;" type="text" name="homeTitleChange" value="<?php echo $homeTitle ?>">
    <input type="hidden" name="rowHomeID" value="<?php echo $getHomeID?>">
</p>

<Strong>Change home subtitle:</Strong>
<p>
    <input style="width: 600px;" type="text" name="homeSubtitleChange" value="<?php echo $homeSubtitle ?>">
    <input type="hidden" name="rowHomeID" value="<?php echo $getHomeID?>">
</p>
<input type="submit" class="btn btn-skin" name="homepage" value="save" />
</form>

更改主页标题:


如果不指定您的错误,我们只能假设您的问题。只有您可以调试您的程序,因此,请在脚本顶部执行以下代码行,并告诉我们您的错误,以备将来注意

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
继续,脚本包含一个条件,用于检查
$\u POST
is
中索引处的值!empty()
但不环绕查询。这意味着,无论值是否为空或已设置,都将执行查询

假设您只希望在设置了值时运行查询,可以使用
if
表达式将其包装起来:

 // an array of all the index's
$index = ['homeSubtitleChange', 'homeTitleChange', 'rowHomeID'];

 // loop through each index and check they're not empty
foreach($index as $_index)
{
    if( empty( $_POST[$_index] ) && !isset( $_POST['homepage'] ) )
    {
         // if empty - halt the program with an error
        die("Expected POST index: $_index or homepage.");
    }
}
unset($_index); //cleanup

 // if it got here - all index's have values
 // as Martin said in the comments, I assume you can wrap mysqli_real_escape_string()
 // and intval() ensuring the value is type (int) to protect
 // your database against SQL attacks
$subtitle = mysqli_real_escape_string($conn, $_POST[$index[0]]);
$title    = mysqli_real_escape_string($conn, $_POST[$index[1]]);
$row_id   = intval($_POST[$index[2]]);

 // consider upgrading to a PDO driver and using prepare() statements
 // this SQL statement is prone to SQL injections

$sql      = "UPDATE Home SET title = '$title', subtitle = '$subtitle' WHERE homeID = '$row_id'";

if( mysqli_query( $conn, $query ) )
{
    die("Success.");
}

die("Failed.");
前体:

  • 您已经包含了两次连接脚本

  • 正如其他人所说,您正在包括隐藏表单字段
    ,请清理您的用户输入;像那样直接将其放入数据库是非常不安全的

    至于你的问题,据我所知,你正在努力确保设置了值,但你也希望能够传递一个空字符串

    如果是这样,我想您需要
    isset

    //...
        if(
            isset($_POST["homeTitleChange"])&&
            isset($_POST["homeSubtitleChange"]) &&
            isset($_POST["rowHomeID"])
        ){
    //...
    

    这将确保您设置了
    POST
    值,如果他们提交了表单,这些值无论如何都应该设置;但是,如果
    $\u POST[“rowHomeID”]
    =
    0
    ,它也将返回
    true
    ,这可能不是您想要的,因此您可能希望返回使用
    !empty
    对于表示它不能是空字符串或等于
    0

    的内容,如果我理解正确,您希望允许空字符串作为输入。 如果是这样,您需要的是
    isset()
    ,而不是
    !empty()

    因此,代码中的这一部分:

    !empty($_POST["homeTitleChange"])&& 
    !empty($_POST["homeSubtitleChange"]) &&  
    !empty($_POST["rowHomeID"])
    
    将其替换为以下内容:

    isset($_POST["homeTitleChange"],$_POST["homeSubtitleChange"],$_POST["rowHomeID"])
    

    你很好,可以走了。

    说要了解你的陈述。即使是这样也不安全!首先,防止你的错误。其次,调试您的值&打开错误报告并告诉我们错误。我假设您的意思是,如果某些字段为空,您的数据库将不接受您的查询。您可以通过在数据库中允许空值来修复这一问题fields@EatPeanutButter
    NULL
    '
    (空字符串)是两种不同的东西。为了更好地避免mysql,请尝试使用PDO。我真的很想+1您的答案,但您甚至没有对修复SQL注入漏洞做任何粗略的操作。您不需要PDO来使用参数化查询。。。或者您甚至可以使用
    mysqli\u real\u escape\u string
    I更新与保护对应的答案。我不使用
    MySQLi.*
    so方法;如果您知道更好的保护方法,请随时更新答案@Martin唯一真正的改进应该是OP实现其他人在评论中所说的准备好的语句。我从来没有遇到过在
    foreach()
    循环之后执行
    unset()
    。我喜欢“清理”的原则@在文档中,一旦循环完成,变量值就会保留下来。
    $homeEditRow = (int)$_POST['rowHomeID']; //force to int.
    if($homeEditRow > 0 ){
      //run MySQL Update query.
    }
    
    $homeTitleUpdate = mysqli_real_escape_string($conn,$_POST["homeTitleChange"]);
    $homeSubtitleUpdate = mysqli_real_escape_string($conn,$_POST["homeSubtitleChange"]); 
    //assuming to be integer required
    $homeEditRow = (int)$_POST["rowHomeID"];
    
     //after your query regardless of outcome:
    var_dump(mysqli_error($conn));
    
    //...
        if(
            isset($_POST["homeTitleChange"])&&
            isset($_POST["homeSubtitleChange"]) &&
            isset($_POST["rowHomeID"])
        ){
    //...
    
    !empty($_POST["homeTitleChange"])&& 
    !empty($_POST["homeSubtitleChange"]) &&  
    !empty($_POST["rowHomeID"])
    
    isset($_POST["homeTitleChange"],$_POST["homeSubtitleChange"],$_POST["rowHomeID"])