Solution 1 :

The mistake you did was that you closed the while loop.Also, put the i outside for better approach.

$i=0;
while($row = mysqli_fetch_array($result))  {

//echo $row["title"];
//echo $row["description"];
//echo $row["datef"];


$sql = "UPDATE agenda SET title = $title, description = $desc, datef = $date1, inserter = $inserter WHERE id = $row[id]; ";
  $i++;
if(mysqli_query($link, $sql)){
header("Refresh:0; url=agenda.php");
}
}
?>

Problem :

I want to get id of a data in php to update it in my database. I have tried a lot of things I found online but could not solve the issue. Here is ALL the code for editing.

This is where the posts are displayed but I don’t know how to get id for each post so that people can edit/can be edited on mysql

<?php

$link = mysqli_connect ('localhost', 'root', '', '');

mysqli_select_db ($link, 'CodersHome');

?>

<?php
session_start ();
if( !isset($_SESSION['username']))
header('location:../login.php');
 ?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>CodersHome Agenda</title>
    <link rel="shortcut icon" type="image/png" href="../tabimg.png">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="profile/profile.css">
    <link rel="stylesheet" type="text/css" href="../navbar/navbar.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendap.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendacard.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <script src="agendatabs.js" charset="utf-8"></script>
  </head>
<?php include '../navbar/navbar.html' ?>
  <body>


    <div id="page-container">
      <div id="content-wrap">

        <ul class="tab">
          <li><a href="#" class="tablinks active" onclick="openCity(event, 'agenda')"><strong>Agenda</strong></a></li>
          <li><a href="#" class="tablinks" onclick="openCity(event, 'createagenda')"><strong>Crea Agenda</strong></a></li>
        </ul>
<br>
    <div id="agenda" class="tabcontent">
      <div class="welcome">
        <h1 class="weltitle">Agenda 1er D</h1>
     </div>
     <br><hr><br>
      <?php
      $result = mysqli_query($link,"SELECT * FROM agenda");

      while($row = mysqli_fetch_array($result))  {

      //echo $row["title"];
      //echo $row["description"];
      //echo $row["datef"];


      ?>

      <?php $i=0; ?>
      <div class="row">
      <div class="column">
      <div class="card">
    <?php echo $row["description"]; ?>
    <?php echo $row["datef"]; ?>  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <?php echo $row["inserter"]; ?> <?php if($_SESSION['username'] == 'admin') { include('eddel.php');

    } ?> <br>
    </div>
    </div>
    </div>
      <br>
      <?php
      $i++;
      }
      ?>

    </div>

Here you can see where I try to Update.

<?php

session_start ();

$link = mysqli_connect ('localhost', 'root', '', '');

mysqli_select_db ($link, '');


if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}

$title = $_POST ['title'];
$desc = $_POST ['editor1'];
$date1 = $_POST ['date1'];
$inserter = $_SESSION['username'];

$result = mysqli_query($link,"SELECT * FROM agenda");

while($row = mysqli_fetch_array($result))  {

//echo $row["title"];
//echo $row["description"];
//echo $row["datef"];
$i=0;
}
$sql = "UPDATE agenda SET title = $title, description = $desc, datef = $date1, inserter = $inserter WHERE id = $row[id]; ";
  $i++;
if(mysqli_query($link, $sql)){
header("Refresh:0; url=agenda.php");
}

?>

<?php
if( !isset($_SESSION['username']))
header('location:../login.php');
?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
   <meta charset="utf-8">
   <title>CodersHome Agenda</title>
   <link rel="shortcut icon" type="image/png" href="../tabimg.png">
   <meta name="viewport" content="width=device-width,initial-scale=1">
   <link rel="stylesheet" href="profile/profile.css">
   <link rel="stylesheet" type="text/css" href="../navbar/navbar.css">
   <link rel="stylesheet" type="text/css" href="agendacss/agendap.css">
   <link rel="stylesheet" type="text/css" href="agendacss/agendacard.css">
   <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
   <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
   <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
   <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
   <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
   <script src="agendatabs.js" charset="utf-8"></script>
 </head>
<?php include '../navbar/navbar.html' ?>
 <body>


<div class="welcome2">
   <p class="weltitle">Edita Una Agenda</p>
