Jump to content

Archived

This topic is now archived and is closed to further replies.

Xider

Mysql Review Help

Recommended Posts

Ok Im making a review script for my site, and I need a little help.

 

First, I made add.php

 

[php]<?php 

$location = "localhost"; 

$username = "username"; 

$password = "pass"; 

$database = "db"; 



$conn = mysql_connect("$location","$username","$password"); 

if (!$conn) die ("Could not connect MySQL"); 

mysql_select_db($database,$conn) or die ("Could not open database"); 



mysql_query("CREATE TABLE reviews (

   

   review_name VARCHAR(20), 

   basic_plot VARCHAR(80), 

   review VARCHAR(100),

   email VARCHAR(20),

   poster_url VARCHAR(20),

   publisher VARCHAR(20),

   author VARCHAR(30))"); 



if($submit); {

$insert = "INSERT INTO reviews (review_name,basic_plot,review,email,poster_url,publisher,author) 

   VALUES (\'$review_name\',\'$basic_plot\',\'$review\',\'$email\',\'$poster_url\',\'$publisher\',\'$author\')"; 

   mysql_query($insert) or die ("Could not add data to the table");

}

?>[/PHP]

 

On this one, im making an edit.php and delete.php, and I have to use ID\'s to make them work. How do you get this ID stuff in.

 

Then theres list.php

 

[PHP]



<?php 

$location = "localhost"; 

$username = "username"; 

$password = "pass"; 

$database = "db"; 



$conn = mysql_connect("$location","$username","$password"); 

if (!$conn) die ("Could not connect MySQL"); 

mysql_select_db($database,$conn) or die ("Could not open database");



$query = "SELECT * FROM reviews"; 

$result = mysql_query($query); 

$numrows = mysql_num_rows($result); 

while($row = mysql_fetch_array($result)){

   print("We have <b>$numrows</b> reviews in our database <p>"); 

   

}



?>



<?





$query = "SELECT * FROM reviews"; 

$result = mysql_query($query); 

$numrows = mysql_num_rows($result); 

while($row = mysql_fetch_array($result)){ 





echo "<b>$row[review_name]</b><br>";



}



?>

[/PHP]

 

Well since Im a newbie, I dont know how to stop this. For all the test reviews I put, after every one, it says, \"We have X many reviews in the DB\", but I only want it to show once. Also, how do you link it to the review.

 

Thanks!

Share this post


Link to post
Share on other sites

Adding id field

[php:1:63bb55816f]<?php

mysql_query(\"CREATE TABLE reviews (

review_id int not null auto_increment primary key,

review_name VARCHAR(20),

basic_plot VARCHAR(80),

review VARCHAR(100),

email VARCHAR(20),

poster_url VARCHAR(20),

publisher VARCHAR(20),

author VARCHAR(30))\");

?>[/php:1:63bb55816f]

 

Listing reviews

[php:1:63bb55816f]<?php

$query = \"SELECT review_id, review_name FROM reviews\";

$result = mysql_query($query);

$numrows = mysql_num_rows($result);

 

print(\"We have <b>$numrows</b> reviews in our database <p>\");

 

while($row = mysql_fetch_array($result)){

$id = $row[\'review_id\'];

echo \"<A href=\"showreview.php?id=$id\"><b>$row[review_name]</b></A><br>\";

 

}

 

 

?>[/php:1:63bb55816f]

 

In showreview.php

[php:1:63bb55816f]<?php

$id = $_GET[\'id\'];

$query = \"SELECT * FROM reviews WHERE review_id = $id\"

// display review contents

?>[/php:1:63bb55816f]

 

hth

Share this post


Link to post
Share on other sites

Thank you very much for your help, but theres a small problem.

[php:1:ceaf638428]

<?php

$location = \"localhost\";

$username = \"username\";

$password = \"pass\";

$database = \"dbname\";

 

$conn = mysql_connect(\"$location\",\"$username\",\"$password\");

if (!$conn) die (\"Could not connect MySQL\");

mysql_select_db($database,$conn) or die (\"Could not open database\");

 

 

$query = \"SELECT id, review_name FROM reviews\";

$result = mysql_query($query);

$numrows = mysql_num_rows($result);

 

print(\"We have <b>$numrows</b> reviews in our database <p>\");

 

while($row = mysql_fetch_array($result)){

$id = $row[\'id\'];

echo \"<A href=\"reviews.php?id=$id\"><b>$row[review_name]</b></A><br>\";

 

}

 

 

?>

[/php:1:ceaf638428]

 

On list.php, it comes up with these errors

 

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/virtual/site5/fst/var/www/html/reviews/list.php on line 14

We have reviews in our database

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/site5/fst/var/www/html/reviews/list.php on line 18

 

And yes, I changed review_id to id

 

Thanks agian, sorry im such a newb :oops:

Share this post


Link to post
Share on other sites

Have you added the id column to your reviews table yet?

 

You can do it without recreating the table with

 

ALTER TABLE reviews ADD id int auto_increment primary key

Share this post


Link to post
Share on other sites

Hi agian, sorry I wasn\'t able to come on here.

 

I tried, and it still doesn\'t work. Maybe I should show you what I have so far.

 

add.php

 

[php:1:f2c2662c81]
<?php
$location = \"localhost\";
$username = \"username\";
$password = \"pass\";
$database = \"db\";

$conn = mysql_connect(\"$location\",\"$username\",\"$password\");
if (!$conn) die (\"Could not connect MySQL\");
mysql_select_db($database,$conn) or die (\"Could not open database\");

mysql_query(\"CREATE TABLE reviews (
ALTER TABLE reviews ADD id int auto_increment primary key,
review_name VARCHAR(20),
basic_plot VARCHAR(80),
review VARCHAR(100),
email VARCHAR(20),
poster_url VARCHAR(20),
publisher VARCHAR(20),
author VARCHAR(30))\");

if($submit); {
$insert = \"INSERT INTO reviews (review_name,basic_plot,review,email,poster_url,publisher,author)
VALUES (\'$review_name\',\'$basic_plot\',\'$review\',\'$email\',\'$poster_url\',\'$publisher\',\'$author\')\";
mysql_query($insert) or die (\"Can\'t add to table. Please report bug to Xider.\");
}

?>
[/php:1:f2c2662c81]

?>[/code]

Share this post


Link to post
Share on other sites

Nooo!

 

Either recreate table completely (losing data) with my original create SQL

 

mysql_query("CREATE TABLE reviews ( 

   id int auto_increment primary key, 

   review_name VARCHAR(20), 

   basic_plot VARCHAR(80), 

   review VARCHAR(100), 

   email VARCHAR(20), 

   poster_url VARCHAR(20), 

   publisher VARCHAR(20), 

   author VARCHAR(30))"); 

or run alter query on its own - adds field witthout you losing data

 

 

ALTER TABLE reviews ADD id int auto_increment primary key

 

hth

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.