Jump to content


Photo

Mysql Review Help


  • Please log in to reply
5 replies to this topic

#1 Xider

Xider
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 August 2003 - 01:45 PM

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&nbsp;<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!

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 August 2003 - 07:32 PM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Xider

Xider
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 August 2003 - 11:18 PM

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:

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 04 August 2003 - 05:45 AM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Xider

Xider
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 August 2003 - 07:18 PM

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]

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 August 2003 - 07:55 AM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users