</div>
<br><hr><br>
           <form class="agfrm" action="agendaedit.php" method="post">

             <div class="form-group">

               <label>Title</label> <br> <br>
               <input type="text" name="title" class="form-control" required><br> <br>

             </div>

             <div class="form-group">

               <label>Description</label> <br> <br>

               <textarea name="editor1" required></textarea>
                    <script>
                            CKEDITOR.replace( 'editor1' );
                    </script> <br> <br>
             </div>

           <label>Date</label> <br> <br>
           <input type="date" id="date1" name="date1"
                  value="2020-02-25"
                  min="2020-01-01" max="2021-12-31" required> <br>
                  <br>
       <button type="submit" class="homepbtn1">Sumbit</button>


       </form>

   </div>


     </div>


 </body>

</html>

<?php include '../Footer/index.html'?>


I want to update a data in a table. I also have the id assgined for each row but i dont know how to get the id to edit the exact row of the table.

Also here is a live demo of where post is created and diplayed.

function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}
body {
background-color: white;
  height: 96vh;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}


.welcome {
  border: 2px solid black;
  color: #7d3caf;
  margin-top: 40px;
  margin-left: 40px;
  margin-right: 40px;
  border-radius: 40px;
  transition: 0.5s ease;
  font-weight: 5em;
  font-family: 'Century Gothic', sans-serif;
  background-color: #f1f1f1;
}

.welcome:hover
{
  border: 1px solid #6747c7 ;
  background-color: #6747c7;
  color: white;
  font-weight: bold;
  font-family: 'Century Gothic', sans-serif;

   }


.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 30px;
  margin-bottom: 30px;
}
 .weldis {
   font-family: 'Century Gothic', sans-serif;
   text-align: center;
 }
.gtac {
  text-align: center;
  font-family: monospace;
}
.gtacl {
  text-align: center;
}

.gtac1:hover {
  color: black;
  background-color: black;
  border:  2px solid black;
}

.lbtn {
  font-size: 20px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  border: 1px solid #fff;
  transition: 0.6s ease
}

.lbtn:hover{
  background-color: #7a7979;
  color: white;
  font-size: 24px;
}


.span1 {
  height: 40px;
}

.homepbtn1 {
  top: 50%;
  left: 80%;
  background-color: #36344d;
  margin-right: 20px;
  font-size: 18px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  transition: 0.6s ease;
  border-radius: 10px;
  cursor: pointer;
}

.homepbtn1:hover {
  background-color: #7a7979;
  color: white;
  background-color: #FF5A36;
    border-radius: 50px;

}

.btnmargin1 {
  height: 60px;
}

.btnmargin2 {
  height: 60px;
}

.extraspace {
  height: 6px;
}



.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 36px;

}
 .weldis {
   font-family: 'Century Gothic', sans-serif;

 }

ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.tab li a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}
div[class*="tabcontent"]:first-of-type {
    display: block;
}
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

/* Dropdown Button */
.dbtn {
  color: black;
  font-size: 30px;
  border: none;
  background-color: transparent;
  cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.ddown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dcon {
  display: none;
  font-size: 16px;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dcon a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dcon a:hover {background-color: #ddd;}

/* Show the dropdown menu on hover */
.ddown:hover .dcon {display: block;}

/* Change the background color of the dropdown button when the dropdown content is shown */
.down:hover .dbtn {background-color: transparent;}

body {
background-color: white;
  height: 96vh;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}


.welcome {
  border: 2px solid black;
  color: #7d3caf;
  margin-top: 40px;
  margin-left: 40px;
  margin-right: 40px;
  border-radius: 40px;
  transition: 0.5s ease;
  font-weight: 5em;
  font-family: 'Century Gothic', sans-serif;
  background-color: #f1f1f1;
}

.welcome:hover
{
  border: 1px solid #6747c7 ;
  background-color: #6747c7;
  color: white;
  font-weight: bold;
  font-family: 'Century Gothic', sans-serif;

   }


.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 30px;
  margin-bottom: 30px;
}
 .weldis {
   font-family: 'Century Gothic', sans-serif;
   text-align: center;
 }
.gtac {
  text-align: center;
  font-family: monospace;
}
.gtacl {
  text-align: center;
}

.gtac1:hover {
  color: black;
  background-color: black;
  border:  2px solid black;
}

.lbtn {
  font-size: 20px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  border: 1px solid #fff;
  transition: 0.6s ease
}

.lbtn:hover{
  background-color: #7a7979;
  color: white;
  font-size: 24px;
}


.span1 {
  height: 40px;
}

.homepbtn1 {
  top: 50%;
  left: 80%;
  background-color: #36344d;
  margin-right: 20px;
  font-size: 18px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  transition: 0.6s ease;
  border-radius: 10px;
  cursor: pointer;
}

.homepbtn1:hover {
  background-color: #7a7979;
  color: white;
  background-color: #FF5A36;
    border-radius: 50px;

}

.btnmargin1 {
  height: 60px;
}

.btnmargin2 {
  height: 60px;
}

.extraspace {
  height: 6px;
}



.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 36px;

}
 .weldis {
   font-family: 'Century Gothic', sans-serif;

 }

ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.tab li a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}
div[class*="tabcontent"]:first-of-type {
    display: block;
}
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}
<?php

