Sunday, July 6, 2008

Create Album with PHP and Database (2)

Files : Table 'album' , adminaddalbum.php

1. Table 'album' to store the albums created
Before uploading images, we have to create a table to record the album that the user create.



Then create an album.
The code are as follows.
if($user){
echo '<form id="form1" method="post" action="adminaddalbum.php">';
echo '<table width=480>';
echo '<tr>';
echo '<td align="center" colspan="2" class="side" width=480><b>Create an Album</b></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="side" width=150>ID</td>';
echo '<td class="sidetitle" width=330>';
echo $user;
echo '<input type="hidden" name="username" value="'.$user.'">';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="side" width=150>Album title</td>';
echo '<td class="sidetitle" width=330><input type="text" name="title"></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sidetitle" width=150><input type="submit" name="Submit" value="Submit"></td>';
echo '<td class="sidetitle" width=330><input type="reset" name="Reset" value="Reset"></td></tr>';
echo '</tr>';
echo '</table>';
echo '</form>';
}else{
echo '<font size=2>You need to log in and must be a member of NCKU ee97a.</font>';
}


I have to first address that in my own website, if the customer wanna create an album, he or she must log in first. Once they log in, the id will be recorded in the variable $user. So if they are sure to log in, the variable $user will not be NULL, and the code above will do the "if" part not the "else" part. The actual layout of the form in the code is shown in the picture below.



Press submit button and the album will be created. Once the submit button is pressed, it will trigger the "adminaddalbum.php." The code is shown below.

2. adminaddalbum.php
<?
$album=$_POST['title'];
$name=$_POST['username'];

$db_conn=mysql_connect("your server", "your id", "your pwd");
mysql_select_db("your database");

$query = "select * from album order by num";
$result=mysql_query($query);
$num_all=mysql_num_rows($result);

$q=mysql_fetch_array($result);
$big=$q[num];

for($i=1;$i<=$num_all;$i++){
$q=mysql_fetch_array($result);
$temp=$q[num];
if($temp>$big) $big=$temp;
}

$how_many_album=$big+1;

//將相簿紀錄到資料庫當中
$query = "INSERT INTO album (num, name, author) "."VALUES ('$how_many_album', '$album', '$name')";
mysql_query($query) or die('Error, query failed');

echo '<meta http-equiv=refresh content=0;url="adminpic.php?id='.$how_many_album.'&user='.$name.'&page=1">';
exit;
?>


The next time we will talk about uploading images.

No comments: