How do I make it?

When I change note, it let me change the name without saying
" you have the same lessons on the lessons list"

$stm = $db->prepare("SELECT * FROM subjects WHERE subject=:sb");

full code

<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
<?php include_once 'class.token.php'; ?>
<?php
@$id = isset($_GET['id']) && is_numeric($_GET['id']) ? (int)$_GET['id'] : 1;
if ($id) {
  $stm = $db->prepare("SELECT * FROM subjects WHERE id=:id");
  $stm->bindParam(":id", $id, PDO::PARAM_STR);
  $stm->execute();
  $row = $stm->fetch(PDO::FETCH_ASSOC);
}
 ?>
                <div class="container-fluid">
                  <div class="card shadow mb-4 text-right">
                    <div class="card-header py-3">
                      <h6 class="m-0 font-weight-bold text-gray-800">add sebject</h6>
                    </div>
                    <div class="card-body">
                      <?php
                          if (isset($_POST['sub2'])) {
                          if(Token::check($_POST['token']) AND !empty($_POST['token'])){
                            $sb=@output($_POST['sb']);
                            $note=@output($_POST['note']);
                            if(!empty($sb)){
                             $stm = $db->prepare("SELECT * FROM subjects WHERE subject=:sb");
                             $stm->bindParam(":sb", $sb, PDO::PARAM_STR);
                             $stm->execute();
                             $rowCount = $stm->rowCount();
                            if($rowCount == 0){
                                $stm = $db->prepare("UPDATE subjects SET subject=:sb,description=:note WHERE id=:id");
                                $stm->bindParam(":sb", $sb, PDO::PARAM_STR);
                                $stm->bindParam(":note", $note, PDO::PARAM_STR);
                                $stm->bindParam(":id", $id, PDO::PARAM_STR);
                                $stm->execute();

                                echo "added";
                                $db = null;
                              direct("./subject_list.php");
                             }else {
                               echo "you have the same lessons on the lessons list";
                           }
                          }else {
                            echo "empty";
                          }
                          }else {
                           echo "try again";

                          }
                        }
                        ?>
                      <div class="container">
                        <form method="post" action="">
                          <input type="hidden" name="dat" min="1" step="1" class="form-control"id="start" placeholder="date">
                        <div class="form-group">
                           <label for="leavetype">lesson code</label>
                          <input id="leavetype" type="text" name="cod" value="<?php echo $row['subject_code']; ?>" class="validate form-control" readonly>
                        </div>
                        <div class="form-group">
                           <label for="leavetype">lesson</label>
                          <input id="leavetype" type="text" name="sb" value="<?php echo $row['subject']; ?>" class="validate form-control">
                        </div>
                        <div class="form-group">
                           <label for="deptshortname">note</label>
                          <textarea id="textarea1" name="note" class="materialize-textarea form-control" length="500" ><?php echo $row['description']; ?></textarea>
                        </div>
                        <input type="hidden" name="token" value="<?php echo Token::create(); ?>">
                        <p align="center"><button type="submit" name="sub2" class="btn btn-primary" id="butsave">edite</button></p>
                      </form>
                      </div>
                  </div>
                </div>
                <!-- /.container-fluid -->
                <script type="text/javascript">
                $('#start').daterangepicker({
                  singleDatePicker: true,
                  locale: { format: "YYYY-MM-DD" }
                  //startDate: moment().subtract(6, 'days')
                });
                </script>
            </div>
<?php include_once 'footer.php'; ?>

Are your id and subject fields really strings?

What is in the various variables when you debug the code? Does it produce the correct results if you run the queries, with those values, in something like phpmyadmin?

How can I make me renote that it doesn’t say it have the same lessons
What conditions should I use?

  1. Don’t use the submit button’s value to check if the form has been submitted.
  2. Hang on a second…

Where id = :id ? So your table is indexed by id. You should be checking the table WHERE the subject = :sb AND id=:id in your SELECT query then. Otherwise, it’s just looking for anyone who’s got that subject set, not this specific user ID.

I’m new in php , how do I fix what’s needed?

I’m a bit confused at the layout here. If the OP is just building up a table of subjects, they may be trying to ensure that they don’t have two subjects with the same name, to remove confusion if they’re sticking them in a drop-down list for example. At that point, they only care that the subject name is not duplicate. On the other hand, if we’re building some kind of timetabling system, or a list of subjects per student, then as you say there is a need to match the id and the subject. Some idea of the table layout would be useful.

I’m not sure now of the difference between id and subject_code.

And, when the form is submitted, I’m not sure where the value of $id is coming from. It doesn’t come from the little if() clause at the start, because the form is submitted with method=post and no parameters, and the code to process the form doesn’t get it from there, and in fact it’s not in the form at all. So in this case, when the form is submitted, $id will always be 1, won’t it? Or have I read it wrongly?

Thank you. It was a cure.

$stm = $db->prepare("SELECT * FROM subjects WHERE subject=:sb AND id != :id");

With the current code, id will always be 1. But that doesnt mean that the database table only contains entries with id = 1. Can’t extrapolate that assumption.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.