PDA

View Full Version : Is there a way...?



maverick
07-05-2007, 02:34 AM
Hi,

I have a small html site where I write reviews of books I've read and I am try to find a way to keep a track of the number of reviews by genre (horror,crime).

Is there a way that i can insert a php script into each table (each review is in a table box) so I can later display at the top of the genre page "There is ... reviews in this section." ?

And if thats possible, would it be possible to display the total amount of reviews in the whole site like "We have read a total of ... books." ? Sorry I'm a total php newbie, only good with html.

Thanks for your help, :)

Mav

Jas
07-05-2007, 03:20 AM
Absolutely, though it will take some re-structuring, I'm sure. First off, all of your reviews will need to be in a MySQL Database (are they in HTML tables now?) and then you can use PHP to retrieve the information. Another advantage is that users can search your reviews. But again, this will take some re-structuring. If you need to learn about PHP, you can take a crash-course with video tutorials at www.phpvideotutorials.com or www.sampsonresume.com (I would also recommend reading up on it with google searches). Otherewise, have fun :)

JShor
07-05-2007, 02:54 PM
Yes you can do this easily in php. Since you are a newbie @ php, you may have a little difficulty at first, but practice makes perfect :)

First off, in your web hosting control panel, create a user, and a database, and add them together with ALL permissions set. Next, go to your administration for mysql (if you have one, usually it is phpMyAdmin), click on the database name you created to the left, and click on the SQL tab on the top.

From there, run this query:

CREATE TABLE `reviews` (
`id` int(11) NOT NULL auto_increment,
`genre` varchar(255) collate latin1_general_ci NOT NULL,
`horror` varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;


Then, to add stuff to the DB, make a page called 'insert.php', and cut and paste this code into it:


<?php

mysql_connect("YOUR_HOST", "YOUR_DB_USER", "YOUR_DB_PASS") or die(mysql_error());
mysql_select_db("YOUR_DB_NAME") or die(mysql_error());

$mystery = $_POST['mystery'];
$horror = $_POST['horror'];

mysql_query("INSERT INTO reviews (mystery, horror) VALUES('$mystery, '$horror') ") or die(mysql_error());

?>
<html>
<form action=insert.php method=post>
Review for genre: <input type=text name=genre><br />
Review for horror: <input type=text name=horror><br />
<input type=submit value='Submit Review To Database'></form>
</html>

From that, you will have information orgranized into a nice and neat format in mysql. Hope you enjoy PHP!

Twey
07-05-2007, 10:06 PM
`genre` varchar(255) collate latin1_general_ci NOT NULL,
`horror` varchar(255) collate latin1_general_ci NOT NULL, Hm? This looks rather odd to me... what exactly do you expect the "horror" column to contain? Personally, I'd just have "genre" and give it values like "horror."

JShor
07-06-2007, 02:14 AM
Your right Twey, my mistake....just change the 'genre' table to something else such as mystery or whatever.

maverick
07-06-2007, 11:15 PM
thanks for replying :) so to add a movie to the database I just put the name of the movie in the "Review for .....:" form box?

How would I add new categories to the insert.php, just add another line of
$genrename = $_POST['genrename']; and add it aswell to

mysql_query("INSERT INTO reviews (mystery, horror,genrename) VALUES('$mystery', '$horror', '$genrename') ") or die(mysql_error()); ?

What code do I use to display the amount of reviews in each category and the total amount in all categories?

Thanks for your help :D

Twey
07-06-2007, 11:23 PM
JShor, beware, you haven't escaped that SQL properly.

confusio
07-09-2007, 12:37 AM
this seems like a good idea, I could use something like that on my site aswell,would it be possible to add sub genres?

GeneralB
07-09-2007, 08:26 AM
This does seem useful ...

I have visited dynamicdrive often but never signed up for forum until about 10 minutes ago. I was searching how to call single file to multiple pages when I stumbled across this question. I'll have to mark it for later when I get to the MySql point.

Meanwhile, Mav, I tried to email you direct but couldn't. Wanted to ask you a ?

Twey
07-09-2007, 09:42 AM
You really do need to escape those variables with mysql_real_escape_string(). As it stands, an attacker can execute arbitrary queries against your database.