musings with LAMP part2

while i was thinking about the way the php script would behave, i decided to add another column to the current existing database so it’ll be easier to delete data when i have to or just for the sake of being able to better remove content directly using the form. i had to run this command at the mysql prompt:

mysql> alter table characters add id int primary key auto_increment not null first;

it tells mysql to create a new column and name it “id” and is a primary key. it gets added, and incremented too, automatically everytime new data is entered to the table.

now, on to the form itself. the form calls some other small php scripts namely:

db_config.php – contains connection info

<?php
$dbhost = ‘darnassus.myhomelab.net’;
$dbuser = ‘uniqueusername’;
$dbpass = ‘ultrahardtoguesspassw0rd’;
$dbname = ‘ordinary_db’;
?>

db_open.php – contains instruction to connect to the mysql db

<?php
$kon = mysql_connect($dbhost, $dbuser, $dbpass) or die (“is ur db messed up in the head?”);
mysql_select_db($dbname);
?>

db_close.php – contains instruction to terminate connection from mysql

<?php
mysql_close($kon);
?>

del.php – this contains insructions to delete data from the db

<?php
include(“db_stuff/db_config.php”);
include(“db_stuff/db_open.php”);
$id = $_GET[‘id’];
mysql_query(“DELETE FROM characters where id=’$id’ “);
include(“db_stuff/db_config.php”);
print “ENTRY DELETED”;
include(“form.php”);
?>

now comes the main php form:

<form   method=”post”>
<table width=”500″ border=”0″ cellpadding=”2″ cellspacing=”1″ >
<tr>
<td width=”50″>Name</td>
<td><input name=”name” type=”text”></td>
</tr>
<tr>
<td width=”50″>Class</td>
<td><textarea name=”class” cols=”50″ rows=”1″></textarea></td>
</tr>
<tr>
<td width=”10″>Age</td>
<td><textarea name=”age” cols=”10″ rows=”1″></textarea></td>
</tr>
<tr>
<td width=”1″>Sex(F/M)</td>
<td><textarea name=”sex” cols=”1″ rows=”1″></textarea></td>
</tr>
<tr>
<td width=”1″>Profession</td>
<td><textarea name=”profession” cols=”50″ rows=”1″></textarea></td>
</tr>
<tr><br>
<td align=”center”><input name=”save” type=”submit” value=”Submit”></td>
</tr>
</table>
</form>
<?php
/* enter data into db */
if(isset($_POST[‘save’]))
{
$name   = $_POST[‘name’];
$class   = $_POST[‘class’];
$age   = $_POST[‘age’];
$sex   = $_POST[‘sex’];
$profession = $_POST[‘profession’];
include ‘db_stuff/db_config.php’;
include ‘db_stuff/db_open.php’;
$query = ” INSERT INTO characters (name,class,age,sex,profession) “.
” VALUES (‘$name’, ‘$class’,’$age’,’$sex’,’$profession’)”;
mysql_query($query) or die(‘Error ,query failed’);
echo ” entry added”;
}
/* end data db entry */
/* view current data in db */
include ‘db_stuff/db_config.php’;
include ‘db_stuff/db_open.php’;
$query=(“SELECT * FROM characters”);
$db_content = mysql_query($query);
print “<table border=1>”;
print “<td>ID</td><td>NAME</td><td>CLASS</td><td>AGE</td><td>SEX</td><td>PROFESSION</td>”;
while ($row = mysql_fetch_array($db_content))
{
print “<tr>”;
print “<td>$row[id]</td><td>$row[name]</td><td>  $row[class] </td><td> $row[age] </td><td> $row[sex] </td><td> $row[profession]</td>”;
print “<td><a href=del.php?id=$row[id]>delete</a></td>”; #add a delete column, just to make data management a bit easier.
print “</tr>”;
}
echo “</table>”;
/* end data view */
include ‘db_stuff/db_close.php’;
?>

that’s it for now, next one’s clustering LAMP.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Code:

This site uses Akismet to reduce spam. Learn how your comment data is processed.