Computing.Net > Forums > Web Development > PHP Question about MYSQL interactio

PHP Question about MYSQL interactio

Reply to Message Icon

Original Message
Name: sputhuff
Date: February 12, 2005 at 04:27:02 Pacific
Subject: PHP Question about MYSQL interactio
OS: Windows XP
CPU/Ram: 512
Comment:

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>


Report Offensive Message For Removal


Response Number 1
Name: juvi
Date: February 12, 2005 at 20:46:07 Pacific
Reply: (edit)

I took a quick glance, but the first 2 things that popped into mind are:

1.) If headers have been sent to the browser, I'm pretty sure you cant use header("Location: ");, Or alteast as far as I remember.

2.) instead of typing out <? echo $var; ?> every time, try using <?=$var?>. It's alot shorter. You can also use it more complexly <?="It\'s currently ".$time." on ".date("d-m-Y")?>

Correspond with me via e-mail and I can take a more generous look through it.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP Question about MYSQL interactio

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge