php - SQL insert query loop -


hello doing school assigment have make comment system corresponding post id , know looping 3 times gave post id. , know postid changeing time. have no idea how fix bug ideas?

<?php require_once("menu.php"); $connection = connecttomysql();  $selectpostquery = "select * (select * `tblposts` order id desc limit 3) t order id desc";  $result = mysqli_query($connection,$selectpostquery)     or die("error in query: ". mysqli_error($connection)); while ($row = mysqli_fetch_assoc($result))  {     $postid = $row['id'];          if (!empty($_post['comment']) ) #to insert new comments in database         {             $comment = $_post['comment'];             $userid = $_session['userid'];             $insertcommentquery = "insert `tblcomments` (`content`,`userid`,`postid`,`timestamp`) values ('$comment','$userid','$postid',current_timestamp)";              $resultcomment = mysqli_query($connection, $insertcommentquery)                 or die("error in query: ". mysqli_error($connection));         }      echo "<div class=\"wrapper\">";     echo "<div class=\"titlecontainer\">";     echo "<h1>$row[title]</h1>";     echo "</div>";     echo "<div class=\"textcontainer\">";     echo "<span>$row[content]</span>";     echo "</div>";      if (!empty($row['imagepath'])) #this check if there path in textfield     {     ?>         <div class="imagecontainer">         <img src="images/<?php echo "$row[imagepath]"; ?>">         </div> <?php     }     echo "<div class=\"timestampcontainer\">";     echo "<b>date posted :</b>$row[timestamp] ";     echo "<b>author :</b> admin";     echo "</div>";      #selecting comments corresponding post     $selectcommentquery = "select * `tblcomments` left join `tblusers` on tblcomments.userid = tblusers.id tblcomments.postid ='$postid'";      $commentresult = mysqli_query($connection,$selectcommentquery)         or die ("error in query: ". mysqli_error($connection));      while ($commentrow = mysqli_fetch_assoc($commentresult))      {         echo "<div class=\"commentcontainer\">";         echo "<div class=\"commentusername\"><h1>username :$commentrow[username]</h1></div>";         echo "<div class=\"commentcontent\">$commentrow[content]</div>";         echo "<div class=\"commenttimestamp\">$commentrow[timestamp]</div>";         echo "</div>";     }       if (!empty($_session['userid']) )      {         echo "<form method=\"post\" action=\"\" class=\"post-frm\">";         echo "<label>new comment</label>";         echo "<textarea id=\"comment\" name=\"comment\"> </textarea>";         echo "<input id=\"submit\" type=\"submit\" name =\"submit\" class=\"button\"/>" ;         echo "</form>";     }     echo "</div>";     echo "<br /> <br /><br />";  } 

require_once("footer.php") ?>

well, that's script does. queries posts, loops through them, , performs insert of them. fix this, store id of post in comment form. when post form, insert single comment , use id in form.

that this:

<?php if (array_key_exists('postid', $_post)) {   $postid = $_post['postid'];   $comment = $_post['comment'];    // perform single insert here, , use $postid , $comment. }  // then, start rendering page:  require_once(menu.php); $connection = connecttomysql();  $selectpostquery = select * (select * `tblposts` order id desc limit 3) t order id desc;  $result = mysqli_query($connection,$selectpostquery)     or die(error in query: . mysqli_error($connection)); while ($row = mysqli_fetch_assoc($result))  {   $postid = $row['id'];   // render post here. ?>   <div class="wrapper">;   <div class="titlecontainer">;   <h1><?=$row['title']?></h1>;   </div>;   <div class="textcontainer">;   <span><?=$row['content']?></span>;   </div>;  <?php   // render comment form each post (is did?)   if (!empty($_session['userid']) )    {?>   <form method=post action= class=post-frm>     <label>new comment</label>     <textarea id=comment name=comment></textarea>     <input type=hidden name=postid value=<?=$postid?>/>     <input id=submit type=submit name =submit class=button/>   </form>       <?} } 

most of code same, processing of post data done before loop.

otherwise, fixed small syntactic things (and maybe introduced new ones, haven't tested it).

also, took html out of echoes. it's matter of taste, of course, experience has taught me big chunks of html in echo statements isn't readable or maintainable. rather close php tags, output raw html , echo variables in it. can use short notation that: <?= $value ?>, means <?php echo $value ?>.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -