02-02-2010, 05:36 PM
Here you go, there are 3 files;
coin.php, connect.php and list.php
Read the files and try to understand what they are doing...
If you have any question just ask....
also you can read more here;
http://www.supportforums.net/showthread.php?tid=1609
http://www.supportforums.net/showthread.php?tid=4506
coin.php, connect.php and list.php
files (Click to View)
connect.php
list.php
coin.php
Code:
<?php
$db = array(
"host" => "localhost", # host
"user" => "root", # login username
"pass" => "", # login password
"base" => "tut" # database name
);
if(!$connect = @mysql_connect($db['host'], $db['user'], $db['pass'])) {
# echo mysql_error(), "<br />"; # mysql error
die("Not Connected!");
}elseif(!$database = @mysql_select_db($db['base'])) {
# echo mysql_error(), "<br />"; # mysql error
die("Database {$db['base']} can not be found!");
}
?>
list.php
Code:
<?php
$sql = "SELECT * FROM `coin_info`;";
$sql = mysql_real_escape_string(trim($sql));
if(!$query = mysql_query($sql)) {
# echo mysql_error(), "<br />";
die("SQL Query failed!");
}
echo <<<TABLE
<table border="1">
<tr>
<td> </td><td>ID</td><td>DATE</td><td>PRICE</td><td>CONDITION</td>
<td>YEAR</td><td>SILVER</td><td>GOLD</td><td>WEIGHT</td>
</tr>
TABLE;
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td><a href=\"t.php?edit={$row['id']}\">Edit</a> | <a href=\"t.php?del={$row['id']}\">Delete</a>";
echo "<td>{$row['id']}</td><td>{$row['date']}</td><td>{$row['price']}</td><td>{$row['condition']}</td>";
echo "<td>{$row['year']}</td><td>{$row['silver']}</td><td>{$row['gold']}</td><td>{$row['weight']}</td>";
echo "</tr>";
}
mysql_free_result($query);
echo <<<TABLE
</table><hr /><hr />
TABLE;
?>
coin.php
Code:
<?php
require_once "connect.php";
$id = NULL;
$submit = "add";
if(isset($_POST['add'])) {
$id = NULL;
$date = trim($_POST['date']);
$price = trim($_POST['price']);
$condition = trim($_POST['condition']);
$year = trim($_POST['year']);
$silver = trim($_POST['silver']);
$gold = trim($_POST['gold']);
$weight = trim($_POST['weight']);
$sql = "INSERT INTO `coin_info` (";
$sql .= "`id` ,`date` ,`price` ,`condition` ,`year` ,`silver` ,`gold` ,`weight`";
$sql .= ")VALUES (";
$sql .= "NULL , '{$date}', '{$price}', '{$condition}', '{$year}', '{$silver}', '{$gold}', '{$weight}');";
if(!@mysql_query($sql)) {
echo mysql_error(), "<br />"; # mysql error
die("SQL Query failed!");
}else{
echo "Coin added!", "<br />";
}
}elseif(isset($_GET['edit'])) {
$id = $_GET['edit'];
$submit = "edit";
$sql = "SELECT * FROM `coin_info` WHERE `id` = '{$id}';";
$sql = trim($sql);
if(!$query = @mysql_query($sql)) {
# echo mysql_error(), "<br />";
die("Unkown ID");
}else{
echo "EDIT ID : {$id}!", "<br />";
}
mysql_free_result($query);
}elseif(isset($_POST['edit'])) {
$edit_id = (int) $_POST['id'];
$sql = "UPDATE `coin_info` SET `date` = '{$_POST['date']}',";
$sql .= "`price` = '{$_POST['price']}',`condition` = '{$_POST['condition']}',`year` = '{$_POST['year']}',";
$sql .= "`silver` = '{$_POST['silver']}',`gold` = '{$_POST['gold']}',`weight` = '{$_POST['weight']}' ";
$sql .= "WHERE `id` = '{$edit_id}' LIMIT 1 ;";
if(!@mysql_query($sql)) {
echo mysql_error(), "<br />"; # mysql error
die("Edit failed!");
}else{
echo "Edited {$edit_id}!", "<br />";
}
}elseif(isset($_GET['del'])) {
$del_id = trim($_GET['del']);
$sql = "DELETE FROM `coin_info` WHERE `id` = '{$del_id}' LIMIT 1";
if(!@mysql_query($sql)) {
# echo mysql_error(), "<br />";
die("Could not delete ID {$del_id}");
}else{
echo "Deleted ID ", $id, "<br />";
}
}
echo <<<HTML
<form action="coin.php" method="post">
<input type="text" name="id" value="{$id}" /><br />
<input type="text" name="date" value="01.01.1257" /><br />
<input type="text" name="price" value="13,001 $" /><br />
<input type="text" name="condition" value="1" /><br />
<input type="text" name="year" value="1567" /><br />
<input type="text" name="silver" value="23" /><br />
<input type="text" name="gold" value="100" /><br />
<input type="text" name="weight" value="5" /><br />
<input type="submit" name="{$submit}" value="Submit" />
</form>
HTML;
include "list.php";
mysql_close($connect);
Read the files and try to understand what they are doing...
If you have any question just ask....
also you can read more here;
http://www.supportforums.net/showthread.php?tid=1609
http://www.supportforums.net/showthread.php?tid=4506