Skip to main content

Posts

Showing posts from October, 2015

Admin panel of a Q & A Forum

In a Q & A Forum, when a user posts a question, it should be sent to the administrator for approval in case it contains inappropriate content. After approval it should be removed from this pending approval page and other users should be able to see the question afterwards. To enable this, we should maintain an approval column in our database table of records and for each record approval should be set to false by default. In the Pending approvals page only the records with approval=false should be displayed. Below is  the MySQL  statement for retrieval, $sql="SELECT * FROM topics WHERE approval=false"; To know which post was approved we should embed the post_id to the URL. And the relevant post should be updated as approval=true. Below is the complete code. <?php $sql="SELECT * FROM topics WHERE approval=false"; $query=mysqli_query($conn,$sql); echo '<form name="approve" method="p...

Rating Counter using PHP

First, we should maintain the rate count in the database separately  for each record (whichever we are going to rate). For this we can use a separate column for this, which will be referred to as  rating  in this post. Basically, what is to be done is to increment the rating, which should be 0 by default, by one each time the +1 button is clicked and to decrement when the -1 button is clicked. if(isset($_GET['postIdPlus'])) //if plus button is clicked { $sql1="UPDATE posts SET rating=rating+1"; $r esult1=mysqli_query($conn,$sql1); } if(isset($_GET['postIdMinus']))   //if minus button is clicked { $sql1="UPDATE posts SET rating=rating-1"; $result1=mysqli_query($conn,$sql1); } When there are many posts, we should distinguish which post was rated. For this purpose, we shall embed the post_id (retrieved from the database along with the other details) to the URL.  echo " <a hre...

To restrict access to certain users

In a website, we need to restrict views to users. As an instance, Admin panel should be accessible for only the administrators. <?php session_start();  if(!empty($_SESSION['login_user'])) // if a user is logged in { $user=$_SESSION['login_user']; include './dbconnect.php'; $sql3="select user_level from users where user_name='$user'";  // checking  the user level of the user logged in $result3 = mysqli_query($conn,$sql3) or die (mysqli_error($conn)); if(mysqli_num_rows($result3) > 0) { while($row3=mysqli_fetch_array($result3)) { $userlevel=$row3['user_level']; } // if the user is not an admin, to redirect to the home page if($userlevel!=1) { echo '<script type="text/javascript"> location="index.php"; alert("You do not have access"); </script>'; } } } // if a user is not logged in else{ echo '<sc...

Creating a Log In/Sign Up page from Scratch using php

HTML Sign-up form <div id="signup-form">                             <form name="f1" method="post" action="<?php $_SERVER['PHP_SELF']?>" onSubmit="return validate_signup()" >                                 <table>                                     <br><br>                                     <tr><td><input type="text" name="name" id="name" placeholder="Name"></td></tr><br>                                     <tr><td><input type="email" id="mail" name="mail"...