Tonight at 2 AM i decided to start learning PHP haha. So i set out and decided to work on a project that i could actually use i built this to catalogue my dvd collection and compare the owners of the dvds to see who has what later i plan on implementing a "borrowing" type thing where i can track and see who borrowed one of my dvds and how long they have had it.
The question i have since i am new to the whole php scene is to look over my code and see if i have made any monotonous coding mistakes i.e. doing something the long way when it could be done in less code.
I added the line:
header("Location: dvds.php");
because when i pushed refresh it would make duplicate entries into my database.
<xmp>
<link href="xampp.css" rel="stylesheet" type="text/css">
<?
// includes
include("conf.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to
connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
if($title!="")
{
if($genre=="")$genre="NULL";
mysql_query("INSERT INTO dvds (title,genre,owner) VALUES('$title','$genre','$owner');");
header("Location: dvds.php");
}
if($action=="del")
{
mysql_query("DELETE FROM dvds WHERE id=$id;");
}
// generate and execute query
$query = "SELECT id, title, genre, owner FROM dvds ORDER BY title ASC";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
?>
<h1>DVD Collection</h1>
<table border=0 cellpadding=0 cellspacing=0>
<tr bgcolor=#f87820>
<td></td>
<td class=tabhead>
Title</td>
<td class=tabhead>
Genre</td>
<td class=tabhead>
Owner</td>
<td class=tabhead>
Command</td>
<td></td>
</tr>
<?
// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
?>
<tr valign=center>
<td class=tabval></td>
<td class=tabval><? echo $row->title; ?></td>
<td class=tabval><? echo $row->genre; ?></td>
<td class=tabval><? echo $row->owner; ?></td>
<td class=tabval><a onclick="return confirm('Are you sure?');" href=dvds.php?action=del&id=<? echo $row->id;?>><span class=red>DELETE</span></td>
</tr>
<?
}
}
// if no records present
// display message
else
{
?>
No DVDs present in catologue.
<?
}
?>
</tr>
<tr valign=bottom>
<td bgcolor=#fb7922 colspan=6></td>
</tr>
</table>
<h2>ADD DVD</h2>
<form action="<? $PHP_SELF; ?>" method="get">
<table border=0 cellpadding=0 cellspacing=0>
<tr><td>Title:</td>
<td><input type=text size=30 name=title></td></tr>
<tr><td>Genre:</td>
<td><input type=text size=30 name=genre></td></tr>
<tr><td>Owner:</td>
<td><input type=text size=10 name=owner></td></tr>
<tr><td></td><td><input type=submit border=0 value="ADD DVD"></td></tr>
</form>
<?
// close database connection
mysql_close($connection);
?>
</xmp>