Best Way To Approve User-added Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asdfghjkl
Forum Newbie
Posts: 4
Joined: Tue Dec 01, 2009 2:03 pm

Best Way To Approve User-added Data

Post by asdfghjkl »

I’ve got a PHP system that uses SMF to handle the user login etc and so that I have a forum as well. I’ve got as far as users being able to login to the page this allows the user to add a review for an object on the object page using an HTML form and I use $_GET to get an ID from the URL and this displays data for the object of that ID so http://www.mywebpage.com/object.php?id=1 displays data for that object from a table called Objects. I have two tables that I want to display data from based on a common column. Here’s an example of the tables and their rows:

Objects (ID, Object, ObjectInfo)
Reviews (ID, Object, Username,Review)

I know that I can use LEFT JOIN to display data from the tables based on the common columns however it seems that I can’t get LEFT JOIN to work in the query when I use WHERE to get the ID of the Object. Basically I want the Object data to be displayed at the top of the page and the reviews are called in a loop because users can add reviews and I only want to display reviews for that object hense why I have a column in the two tables called Object. Here’s an example of how the code looks so far:

Code: Select all

<?php
include ’header.php’;
$id = $_GET[‘id’];
$sql = mysql_query(“SELECT * FROM Objects WHERE id = $id”);
$row = mysql_fetch_array($sql);
$object = $row[‘object’];
$objectInfo = $row[‘objectInfo’];
echo $object;
echo $objectInfo;
while($row = mysql_fetch_array($sql))
{
   // Here I want to display reviews that relate to the ID of the object
}
Once I figure this out I want to know what is the best way to approve user-added data. When the user adds a review I use a form that inserts the data into an approve table so that I can check for errors etc. I’ve set-up a page that selects all the data from the approve table and displays the data as values in a form so that I can submit the form and it will execute a script that adds the values into the reviews table but this is just too much because now I’m using a form to get data from the users and then I’m using a script to insert that into the approve table and now I’m trying to use a form and using the data as values in the form so that I can execute a script to insert that into the reviews table. Is there not an easier way to do this? I prefer to keep the forms and scripts separate. Let me show you what I mean…

So just below the loop in the above code I use an SMF global to check that the user is logged in by using if($context[‘user’][‘is_guest’]) and if they are I echo an HTML form and I’ve also used SMF globals to get the username and the object by calling $object as set previously in the code. If the user isn’t logged in then there will be a login form displayed at the top of the page as this has been included and it’s all working I’m just trying to give you a clear idea of what I’m trying to do and you can tell me if I’m going about this professionally:

Code: Select all

if($context[‘user’][‘is_guest’])
{
   echo “You must be logged in to add a review.”;
}
else
{
   echo “<form action=\”insert.php\” method=\”post\”>\n”;
   echo “<input type=\”hidden\” name=\”object\” value=\””.$object”\”>\n”;
echo “<input type=\”hidden\” name=\”username\” value=\””.$context[‘user’][‘name’].”\”>\n”;
   echo “Review: <input type=\”text\” name=\”review\”>\n”;
   echo “<input type=\”submit\” value=\”Submit\” />\n”;
}
include ’footer.php’;
?>
And here’s the add.php script used to process the form:

Code: Select all

 
<?php
include ‘db.php’;
$object = $_POST[‘object’];
$username = $_POST[‘username’];
$review = $_POST[‘review’];
mysql_query(“INSERT INTO approvals (object,username,review) VALUES (‘$object’,’$username’,’$review’)”);
?>
This all works okay but I’m stuck on what to do next in order to approve the data and transfer to the reviews table and delete the approved data from the approvals table. If anyone has any tips on how to improve my code or any ideas on what to do next please do try to respond asap. I’m really stuck right now and I didn’t get any sleep last night because of this. Thanks.
Last edited by John Cartwright on Tue Dec 01, 2009 7:58 pm, edited 1 time in total.
Reason: Please don't resize your entire post's font-size
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Best Way To Approve User-added Data

Post by yacahuma »

I am sorry if I did not get the whole idea of what your are trying to do. BUT. if your are saying that you have a table with some comments and you need to approved them, just add a flag (tinyint) to the table. The default value will be 0(not approved) and then when is approved , change the value to one.


Hope it helps
asdfghjkl
Forum Newbie
Posts: 4
Joined: Tue Dec 01, 2009 2:03 pm

Re: Best Way To Approve User-added Data

Post by asdfghjkl »

Yeah that does help thanks so much! Everything else is sorted as well.
Post Reply