Php 复制而不是更新

Php 复制而不是更新,php,html,mysql,Php,Html,Mysql,大家好 我有一个数据库与指南,在管理模式下可以编辑。我刚刚重新制作了输入区域,一切都很好,除了当以管理员身份登录时我无法更新指南,它只是创建一个新的指南,而不是简单地更新。 请对我温柔一点,因为我是编码领域的初学者,+我希望有一些新的视角:)非常感谢 我的仪表盘代码 <?php include("header.php"); ?> <?php if(!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "

大家好
我有一个数据库与指南,在管理模式下可以编辑。我刚刚重新制作了输入区域,一切都很好,除了当以管理员身份登录时我无法更新指南,它只是创建一个新的指南,而不是简单地更新。 请对我温柔一点,因为我是编码领域的初学者,+我希望有一些新的视角:)非常感谢

我的仪表盘代码

<?php include("header.php"); ?>

<?php
if(!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"){
    die("<script> window.location = 'login.php' </script>");
}

$error=false;
$success=false;

if(isset($_GET) && !empty($_GET)) {
  $id = base64_decode($_GET['id']);
  $user_id = $_SESSION['userInfo']['id'];

  $selectSql = "SELECT * FROM guides WHERE 1 = 1 AND user_id = " . $user_id . " AND id = " . $id;
  $result = $conn->query($selectSql);

  $id = 0;
  $title = $step2 = $step3 = $step4 = $step5 = $step6 = $step7 = $step8 = $step9 = '';

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      $id = $row['id'];
      $title = $row['title'];
      $step2 = $row['step2'];
      $step3 = $row['step3'];
      $step4 = $row['step4'];
      $step5 = $row['step5'];
      $step6 = $row['step6'];
      $step7 = $row['step7'];
      $step8 = $row['step8'];
      $step9 = $row['step9'];
    }
  }
}

if(isset($_POST) && !empty($_POST)){
  $user_id = $_SESSION['userInfo']['id'];

  if($_POST['id']){
    $sqlInsert = 'UPDATE guides SET title = "'.htmlentities($_POST["title"]).'", step2 = "'.htmlentities($_POST["step2"]).'", step3 = "'.htmlentities($_POST["step3"]).'", step4 = "'.htmlentities($_POST["step4"]).'", step5 = "'.htmlentities($_POST["step5"]).'", step6 = "'.htmlentities($_POST["step6"]).'", step7 = "'.htmlentities($_POST["step7"]).'", step8 = "'.htmlentities($_POST["step8"]).'", step9 = "'.htmlentities($_POST["step9"]).'" WHERE id = ' . $_POST['id'] . ' AND user_id = ' . $_SESSION['userInfo']['id'];
  }else{
    $sqlInsert = 'INSERT INTO guides(user_id, title, step2, step3, step4, step5, step6, step7, step8, step9)VALUES ("' .$user_id. '", "'.htmlentities($_POST["title"]).'", "'.htmlentities($_POST["step2"]).'", "'.htmlentities($_POST["step3"]).'", "'.htmlentities($_POST["step4"]).'", "'.htmlentities($_POST["step5"]).'", "'.htmlentities($_POST["step6"]).'", "'.htmlentities($_POST["step7"]).'", "'.htmlentities($_POST["step8"]).'", "'.htmlentities($_POST["step9"]).'")';
  }

  if ($conn->query($sqlInsert) === TRUE) {
    if($_POST['id']){
      $success = "Your guide has been updated successfully!";
    }else{
      $success = "Your guide has been added successfully!";
    }
    $_SESSION['success'] = $success;
    header("Location: dashboard.php");
  }else{
    $error[] =  "Error Message: ".$conn->error;
  }
}

?>

<!DOCTYPE HTML>
<html>
<head>
<title>Guideory - share your knowledge</title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum- 
scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="keywords" content="Guideory - share your knowledge" />
<!--web-fonts-->
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">

<link rel="stylesheet" href="css/style.css">

<!--web-fonts-->
</head>
<body>
<div class="header">

    </div>
    <!---header--->
    <!---main--->
        <div class="main">
            <div class="main-section">
                <div class="login-form">
                    <h2>Share a piece of your knowledge</h2>
                    <br>
                    <h4>You can create up to 8 steps, not including the title. 
