复制而不是更新

时间:2018-06-12 17:52:21

标签: php html mysql

大家好
我有一个带有指南的数据库,可以在管理模式下编辑。我刚刚重新创建了输入区域并且一切都很好,除非以管理员身份登录我无法更新指南,它只是创建一个新指南而不是简单地更新。 请对我温柔,因为我是编码世界的初学者,+我会喜欢这个新鲜的眼睛:)非常感谢你

我的信息中心代码

<?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>

1 个答案:

答案 0 :(得分:2)

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

还有一些额外的提示

!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"

你可能想要isset($_SESSION['isLogin']),因为当没有设置变量时,它永远不会是YES

isset($_GET) && !empty($_GET)

您可以在此处删除isset,仅使用empty

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

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