Jump to content

Mysql Review Help


Xider

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!

Link to comment
https://forums.phpfreaks.com/topic/825-mysql-review-help/
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

Link to comment
https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2739
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:

Link to comment
https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2741
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]

Link to comment
https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2848
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

Link to comment
https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2855
Share on other sites

Archived

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

×
×
  • Create New...

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.