Insert data from a link into mysql

Hello everyone
i am new here.
i’d like you to help me with a php problem i am confronting.
i get back a value by a variable from html balise with $_GET['id']; method

<a href=readingall.php?read=".$row['id'].">read more</a>

in the readingall page i want to insert this same value that i get back from the link into a sql querry,

here is some code

  $id = ($_GET['id']);
  $abc = $id;
  if(isset($_POST['poste'])){
  $con = mysqli_connect("localhost","root","");
  mysqli_select_db($con,"blog");

  $iduser = $_SESSION['userID'];
  $date = date('H:i / d-m-y');
  $pseudo = $_POST['pseudo'];
  $commentaire = $_POST['commentaire'];

    $sql = "INSERT INTO commentaire (textec, datec,id)
    VALUES('$commentaire', '$date','$abc')";
    mysqli_query($con, $sql);
    header("Location:index.php?upload=success");

it won’t work. how can you help me please?

What is the purpose for this first of all? Is it to track someone while they’re visiting a few links? We need to know the purpose in order to know what the code should be doing.

uh ok
so, it is about the comments i am trying to do.
then i need to save the id of the article that the user comment into the comment table in the database

Look at your link code. Are you “GETting” ‘id’ when link is pressed or maybe something else is defined like ‘read’. Code should be consistent.

when i click on ‘read more’
the id of the article that have been clicked is send by the link and display the article from the database, that works perfectly, in the same page that displayed the article there is a form to write your comment so i need the id of that article to save in the database because i’ll need it.
when i click on the comment button, it does not work. i mean it does not want to insert into the database, i don’t think it is a sql querry problem.
i’d like you to help to konw why after if(isset($_POST['poste'])){ … the value have sent by the link can’t still use to insert into the database.

You’re doing it correctly in an incorrect way. Where is the rest of your code?

let me a one minute to post it

here is where i display all articles from the database.

<?php
    $conn = mysqli_connect('localhost','root','','blog');

    $sql = "SELECT a.id, a.image, a.titre,  a.text, a.dateart, u.uidUsers, u.profilepic
    FROM articles a, users u WHERE a.idUsers = u.idUsers GROUP BY id DESC";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_array($result)){
      echo"<br/>";
      echo"<div class=\"contenue\">";

      $strcut = substr($row['text'], 0, 260);
      $desc = substr($strcut, 0, strrpos($strcut, ' ')).'...';

      if($row['profilepic']==''){
        echo"<a href=\"#\"><img style=\"=width:30px;height:30px;\" src='profile/defaultpic.png'> ".$row['uidUsers']."</a>";
        echo"<label style=\"font-size:13px;\"> &nbsp;| Cet article a ete publie a ".$row['dateart']."</label>";
      }else{
        echo"<a href=\"#\"><img style=\"=width:30px;height:30px;\" src='profile/".$row['profilepic']."'> ".$row['uidUsers']."</a>";
        echo"<label style=\"font-size:13px;\"> &nbsp;| Cet article a ete publie a ".$row['dateart']."</label>";
      }
      echo"<hr/>";
      echo"<h3>".$row['titre']."</h3>";
      echo"<img style=\"=width:200px;height:200px;\" src='images/".$row['image']."'>";

      $url = "readingall.php?read=".$row['id']."";
      echo"<p>".$desc."
      <a href=readingall.php?read=".$row['id'].">Lire la suite</a>
      </p>";

      echo"<a href=\"\">J'aime</a> | <a href=\"#\">J'aime pas</a> | <a href=\"#\">Commentaire</a>";
      echo"</div>";
    }
  ?>

here is where i display the article whe the id from database = to id from the link

<?php include("head.inc.php");
  $id = ($_GET['read']);
  $idc = $id;
  if(isset($_POST['poste'])){
  $aaa = $_GET['$id'];
  $con = mysqli_connect("localhost","root","");
  mysqli_select_db($con,"blog");

  $iduser = $_SESSION['userID'];
  $date = date('H:i / d-m-y');
  $pseudo = $_POST['pseudo'];
  $commentaire = $_POST['commentaire'];

  if(isset($_SESSION['userID'])){

    $sql = "INSERT INTO commentaire (idUsers, textec, datec,id)
    VALUES('$iduser','$commentaire', '$date','$idc')";
    mysqli_query($con, $sql);
    header("Location:index.php?upload=success");
    exit();
  } else{
    $sql = "INSERT INTO commentaire (pseudo, textec, datec, id)
    VALUES('$pseudo', '$commentaire', '$date', $idc)";
    mysqli_query($con, $sql);
    header("Location:index.php?upload=success");

  }

}

?>
<br/><br/>

<link rel="stylesheet" href="styles/index.css">
<link rel="stylesheet" href="styles/affiche_art.css">
<link rel="stylesheet" href="styles/comments.css">


<body style="background-color:#e2e2e2;">
<div class="aff_art">
  <?php

    $conn = mysqli_connect('localhost','root','','blog');
    $sql = "SELECT a.id, a.image, a.titre,  a.text, a.dateart, u.uidUsers, u.profilepic
    FROM articles a, users u WHERE a.idUsers = u.idUsers AND a.id = '$id'";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_array($result)){
      echo"<br/>";
      echo"<div class=\"contenue\">";


      if($row['profilepic']==''){
        echo"<a href=\"#\"><img style=\"=width:30px;height:30px;\" src='profile/defaultpic.png'> ".$row['uidUsers']."</a>";
        echo"<label style=\"font-size:13px;\"> &nbsp;| Cet article a ete publie a ".$row['dateart']."</label>";
      }else{
        echo"<a href=\"#\"><img style=\"=width:30px;height:30px;\" src='profile/".$row['profilepic']."'> ".$row['uidUsers']."</a>";
        echo"<label style=\"font-size:13px;\"> &nbsp;| Cet article a ete publie a ".$row['dateart']."</label>";
      }
      echo"<hr/>";
      echo"<h3>".$row['titre']."</h3>";
      echo"<img style=\"=width:200px;height:200px;\" src='images/".$row['image']."'>";
      echo"<p>".$row['text']."</p>";

      echo"<a href=\"#\">J'aime</a>
      | <a href=\"#\">J'aime pas</a>
      | <a href=\"#\">Commentaire</a>";
      echo"</div>";

      echo"<br/>";
  ?>

<!-- code action commentaire-->

  <?php
    if(isset($_SESSION['userID'])){
      echo'
      <div class="contenue">
      <div class="commentaire">
      <form action="readingall.php" method="post">
      <textarea name="commentaire" id="commentaire" rows="2" cols="50" placeholder="Commenter ici"></textarea>
      <input type="submit" name="poste" id="poste" value="POSTE">
      </form>
      </div>
      </div>
      ';
    }else{
      echo'
      <div class="contenue">
      <div class="droite">
        <h5>Commenter en tant qu\'un visiteur</h5>
        <form action="readingall.php" method="post">
          <input type="text" name="pseudo" id="pseudo" placeholder="Pseudo">
          <textarea name="commentaire" id="commentaire" rows="2" cols="5" placeholder="Commenter ici!"></textarea>
          <button type="submit" name="poste" id="poste">POSTE</button>
        </form>

      </div>

      <div class="gauche">
        <h5>Connectez ou incrivez vous!</h5>
        <form action="login.php" method="post">
          <button type="submit">Connexion</button>
        </form>
        <br/>
        <form action="ncomptee.php" method="post">
          <button type="submit" name="button">Inscription</button>
        </form>
      </div>
    </div>
      ';
    }
  }
?>

there is some french text, but i think that not gonna be a problem
i am from haiti.

See a problem here? All of these don’t have the same “value”. More importantly, you go from an input button to a regular button. So the $_POST section won’t even get triggered because none of these elements have the poste as their value. POSTE and poste are 2 completely different things when dealing with case sensitive languages. What I suggest is to just do

if($_SERVER['REQUEST_METHOD'] == 'POST') {

    $aaa = $_GET['read'];
....

Instead of using if(isset($_POST['poste'])). This way, you aren’t relying on an input field to see if your form was submitted or not.

thank you!
let me try to fix that.
thank you so much!

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