making a SQL powered website
Moderator: General Moderators
making a SQL powered website
This may sound a bit confusing so I'll use an example.
Say I want to make a book-review website using PHP, and I want to write lots of book reviews. Instead of creating a page for every book, which could mean hundreds and hundreds, is there a way you can do this with mySQL and if so, how?
I would prefer to maybe fill out a form on a website with the 'book-review' which would enter the data into the database, and then you will be able to access this 'book-review' using a URL (in your website template of course)
Please reply and help me
Say I want to make a book-review website using PHP, and I want to write lots of book reviews. Instead of creating a page for every book, which could mean hundreds and hundreds, is there a way you can do this with mySQL and if so, how?
I would prefer to maybe fill out a form on a website with the 'book-review' which would enter the data into the database, and then you will be able to access this 'book-review' using a URL (in your website template of course)
Please reply and help me
well i'll give you an example, but there are just some things you are going to *have* to do on your own. And trust me, these things aren't something that would take an einstien to figure out... But basically, you are going to need to read the MySQL manual included with the distribution file, along with http://www.php.net/mysql function list to get a basic understanding of how to connect to mysql, and request information from tables. You are probably also going to have to look into http://www.php.net/arrays..
but anyways, here would be an easy/quick example (assuming you have a table setup to work with this code.. )
add_book.htm
add_book.php
Search.htm
search.php
view.php
Hope this helps.
but anyways, here would be an easy/quick example (assuming you have a table setup to work with this code.. )
add_book.htm
Code: Select all
<html>
<head><title>Book Administration</title></head>
<body>
<form name="add_book" method="post" action="add_book.php">
<table>
<tr>
<td>Book Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Author's Name</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>Date Published</td>
<td><input type="text" name="date"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Add Book"></td>
</tr>
</table>
</form>
</body>
</html>add_book.php
Code: Select all
<?php
if(!isset($_POST['name']) || !isset($_POST['author']) || !isset($_POST['date']))
{
echo 'All Fields Must Be Entered Before This Book Can Be Added!<br />Please Try Again! <br /><br /><a href="add_book.htm">Back</a>'
exit;
}
$name = $_POST['name'];
$author = $_POST['author'];
$date = $_POST['date'];
mysql_connect('localhost','username','password');
mysql_select_db('my_books_db');
$sql = "INSERT into book_collection (Book_Title, Author, Date_Published) VALUES ('".$name."','".$author."','".$date."')";
$result = mysql_query($sql) or die(MySQL_Error());
echo 'Book Successfully Added!<br /><br /><a href="add_book.htm">Add Another Book</a>';
?>Search.htm
Code: Select all
<html>
<head><title>Search for a Book!</title></head>
<body>
<form name="search" method="post" action="search.php">
<table>
<tr>
<td>Enter a little something to search for</td>
<td><input type="text" name="search_box"></td>
<td><input type="submit" name="submit" value="Search">
</tr>
</table>
</form>
</body>
</html>Code: Select all
<?php
$search = $_POST['search'];
mysql_connect('localhost','username','password');
mysql_select_db('my_books_db');
$sql = "SELECT * from book_collection where (Book_Title LIKE '%".$search."%') or (Author LIKE '%".$search."%') or (Date LIKE '%".$search."%')";
$result = mysql_query($sql) or die(MySQL_Error());
$num_rows = mysql_num_rows($result);
if($num_rows == '0')
{
echo 'No Matches Found!';
exit;
}
$Book_Title = array();
$Author = array();
$Date = array();
$Unique_ID = array();
while($row = mysql_fetch_assoc($result))
{
$Book_Title[] = $row['Book_Title'];
$Author[] = $row['Author'];
$Date[] = $row['Date'];
$Unique_ID[] = $row['unique_id'];
}
echo '<h3>Found '.$num_rows.' matches!';
echo '<table><tr><td>Book Title</td><td>Author</td><td>Date Published</td><td>View Book</td></tr>
for($i=0; $i<count($Book_Title); $i++)
{
echo '<tr><td>'.$Book_Title[$i].'</td><td>'.$Author.'</td><td>'.$Date.'</td><td><a href="view.php?id='.$Unique_ID.'">View Book</a></td></tr>';
}
echo '</table>';
?>Code: Select all
$ID = $_GET['id'];
mysql_connect('localhost','username','password');
mysql_select_db('my_books_db');
$sql = "SELECT * from book_collection where unique_id = '".$ID."'";
$result = mysql_query($sql);
echo '<h3>Book Information</h3>';
$Book_Title = array();
$Author = array();
$Date = array();
while($row = mysql_fetch_assoc($result))
{
$Book_Title[] = $row['Book_Title'];
$Author[] = $row['Author'];
$Date[] = $row['Date'];
}
echo '<table><tr><td>Book Title</td><td>Author</td><td>Date Published</td></tr>
for($i=0; $i<count($Book_Title); $i++)
{
echo '<tr><td>'.$Book_Title[$i].'</td><td>'.$Author.'</td><td>'.$Date.'</td></tr>';
}
echo '</table>';
?>Hope this helps.
Thanks, this is really helpful but unfortunately I have come across errors that I cannot solve.
The add_book code was fine if removed the word "exit" from the code but then ti worked.
But on Search Books, I come across this message
Parse error: parse error, unexpected '>' in /home/yesyes/public_html/book/search.php on line 33
Could anyone be so kind as to help me?
The add_book code was fine if removed the word "exit" from the code but then ti worked.
But on Search Books, I come across this message
Parse error: parse error, unexpected '>' in /home/yesyes/public_html/book/search.php on line 33
Could anyone be so kind as to help me?