Xider Posted August 3, 2003 Share Posted August 3, 2003 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! Quote Link to comment https://forums.phpfreaks.com/topic/825-mysql-review-help/ Share on other sites More sharing options...
Barand Posted August 3, 2003 Share Posted August 3, 2003 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 Quote Link to comment https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2739 Share on other sites More sharing options...
Xider Posted August 3, 2003 Author Share Posted August 3, 2003 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: Quote Link to comment https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2741 Share on other sites More sharing options...
Barand Posted August 4, 2003 Share Posted August 4, 2003 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 Quote Link to comment https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2744 Share on other sites More sharing options...
Xider Posted August 8, 2003 Author Share Posted August 8, 2003 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] Quote Link to comment https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2848 Share on other sites More sharing options...
Barand Posted August 9, 2003 Share Posted August 9, 2003 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 Quote Link to comment https://forums.phpfreaks.com/topic/825-mysql-review-help/#findComment-2855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.