$link = mysqli_connect ('localhost', 'root', 'mywebsite1245', 'CodersHome');

mysqli_select_db ($link, 'CodersHome');

//$con = mysqli_connect ('Localhost', 'id12806518_root', 'mywebsite1245', 'id12806518_codershome');

//mysqli_select_db ($con, 'id12806518_codershome');
?>

<?php
session_start ();
if( !isset($_SESSION['username']))
header('location:../login.php');
 ?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>CodersHome Agenda</title>
    <link rel="shortcut icon" type="image/png" href="../tabimg.png">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="profile/profile.css">
    <link rel="stylesheet" type="text/css" href="../navbar/navbar.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendap.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendacard.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <script src="agendatabs.js" charset="utf-8"></script>
  </head>
<?php include '../navbar/navbar.html' ?>
  <body>


    <div id="page-container">
      <div id="content-wrap">

        <ul class="tab">
          <li><a href="#" class="tablinks active" onclick="openCity(event, 'agenda')"><strong>Agenda</strong></a></li>
          <li><a href="#" class="tablinks" onclick="openCity(event, 'createagenda')"><strong>Crea Agenda</strong></a></li>
        </ul>
<br>
    <div id="agenda" class="tabcontent">
      <div class="welcome">
        <h1 class="weltitle">Agenda 1er D</h1>
     </div>
     <br><hr><br>
      <?php
      $result = mysqli_query($link,"SELECT * FROM agenda");

      while($row = mysqli_fetch_array($result))  {

      //echo $row["title"];
      //echo $row["description"];
      //echo $row["datef"];


      ?>

      <?php $i=0; ?>
      <div class="row">
      <div class="column">
      <div class="card">
    <?php echo $row["description"]; ?>
    <?php echo $row["datef"]; ?>  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <?php echo $row["inserter"]; ?> <?php if($_SESSION['username'] == 'admin') { include('eddel.php');

    } ?> <br>
    </div>
    </div>
    </div>
      <br>
      <?php
      $i++;
      }
      ?>

    </div>

    <div id="createagenda" class="tabcontent">


<div class="welcome2">
    <p class="weltitle">Crea Una Agenda</p>
</div>
<br><hr><br>
            <form class="agfrm" action="insert.php" method="post">

              <div class="form-group">

                <label>Title</label> <br> <br>
                <input type="text" name="title" class="form-control" required><br> <br>

              </div>

              <div class="form-group">

                <label>Description</label> <br> <br>

                <textarea name="editor1" required></textarea>
                     <script>
                             CKEDITOR.replace( 'editor1' );
                     </script> <br> <br>
              </div>

            <label>Date</label> <br> <br>
            <input type="date" id="date1" name="date1"
                   value="2020-02-25"
                   min="2020-01-01" max="2021-12-31" required> <br>
                   <br>
        <button type="submit" class="homepbtn1">Sumbit</button>


        </form>

    </div>


      </div>


  </body>

</html>

  <?php include '../Footer/index.html'?>

Comments

Comment posted by prepared statements

String values need to be quoted or else they will be treated as column names, causing the query to fail. However, ssince you’re using mysqli, take advantage of

Comment posted by Aioros

I don’t understand what rows are you trying to update.

Comment posted by bobby-tables.com

Warning:

Comment posted by ADyson

…and it will probably also help with the SQL syntax error you’re almost certainly experiencing (but haven’t reported).

Comment posted by ADyson

Also,

Comment posted by ADyson

if it’s really the OP’s intention to update

Comment posted by AbisGamerYT

How do I pass from the html file ? I also adeed the html file above

Comment posted by P.Srivastava

@AbisGamerYT use something like this

Comment posted by AbisGamerYT

Ok so I get the id in input and hide it right ? I tried that before but I don’t know how to get that ECHO value that is hidden to my php file. Do I have to use

Comment posted by P.Srivastava

yes using post or get method but make sure there are conditions given if the post method has a NULL value, so you have to set the default value

By

Leave a Reply

Your email address will not be published. Required fields are marked *