[PHP]Tutorial

In jos

[PHP]Tutorial

Mesaj Scris de Max la data de Dum Sept 07, 2008 7:26 pm

Deci sa incepem:
prima si prima data trebuie sa creem o baza de date si apoi sa facem un query SQL care aratA cam asha:

CREATE TABLE intrari (
id INT NOT NULL AUTO_INCREMENT,
nume VARCHAR(60) NOT NULL,
prenume VARCHAR(60) NOT NULL,
varsta VARCHAR(10) NOT NULL,
email VARCHAR(100) NOT NULL,
comentariu VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) TYPE = MYISAM;


Apoi in folderul radacina al serverului dumneavoastra faceti un folder numit formular.
Acum creem config.php cu informatiile necesare conexiunii la baza de dtae:

<?php
session_start();
set_time_limit(0);
error_reporting(E_ALL);
// Informatii baza de date
$AdresaBazaDate = "localhost";
$UtilizatorBazaDate = "root";
$Paro***zaDate = "parola_baza";
$NumeBazaDate = "formular";
$conexiune = mysql_connect($AdresaBazaDate,$UtilizatorBazaDate,$Paro***zaDate)
or die("Nu ma pot conecta la MySQL!"smiley;
mysql_select_db($NumeBazaDate,$conexiune) or die("Nu gasesc baza de date!"smiley;
function addentities($data){
if(trim($data) != ''smiley{
$data = htmlentities($data, ENT_QUOTES);
return str_replace('\\', '\', $data);
} else return $data;
} // End addentities() --------------
?>


Acum creeati index.php si introduce-ti urmatoru cod:

<?php
require_once('config.php'smiley;
if(!isset($_SESSION['nume'])) $_SESSION['nume'] = '';
if(!isset($_SESSION['prenume'])) $_SESSION['prenume'] = '';
if(!isset($_SESSION['varsta'])) $_SESSION['varsta'] = '';
if(!isset($_SESSION['email'])) $_SESSION['email'] = '';
if(!isset($_SESSION['comentariu'])) $_SESSION['comentariu'] = '';
echo '<table width="310" border="0" cellpadding="0" cellspacing="0">
<form name="formular" action="validare.php" method="post">
<tr>
<td height="36" colspan="3" valign="top"><h1>Formular</h1>Comentariul nu trebuie sa
fie mai lung de 255 caractere.</td>
<td width="1"></td>
</tr>
<tr>
<td width="80" height="19" valign="top"> </td>
<td width="15" rowspan="10" valign="top"> </td>
<td width="214" valign="top"> </td>
<td></td>
</tr>
<tr>
<td height="22" align="right" valign="top">Nume:</td>
<td valign="top">
<input type="text" name="nume" value="'.$_SESSION['nume'].'"> </td>
<td></td>
</tr>
<tr>
<td height="7"></td>
<td></td>
<td></td>
</tr>
<tr>
<td height="22" align="right" valign="top">Prenume:</td>
<td valign="top"><input type="text" name="prenume"
value="'.$_SESSION['prenume'].'"></td>
<td></td>
</tr>
<tr>
<td height="9"></td>
<td></td>
<td></td>
</tr>
<tr>
<td height="22" align="right" valign="top">Varsta:</td>
<td valign="top"><input type="text" size="3" maxLength="3" name="varsta"
value="'.$_SESSION['varsta'].'"> ani</td>
<td></td>
</tr>
<tr>
<td height="10"></td>
<td></td>
<td></td>
</tr>
<tr>
<td height="22" align="right" valign="top">Email:</td>
<td valign="top"><input type="text" name="email"
value="'.$_SESSION['email'].'"></td>
<td></td>
</tr>
<tr>
<td height="9"></td>
<td></td>
<td></td>
</tr>
<tr>
<td height="19" align="right" valign="top">Comentariu:</td>
<td rowspan="2" valign="top"><textarea name="comentariu" cols="30" rows="5"
value="'.$_SESSION['comentariu'].'">'.$_SESSION['comentariu'].'</textarea></td>
<td></td>
</tr>
<tr>
<td colspan="2" rowspan="3" valign="top"> </td>
<td height="83"></td>
</tr>
<tr>
<td height="17" valign="top"> </td>
<td></td>
</tr>
<tr>
<td height="24" valign="top"><input name="Trimite" type="submit" id="Trimite"
value="Trimite">
<input name="Reseteaza" type="reset" id="Reseteaza" value="Reseteaza"> </td>
<td></td>
</tr>
</form>
</table>';
?>


Acum creeati validare.php si introduceti urmatorul cod :

<?php
require_once('config.php'smiley;
$_SESSION['nume'] = $_POST['nume'];
$_SESSION['prenume'] = $_POST['prenume'];
$_SESSION['varsta'] = $_POST['varsta'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['comentariu'] = $_POST['comentariu'];
echo 'Nume: '.$_SESSION['nume'].'<br>
Prenume: '.$_SESSION['prenume'].'<br>
Varsta: '.$_SESSION['varsta'].'<br>
Email: '.$_SESSION['email'].'<br>
Comentariu: '.$_SESSION['comentariu'].'<br><br>
Daca datele sunt corecte, apasati <a href="prelucrare.php">aici</a> pentru a le valida
<br> si a le introduce in baza de date.';
?>


Apoi creeati un nou fisier cu numele prelucrare.php, si introduceti codul de mai jos:

<?php
require_once('config.php'smiley;
if(($_SESSION['nume'] == ""smiley || ($_SESSION['prenume'] == ""smiley || ($_SESSION['varsta'] ==
""smiley || (!is_numeric($_SESSION['varsta'])) || ($_SESSION['email'] == ""smiley ||
($_SESSION['comentariu'] == ""smiley || (strlen($_SESSION['comentariu']) > 255) )
{
echo 'Nu ai introdus date in formular sau cele introduse nu sunt corecte. <br>
Apasa <a href="index.php">aici</a> pentru a te intoarce la pagina anterioara.';
} else {
echo 'Va multumim. <br>
Datele au fost introduse cu succes in baza de date. <br>
Pentru vizualizare apasati <a href="vizualizare.php">aici</a>.';
$cerereSQL = "INSERT INTO `intrari` (`nume`, `prenume`, `varsta`, `email`,
`comentariu`)
VALUES ('".$_SESSION['nume']."', '".$_SESSION['prenume']."',
'".$_SESSION['varsta']."', '".$_SESSION['email']."', '".$_SESSION['comentariu']."'smiley;";
mysql_query($cerereSQL);
$_SESSION['nume'] = '';
$_SESSION['prenume'] = '';
$_SESSION['varsta'] = '';
$_SESSION['email'] = '';
$_SESSION['comentariu'] = '';
}
?>


Acum inca un fisier cu numeel vizualizare.php, si introduceti urmatorul cod:

<?php
require_once('config.php'smiley;
$cerereSQL = 'SELECT * FROM intrari';
$rezultat = mysql_query($cerereSQL);
while($rand = mysql_fetch_array($rezultat)) {
echo '<b>Nume:</b> '.$rand['nume'].' <br>
<b>Prenume:</b> '.$rand['prenume'].' <br>
<b>Varsta:</b> '.$rand['varsta'].' ani <br>
<b>Email:</b> '.$rand['email'].' <br>
<b>Comentariu:</b> '.$rand['comentariu'].' <br><br>';
}
?>

Max
Moderator
Moderator

Numarul mesajelor : 53
Varsta : 31
Localitate : Confidential
Stare de spirit : Rakiu
Data de inscriere : 23/08/2008

Vezi profilul utilizatorului

Sus In jos

Sus


 
Permisiunile acestui forum:
Nu puteti raspunde la subiectele acestui forum