简体   繁体   English

PHP更新什么都没有发生

[英]PHP update nothing happens

So this the code in the edit.php document 所以这是edit.php文件中的代码

<?php
    session_start();

    include_once("db.php");

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

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

    if(!isset($_GET['pid'])){
        header("Location: index.php");  
    } else {
        echo "This seems to work";
        $pid = $_GET['pid'];
        echo $pid;
    }
        if(isset($_POST['edit'])){
            $title = strip_tags($_POST['title']);
            $content = strip_tags($_POST['content']);

            $title = mysqli_real_escape_string($db, $title);
            $content = mysqli_real_escape_string($db, $content);

            $sql = "UPDATE users SET title='$title', content='$content' WHERE id='$pid'";

            if($title == "" || $content == ""){
            echo "Please complete your post!";
            return;

        }
        mysqli_query($db, $sql);
        header("Location: index.php");
    }
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Blog - Post</title>
</head>
<body>
    <form action="edit.php" method="edit" enctype="mutlipart/form-data">
        <input placeholder="Title" name="title" type="text" autofocus size="48"><br  /><br />
        <textarea placeholder="Content" name="content" rows="20" cols="50"></textarea><br />
        <input name="edit" type="submit" value="Post">
    </form>
</body>
</html>

This is the code in db.php 这是db.php中的代码

<?php
    $db = mysqli_connect("localhost", "user", "password", "dbname")
?>

I know I am doing something very wrong with the update thing because it doesn't want to update. 我知道我在做更新事情很不对劲,因为它不想更新。 But I don't know how to fix it. 但是我不知道如何解决。 Help please? 请帮助?

method="post" //change to post. Default is get

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="mutlipart/form-data">

we use <?php echo $_SERVER['PHP_SELF'] ?> , because everything is happening on the same page. 我们使用<?php echo $_SERVER['PHP_SELF'] ?> ,因为所有事情都在同一页面上发生。

Using prepared statements, 使用准备好的语句,

$sql = "UPDATE users SET title=?, content=? WHERE id=?";
$statement = $db->prepare($sql);
$statement->bind_param('ssi', $title, $content, $pid);   
if($statement->execute()){
       //all good
} else {
   echo $db->error;    
}

Like others have said use PDO. 就像其他人所说的使用PDO。

If you want to keep using the code you've already got. 如果您想继续使用已有的代码。 Change the form tags to: 将表单标签更改为:

  <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="mutlipart/form-data">

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

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