Atleast one step is required. When writing your guide, remember that other 
people have to be able to read it, so be as specific as possible.</h4>
                      <form role="form" method="post">
                         <div id="step-1">
                    <ul>
                             <li class="text-info" id="title">Title:</li>
                             <li><input type="text" value="<?php echo $title; 
?>" name="title" id="title" placeholder="Enter the title for your guide here" 
required></li>
                             <div class="clear"></div>
                         </ul>


                         <ul>
                            <li class="text-info" id="step2">Step 1:</li>
                            <li><textarea name="step2" id="step2" 
 placeholder="Enter the description for step 1 here" required><?php echo 
$step2; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                        <br>

                         <ul>
                            <li class="text-info">Step 2:</li>
                            <li><textarea name="step3" placeholder="Enter the 
description for step 2 here"><?php echo $step3; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                         <ul>
                            <li class="text-info">Step 3:</li>
                            <li><textarea name="step4" placeholder="Enter the 
 description for step 3 here"><?php echo $step4; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                         <ul>
                            <li class="text-info">Step 4:</li>
                            <li><textarea name="step5" placeholder="Enter the 
description for step 4 here"><?php echo $step5; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                       <ul>
                            <li class="text-info">Step 5:</li>
                            <li><textarea name="step6" placeholder="Enter the 
description for step 5 here"><?php echo $step6; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                       <ul>
                            <li class="text-info">Step 6:</li>
                            <li><textarea name="step7" placeholder="Enter the 
description for step 6 here"><?php echo $step7; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                       <ul>
                            <li class="text-info">Step 7:</li>
                            <li><textarea name="step8" placeholder="Enter the 
description for step 7 here"><?php echo $step8; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                       <ul>
                            <li class="text-info">Step 8:</li>
                            <li><textarea name="step9" placeholder="Enter the 
 description for step 8 here"><?php echo $step9; ?></textarea></li>
                            <div class="clear"></div>
                        </ul>
                        <input type="submit" value="Create guide">
                    </form>
                </div>

            </div>
        </div>



</body>
</html>


没有名为
id
的输入,因此
$\u POST['id']
不存在,这就是为什么存在插入而不是更新

还有一些额外的提示

!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"
您可能希望在此处设置
isset($\u SESSION['isLogin'])
,因为当变量未设置时,它永远不会是
YES

isset($_GET) && !empty($_GET)
您可以将
isset
放在此处,并且只能使用
empty

while ($row = $result->fetch_assoc()) {

只有最后一行存储在这些变量中,因为您正在覆盖它们。

没有名为
id
的输入,因此
$\u POST['id']
不存在,这就是为什么要插入而不是更新

还有一些额外的提示

!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"
您可能希望在此处设置
isset($\u SESSION['isLogin'])
,因为当变量未设置时,它永远不会是
YES

isset($_GET) && !empty($_GET)
您可以将
isset
放在此处,并且只能使用
empty

while ($row = $result->fetch_assoc()) {

只有最后一行存储在这些变量中,因为您正在覆盖它们。

警告:当使用
mysqli
时,您应该使用and将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将
$\u POST
$\u GET
或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害
htmlentities
不是SQL的转义方法。提示:不要通过
$stepN
创建名为
$step1
的变量,而是创建一个
数组。如果您刚刚开始使用PHP并想构建应用程序,我也强烈建议你看看各种各样的,看看你是否能找到一个适合你的风格和需要。它们有多种口味,从轻量级到更全面的。这些示例为您提供了具体的工作示例,并对如何编写代码和组织文件提供了更有力的指导。@tadman非常感谢您提供的信息警告:在使用
mysqli
时,您应该使用并将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将
$\u POST
$\u GET
或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害
htmlentities
不是SQL的转义方法。提示:不要通过
$stepN
创建名为
$step1
的变量,而是创建一个
数组。如果您刚刚开始使用PHP并想构建应用程序,我也强烈建议你看看各种各样的,看看你是否能找到一个适合你的风格和需要。它们有多种口味,从轻量级到更全面的。这些都为您提供了具体的工作示例,并对如何编写代码和组织文件提供了更有力的指导。@tadman非常感谢您提供的信息谢谢@Rok:)您认为这样行吗<代码>谢谢@Rok:)你觉得这样行吗<代码>