Ti cosiglio di usare notepad ++ per visualizzare meglio
<?php session_start(); ?>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
<title>Shop Online</title>
</head>
<body>
<div id="boss">
<div id="header">
<?php
//Sceglie le voci del menu da visualizzare in base allo stato della sessione
if($_SESSION['signin']!=1)
{
echo '<a href="ShopOnline.php?action=login">Login</a>';
echo '<a href="ShopOnline.php?action=registrazione">Registr azione</a>';
}
else
{
echo '<a href="ShopOnline.php?action=logout">Logout</a>';
echo '<a href="ShopOnline.php?action=shop">Shop</a>';
echo '<a href="ShopOnline.php?action=carrello">Carrello</a>';
echo '<a href="ShopOnline.php?action=ordini">Ordini</a>';
}
?>
</div>
<div id="logo">
<a id="logs" href="ShopOnline.php"><img src="imm/BS-SHOP-ONLINE-WEB-BANNER_970w-x-315h_OPT2.jpg"></img></a>
</div>
<div id='main'>
<?php
//connessione database
try{
$user = "root";
$pass = "";
$dbh = new PDO('mysql:host=localhost;dbname=quintaa_db', $user, $pass);
} catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
echo '<form id="mainf" action="" method="post">';
//tentativo di login
if($_POST['invio']=='login')
{
if($_SESSION['signin']!=1)
{
$stmt = $dbh->prepare("SELECT * FROM utentishop WHERE username='".$_POST['user']."' AND password=MD5('".$_POST['pass']."')");
$stmt->execute();
$count = $stmt->rowCount();
$row = $stmt->fetch();
//succceso login
if($count>0)
{
echo " <br><h3>Login effettuato con successo</h3><br><br>";
echo '<input type="submit" name="ok" value="home" /><br>';
$_SESSION['signin']=1;//<------ qua viene asseganto alla sessione 1
$_SESSION['username']=$_POST['user'];
$_SESSION['admin']=$row['isadmin'];
}
else{
//fallimento login
echo " <br><h3>Errore login, username o password errati</h3><br>";
echo '<input type="submit" name="ok" value="home" /><br>';
}
}
}
//logout
else if($_POST['logout']=='si')
{
$_SESSION['signin']=0;
$_SESSION['username']=null;
$_SESSION['ll']=0;
$_SESSION['prodotto']=null;
$_SESSION['nprodotto']=null;
echo '<h3>Logout effettuato</h3>';
echo '<br><input type="submit" name="ok" value="home" /><br>';
}
//no logout
else if($_POST['logout']=='no')
{
header('Location: ShopOnline.php');
}
//tentativo di registrazione
else if($_POST['invio']=='registrati')
{
if($_SESSION['signin']!=1)
{
$stmt = $dbh->prepare("SELECT * FROM utentishop WHERE username='".$_POST['user']."'");
$stmt->execute();
$count = $stmt->rowCount();
//utente gia registrato
if ($count>0) {
echo "<h3>Utente gia registrato</h3>";
echo '<input type="submit" name="ok" value="<-indietro" /><br>';
} else {
//registrazione con successo
$stmt = $dbh->prepare("INSERT INTO utentishop (username,password,mail,isadmin) VALUES ('".$_POST['user']."',MD5('".$_POST['pass']."'),'".$_POST['mail']."','0')");
$stmt->execute();
$dbh = null;
echo "<h3>Account registrato con successo</h3>";
echo '<input type="submit" name="ok" value="home" /><br>';
}
$dbh = null;
}
}
else if($_GET['action']=="login")
{
//se la pagina è la pagina di login
if($_SESSION['signin']!=1)
{
echo '<br><br><br>Username:';
echo '<input type="text" name="user" value="username"></input><br><br>';
echo 'Password:';
echo '<input type="password" name="pass" value="password"></input><br><br>';
echo '<input id="button" type="submit" name="invio" value="login"></input><br><br><br>';
}
}
else if($_GET['action']=="registrazione")
{
//se la pagina è la pagina di registrazione
if($_SESSION['signin']!=1)
{
echo '<br><br><br>Username:';
echo '<input type="text" name="user" value="username"></input><br><br>';
echo 'Password:';
echo '<input type="password" name="pass" value="password"></input><br><br>';
echo 'E-mail:';
echo '<input type="text" name="mail" value="example@example.com"></input><br><br><br>';
echo '<input id="button" type="submit" name="invio" value="registrati"></input><br><br>';
}
}
else if($_GET['action']=="logout")
{
//se la pagina è la pagina di logout
if($_SESSION['signin']==1)
{
echo '<br><br><br>Vuoi veramente fare il logout?<br>';
echo '<br><input id="button" type="submit" name="logout" value="si"></input>';<----se sceglie si
echo ' <input id="button" type="submit" name="logout" value="no"></input><br><br>';<----se sceglie no
}
}
else if($_GET['action']=="shop" && $_SESSION['signin']==1)
{
//se la pagina è la pagina dello shop
//riempo combobox
if($_POST['combo']==null)
{
echo '<h2>Seleziona una categoria</h2>';
}
else
{
echo '<br><br><br>';
}
echo '<form action="" method="post">';
if($_SESSION['signin']==1)
{
$stmt = $dbh->prepare("SELECT * FROM categoria");
$stmt->execute();
echo '<select name="combo" onchange="submit()">';
if($_POST['combo']==null)
{
echo ' <option value="" selected></option>';
}
while ($row = $stmt->fetch()) {
if($row['id']==$_POST['combo'])
{
echo ' <option value="'.$row['id'].'" selected>'.$row['descrizione'].'</option>';
}
else
{
echo ' <option value="'.$row['id'].'">'.$row['descrizione'].'</option>';
}
}
}
echo '</select>';
echo '</form>';
//fine riempimento combobox
//crezione tabella
if($_POST['combo'])
{
$stmt = $dbh->prepare("SELECT P.id_prodotto, P.nome, P.costo, P.imm, C.descrizione FROM prodotto P INNER JOIN categoria C ON P.categoria=C.id where C.id='".$_POST['combo']."'");
echo '<table>';
echo '<tr>';
echo '<td>Nome</td>';
echo '<td>Costo</td>';
echo '<td>Categoria</td>';
echo '<td>Anteprima</td>';
echo '</tr>';
if ($stmt->execute(array($_GET['nome']))) {
while ($row = $stmt->fetch()) {
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['nome'].'</td>';
echo '<td>'.$row['costo'].'</td>';
echo '<td>'.$row['descrizione'].'</td>';
echo '<td><img height=70px width=70px src="'.$row['imm'].'"</img></td>';
echo '<td><input type="submit" name="" value="Add"></input></td>';
echo '<input type="hidden" name="aggiungi" value="'.$row['id_prodotto'].'"></input>';
echo '<input type="hidden" name="combo" value="'.$_POST['combo'].'"></input>';
echo '</tr>';
echo '</form>';
}
}
echo '</table>';
}
if($_POST['aggiungi'])
{
//se la pagina è la pagina di aggiunta prodotto
if($_SESSION['ll'])
{
$x=1;
while($_SESSION['prodotto'][$x])
{
if($_SESSION['prodotto'][$x]==$_POST['aggiungi'])
{
$_SESSION['nprodotto'][$x]=$_SESSION['nprodotto'][$x]+1;
$d=3;
}
$x=$x+1;
}
if($d!=3)
{
$_SESSION['ll']=$_SESSION['ll']+1;
$_SESSION['prodotto'][$_SESSION['ll']]=$_POST['aggiungi'];
$_SESSION['nprodotto'][$_SESSION['ll']]=1;
}
$d=0;
}
else
{
$_SESSION['ll']=1;
$_SESSION['prodotto'][$_SESSION['ll']]=$_POST['aggiungi'];
$_SESSION['nprodotto'][$_SESSION['ll']]=1;
}
}
}
if($_GET['action']=='carrello'&&$_SESSION['signin']==1)
{
//se la pagina è la pagina del carrello
$x=1;
echo '<br><table id="carrello" >';
if($_SESSION['prodotto']==null||$_SESSION['nprodotto'][1]==null)
{
echo'<h3>0 prodotti presenti nel carrello</h3>';
}
else if($_SESSION['prodotto'])
{
echo '<tr>';
echo '<td>Nome</td>';
echo '<td>N prodotto</td>';
echo '</tr>';
}
while($_SESSION['prodotto'][$x])
{
echo '<form action="" method="post">';
echo '<tr>';
$stmt = $dbh->prepare("SELECT P.nome FROM prodotto P where P.id_prodotto='".$_SESSION['prodotto'][$x]."'");
$stmt->execute();
$row = $stmt->fetch();
echo '<td>'.$row['nome'].'</td>';
echo '<td>'.$_SESSION['nprodotto'][$x].'</td>';
echo '<td><input type="submit" name="" value="+"></input></td>';
echo '<input type="hidden" name="aggiungi" value="'.$_SESSION['prodotto'][$x].'"></input>';
echo '</form>';
echo '<form action="" method="post">';
echo '<td><input type="submit" name="" value="-"></input></td>';
echo '<input type="hidden" name="rimuovi" value="'.$_SESSION['prodotto'][$x].'"></input>';
echo '</tr>';
echo '</form>';
$x=$x+1;
}
echo '</table>';
if($_POST['aggiungi'])
{
if($_SESSION['ll'])
{
$x=1;
while($_SESSION['prodotto'][$x])
{
if($_SESSION['prodotto'][$x]==$_POST['aggiungi'])
{
$_SESSION['nprodotto'][$x]=$_SESSION['nprodotto'][$x]+1;
header('Location: ShopOnline.php?action=carrello');
}
$x=$x+1;
}
}
}
else if($_POST['rimuovi'])
{
if($_SESSION['ll'])
{
$x=1;
while($_SESSION['prodotto'][$x])
{
if($_SESSION['prodotto'][$x]==$_POST['rimuovi'])
{
$_SESSION['nprodotto'][$x]=$_SESSION['nprodotto'][$x]-1;
if($_SESSION['nprodotto'][$x]==0)
{
$y=$x;
while($_SESSION['prodotto'][$y])
{
$_SESSION['nprodotto'][$y]=$_SESSION['nprodotto'][$y+1];
$_SESSION['prodotto'][$y]=$_SESSION['prodotto'][$y+1];
$y=$y+1;
}
$_SESSION['ll']=$_SESSION['ll']-1;
}
header('Location: ShopOnline.php?action=carrello');
}
$x=$x+1;
}
}
}
if($_POST['ordine']=="ordina")
{
$stmt = $dbh->prepare("INSERT INTO ordine(id_utente) VALUES ('".$_SESSION['username']."')");
$stmt->execute();
$lastId = $dbh->lastInsertId();
$x=1;
while($x<=$_SESSION['ll'])
{
$stmt = $dbh->prepare("INSERT INTO ordinieff(id_ordine,prodotto,nprodotto) VALUES('".$lastId."','".$_SESSION['prodotto'][$x]."','".$_SESSION['nprodotto'][$x]."')");
$stmt->execute();
$x=$x+1;
}
$_SESSION['prodotto']=null;
$_SESSION['nprodotto']=null;
$_SESSION['ll']=0;
header('Location: ShopOnline.php?action=ordok');
// insert one row
}
else if($_SESSION['ll']>0)
{
echo '<form action="" method="post"><br>Ordina: <input type="submit" name="ordine" value="ordina"></input></form>';
}
}
if($_GET['action']=='ordini'&&$_SESSION['signin']==1)
{
//se la pagina è la pagina degli ordini
$stmt = $dbh->prepare("SELECT id_ordine FROM ordine where id_utente='".$_SESSION['username']."'");
$stmt->execute();
$a=0;
while ($row = $stmt->fetch()) {
echo '<form action="" method="post">';
if($a==0)
{
echo '<h3 id="hordd">Ordine n: '.$row['id_ordine'].'</h3><br><table id="ordine">';
}
else
{
echo '<h3 id="hord">Ordine n: '.$row['id_ordine'].'</h3><br><table id="ordine">';
}
echo '<tr>';
echo '<td>Prodotto</td>';
echo '<td>n Prodotto</td>';
echo '</tr>';
try{
$user = "root";
$pass = "";
$dbhh = new PDO('mysql:host=localhost;dbname=quintaa_db', $user, $pass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$stmtt = $dbhh->prepare("SELECT prodotto,nprodotto FROM ordinieff where id_ordine='".$row['id_ordine']."'");
$stmtt->execute();
while ($roww = $stmtt->fetch()) {
echo '<tr>';
try{
$user = "root";
$pass = "";
$data = new PDO('mysql:host=localhost;dbname=quintaa_db', $user, $pass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$stmttt = $data->prepare("SELECT P.nome FROM prodotto P where P.id_prodotto='".$roww['prodotto']."'");
$stmttt->execute();
$rowww = $stmttt->fetch();
echo '<td>'.$rowww['nome'].'</td>';
echo '<td>'.$roww['nprodotto'].'</td>';
echo '</tr>';
}
echo '</table>';
if($a==0)
{
echo '<br><input id="rorr" name="" type="submit" value="Rimuovi ordine"></input><br>';
}
else
{
echo '<br><input id="ror" name="" type="submit" value="Rimuovi ordine"></input><br>';
}
echo '<input name="rordine" type="hidden" value="'.$row['id_ordine'].'"></input>';
echo '</form><br>';
$a=1;
}
if($_POST['rordine'])
{
$stmt = $dbh->prepare("DELETE FROM ordine where id_ordine='".$_POST['rordine']."'");
$stmt->execute();
header('Location: ShopOnline.php?action=ordini');
}
}
//se è stato effettuato con successo un ordine
if($_GET['action']=="ordok")
{
echo '<h1>Ordine effettuato</h1>';
}
echo'</form>';
?>
</div>
</div>
</body>