We do{make} the elementary similarity of Internet - shop.
Though, certainly, will name more correctly it " system of the order of the goods on a site ".
But, looking at sites of numerous Russian firms which work
By such principle (www.dostavka.ru, www.depo.ru, www.wstore.ru),
We too with pathos shall name a set of unpretentious scripts "INTERNET - SHOP".
That for this purpose it is required: any web-server supporting job with cgi.
Quite will approach apache, eserv, pws. Established php versions 3.0.kh - 4.0.kh,
The established server of databases mysql (though, has no value which-
But this example is written recognizing that established mysql).
The platform has no special value. It will work both on win32, and on unix.
That we want to receive: that users, travelling on our site,
Could reserve their interesting goods and that this order has fallen to us into hands.
And further usual job of managers already begins.
Let's define{determine} problems{tasks}:
1) Each user at call on a site should receive the unique identifier.
We should know who exactly reserves the goods?
2) For each user at us should virtual "basket" is stored{kept},
Where it will be taken into account that is reserved and in what kol-ve.
3) Eventually, the order is sent on e-mail menezhderu (with the information who, when,
As how much has reserved) and to the user made the order.
We begin.
We do{make} tables for a database of the goods. Here the clause - an ideal case is necessary,
If at you each goods have the unique code.
Duly and should be.
At you not so? Make a field auto_increment - strongly facilitate a life.
Voobhhem, we shall be schiatat` that with base of the goods at you all ok.
For definiteness, we shall consider the most simple case - at you only one table with
About such structure:
create table main (
id varchar (20) not null,
manufacture varchar (255),
name varchar (255) not null,
price double (8,2) default ' 0.00 ' not null,
primary key (id)
);
insert into main values (' 1002575 ',' manuf_1 ',' tovar 1 ',' 150.21 ');
insert into main values (' 125575 ',' manuf_1 ',' tovar 2 ',' 254.02 ');
insert into main values (' 1255757 ',' manuf_1 ',' tovar 3 ',' 456.24 ');
insert into main values (' 4556852 ',' manuf_2 ',' tovar 1 ',' 75.24 ');
insert into main values (' 15957725 ',' manuf_2 ',' tovar 2 ',' 5721.01 ');
insert into main values (' 5732548 ',' manuf_2 ',' tovar 3 ',' 685.49 ');
But any difference basic no, certainly.
1) We do{make} a file in which at us to each user the identifier is appropriated{given}:
File cookie.php3
<?
if (isset ($cookie_tmp))
{$uid = $cookie_tmp;}
else {
srand (time ());
$uid = md5 (uniqid (" "));
setcookie ("cookie_tmp", $uid, time () +604800, "/");}
?>
It we establish{install} a file cookie with a name cookie_tmp, value $uid
And time of a life 1 week since the present{true} moment.
This file is inserted UP TO a conclusion of ANY tags html on all (or on what it is necessary)
Pages of our site:
<? php require ("cookie.php3");?>
If you are not seduced with use of files cookie it is possible to drag
This most $uid on all site in a line of search or the latent fields of the form...
2) We do{make} the table for storage of "basket" of orders:
create table shop_tmp (
id int (20) default ' 0 ' not null auto_increment,
uid varchar (50) default ' 0 ' not null,
activated int (14) default ' 0 ' not null,
goods_id int (20) default ' 0 ' not null,
goods text not null,
price double (8,2) default ' 0.00 ' not null,
quantity smallint (6) default ' 0 ' not null,
summa double (8,2) default ' 0.00 ' not null,
primary key (id)
);
The field activated is useful for check when recording in the table has been added
And removals{distances} if, for example, it has taken place more weeks ago.
2.1) We do{make} a file which adds in this our table the information:
File add.php3
<html> <body>
<div align = "center"> Addition of the goods in a basket </div>
<table border=1 align = "center">
<tr>
<td> the Manufacturer </td>
<td> the Name of the goods </td>
<td> the Price </td>
</tr>
<? php
require ("connect.php3");
$result = mysql ($dbname, " select * from main where id = ' $ id ' ");
$manufacture = mysql_result ($result, 0, "manufacture");
$name = mysql_result ($result, 0, "name");
$price = mysql_result ($result, 0, "price");
?>
<tr>
<td> <? echo "$manufacture";?> </td>
<td> <? echo "$name";?> </td>
<td> <? echo "$price";?> roubles </td>
</tr> </table>
<form action = "basket.php3" method = "post">
<div align = "center">
<p> Quantity{Amount}
<input type = "text" name = "quantity" value = "1" maxlength = "4"> pieces.
<input type = "submit" name = "add" value = " To add in a basket ">
</p>
</div>
<input type = "hidden" name = "goods_id" value = " <? echo " $id ";?> ">
<input type = "hidden" name = "goods" value = " <? echo " $manufacture $name ";?> ">
<input type = "hidden" name = "price" value = " <? echo " $price ";?> ">
<input type = "hidden" name = "uid" value = " <? echo " $uid ";?> ">
</form> </body> </html>
2.2) In view of it, now our file which deduces the information
From a database looks approximately so:
File show.php3
<? php require ("cookie.php3");?>
<html>
<head>
<script language = "javascript">
function basket (url) {bag = window.open (url, ' basket ',' toolbar=no, location=no, status=no,
menubar=no, scrollbars=yes, width=600, height=300, resizable=yes'); bag.focus ();}
</script>
</head>
<body>
<table border=1>
<? php
require ("connect.php3");
$sql = " select * from main order by manufacture, name ";
$result = mysql ($dbname, $sql);
$num = mysql_num_rows ($result);
$i = 0;
while ($i <$num) {
$id=mysql_result ($result, $i, "id");
$manuf=mysql_result ($result, $i, "manufacture");
$name=mysql_result ($result, $i, "name");
$price=mysql_result ($result, $i, "price");
echo " <tr> n
<td> $manuf </td> n
<td> $name </tdn>
<td> $price </td> n
<td> <a href = " javascript:basket (' add.php3? uid = $ uid*id = $ id ') "> the Order </a> </td> n
</tr> ";
$i ++;
}
?>
</table>
</body> </html>
2.3) We do{make} a file basket.php3 - he at us responsible{crucial} for addition of recordings
In a basket and viewing of this basket:
File basket.php3
<? require ("cookie.php3");?>
<html>
<head>
<body>
<div align = "center"> <p> Contents of your basket </p> </div>
<table border=1 align = "center">
<tr>
<td> the Name of the goods </td>
<td> the Price </td>
<td> Kol-in </td>
<td> the Sum </td>
<td> To remove </td>
</tr>
<? php
$table_4 = "shop_tmp";
echo " <form method ='post ' action = ' $ php_self '> ";
$summa = $price * $ quantity;
$activated = time ();
Addition of the goods in a basket
function add () {
global $uid, $activated, $goods, $price, $quantity, $summa, $goods_id, $table_4;
require ("connect.php3");
$result = mysql ($dbname, " select * from $table_4 where (goods_id = ' $ goods_id ') and (uid = ' $ uid ') ");
$num = mysql_num_rows ($result);
if ($num> 0) // means such goods in a basket already are and it is necessary to increase kol-in and the sum
{
$result_1=mysql ($dbname, " select * from $table_4 where (goods_id = ' $ goods_id ') and (uid = ' $ uid ') ");
$quantity_table=mysql_result ($result_1,0, "quantity");
$price=mysql_result ($result_1,0, "price");
$quantity_total = $ quantity_table + $ quantity;
$summa_total = $ quantity_total * $ price;
$sql = " update $table_4 set quantity = $ quantity_total, summa = $ summa_total
where goods_id = ' $ goods_id ' and uid = ' $ uid ' ";
mysql_db_query ($dbname, $sql);
}
else // such goods for this user still no - are simply added
{
$sql_insert = " insert into $table_4 (id, uid, activated, goods, price, quantity, summa, goods_id)
values (", ' $ uid ', $activated, ' $goods', $ price, $quantity, $summa, '$goods_id') ";
mysql ($dbname, $sql_insert);
}
view (); // have added and vyzyvvaem function for viewing a basket
}
function delete () {
global $id, $table_4;
require ("connect.php3");
$result = mysql ($dbname, " delete from $table_4 where (id = ' $ id ') ");
view ();
}
function delete_all () {
global $uid, $table_4;
require ("connect.php3");
$result = mysql ($dbname, " delete from $table_4 where (uid = ' $ uid ') ");
view ();
}
function view () {
global $uid, $activated, $table_4;
require ("connect.php3");
$result = mysql ($dbname, " select * from $table_4 where (uid = ' $ uid ') order by goods ");
$num = mysql_num_rows ($result);
if ($num == 0) {
echo "<tr>";
echo " <td colspan=5 align=center> <p> In your basket there are no goods! </td> </tr> ";
}
else {
$total=0.00;
$i=0;
while ($i <$num)
{
$goods=mysql_result ($result, $i, "goods");
$goods_id=mysql_result ($result, $i, "goods_id");
$quantity=mysql_result ($result, $i, "quantity");
$id=mysql_result ($result, $i, "id");
$price=mysql_result ($result, $i, "price");
$summa=mysql_result ($result, $i, "summa");
echo " <tr> n
<td> $goods </td> n
<td> $price </td> n
<td> $quantity pieces </td> n
<td> $summa </td> n
<td> <a href ='basket.php3? action=delete*id = $ id*uid = $ uid '> To remove </a> </td> n
</tr> n ";
$total = $ summa + $ total;
$i ++;
}
}
echo " <tr>
<td colspan=5 align=center> <p> the Total sum of the order - $total roubles </td> </tr> ";
}
if ($add)
{add ();}
elseif ($action == "delete") {delete ();}
elseif ($empty) {delete_all ();}
else {view ();}
?>
</table> </form>
<div align = "center"> <br>
<form method ='post ' action ='order.php3 '>
<input type = "hidden" name = "uid" value = " <? echo " $uid ";?> ">
<input type = "submit" name = "order" value = " To issue the order ">
</form>
<? echo " <form method ='post ' action = ' $ php_self '> ";?>
<input type ='hidden ' name = "uid" value = " <? echo " $uid ";?> ">
<input type = "submit" name = "empty" value = " To clear a basket ">
</form>
</div>
</body>
</html>
================================================================
3) Now it are necessary to generate the text of the letter:
File order.php3
<html>
<head>
<body>
<form name ='order ' action ='end.php3 ' method ='post '>
Your name and surname <input type = "text" name = "name" size = "40" maxlength = "60"> <br>
e-mail <input type = "text" name = "email" size = "40" maxlength = "40"> <br>
<center> the following positions are Now reserved: </center>
<table border=1 align = "center">
<tr>
<td> the Article of the goods </td>
<td> the Name of the goods </td>
<td> the Price </td>
<td> Kol-in </td>
<td> the Sum </td>
</tr>
<? php
{
$table_4 = "shop_tmp";
require ("connect.php3");
$result = mysql ($dbname, " select * from $table_4 where (uid = ' $ uid ') ");
$num = mysql_numrows ($result);
if ($num == 0) {
echo "<tr>";
echo " <td colspan=5> In your basket there are no goods! </td> </tr> ";
}
else {
$body = " ";
$total=0;
$i=0;
while ($i <$num)
{
$goods=mysql_result ($result, $i, "goods");
$price=mysql_result ($result, $i, "price");
$goods_id=mysql_result ($result, $i, "goods_id");
$quantity=mysql_result ($result, $i, "quantity");
$id=mysql_result ($result, $i, "id");
$summa=mysql_result ($result, $i, "summa");
$total = $ total + $ summa;
$body_1 = " <tr> <td> $goods_id </td> <td> $goods </td> <td> $price roubles </td>
<td> $quantity pieces </td> <td> $summa roubles </td> </tr> ";
echo $body_1;
$body. = $body_1;
$i ++;
}
}
$body = htmlspecialchars ($body);
echo " <tr>
<td colspan=5 align=center> the Total sum of the order - $total roubles </td> </tr>
<input type=hidden name ='total ' value = ' $ total '>
<input type=hidden name ='body ' value = " $ body ">
<input type=hidden name ='uid ' value = ' $ uid '> ";
}
?>
</table>
<br>
<div align = "center"> <input type = "submit" name = "submit" value = " To finish registration of the order "> </div>
</form>
</body> </html>
4).. And to send letters (to the client and the manager)
Yes, the necessary comment. I on the first page of the site
Specified what is it simply ADVICE{COUNCILS}, and in any way ready decisions.
Also that something else is necessary to esteem before to undertake a spelling.
In this case, I hint clause{article} " receptions of safe programming on php ".
See listing the following file and read that,
About what I speak - at once it becomes clear where a hole...
File end.php3
<?
if ($name == " ") {
echo " you have not specified the name! ";
exit;}
elseif ($email == " ") {
echo " you have not specified email address! ";
exit;}
else
{
// $message and $header is for sending to the manager
// $message_1 and $header_1 is for sending to the client
$message = " <html> <body> <font size = ' + 1 '> <p align ='justify '>
The new order from a site www.nnn.net. <br>
Data on the buyer: <br> the Name - $name <br>
The company - $company <br> Phone - $phone <br>
e-mail - $email <br> <br>
The maintenance{contents} of the order: <br> <br>
<table border=1> $body
<tr> <td colspan=5> <b> All for the sum - $total roubles </b> </td> </tr>
</table> </font> </p> </body> </html> ";
$header = " to: info@nnn.netnfrom: mail-robot@nnn.netnreply-to: $emailnx-mailer:
php auto-mailerncontent-type: text/html; nmime-version: 1.0n ";
$message_1 = " <html> <body> <font size = ' + 1 '> <p align ='justify '>
Dear $name! n On a site of firm ' nnn ' (www.nnn.net) you had been made the order:
<br> <br>
<table border=1> $body
<tr> <td colspan=5> <b> All for the sum - $total roubles </b> </td> </tr>
</table> <br>
Our managers will contact you in the near future for specification of details.
Thanks.
</font> </p> </body> </html> ";
$header_1 = " to: $emailnfrom: mail-robot@nnn.netnreply-to: info@nnn.netnx-mailer:
php auto-mailerncontent-type: text/html; nmime-version: 1.0n ";
@mail ("info@nnn.net", " order from nnn web site ", $message, $header);
@mail ($email, " order from nnn web site ", $message_1, $header_1);
echo " <h4> Dear <b> $name </b>! Your order is accepted.
The manager of our firm will contact you in the near future for specification of details. <br>
Thanks for visiting of our site! </h1> n ";
}
?>
Well? Certainly, danger that this script can palm off
And he them will send ANY data easy..
Output{Exit}: to not send from the previous page contents of the order,
And to form it it is direct in this script.
In general, it is necessary to try to drag behind itself as small as possible the data.
Also, check of NEVER will hurt from what page there has come search.
5) All. We have received all that wanted. Now it are necessary to make function only
Which will clean the table shop_tmp from old orders. But it is already @.
6) Before all operations with a database the file is caused
For connection with a database of such kind:
File connect.php3
<? php
{
$dbname = "test";
$host = "localhost";
$port = "3306";
$user = "root";
$password = " ";
mysql_connect ($host, $user, $password);
mysql_select_db ($dbname) or die (" unable to select database ");
}
?>
ps. Once again I repeat - all that I here am written has checked up-
IT works. If at you not so - you something is simple have incorrectly made...
pps. At the request of workers, dopisan item{point} 4.
Also the item{point} three is a little corrected..

|