Meilleure
Meilleure

Reputation: 3

Duplicating instead of updating

Hello guys
I have a db with guides that in admin mode can be edited. I have just remade the input area and all is good except when logged in as admin i cant update guides, it simply creates a new guide instead of simply updating. Please be gentle with me as i am a beginner in the coding world, + i would love some fresh eyes on this :) thank you very much

my dashboard code

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

Upvotes: 0

Views: 53

Answers (1)

Rok D.
Rok D.

Reputation: 244

There is no input with name id so $_POST['id'] doesn't exist and that's why there's an insert instead of update.

And some extra hints

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

You probably want isset($_SESSION['isLogin']) here since when the variable is not set it can never be YES

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

You can drop isset here and only use empty.

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

Only the last row is stored in those variables, since you are overwriting them.

Upvotes: 2

Related Questions