programmazione

PHP – My SQL – Login

PHP – My SQL – Login

DOWNLOAD

 

Creazione database

Entriamo in phpMyAdmin

In alto posizioniamoci su localhost> mydatabase

Selezioniamo linguetta “SQL”, nell’area di input copiamo i comandi SQL:

CREATE TABLE IF NOT EXISTS `members` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `username` varchar(65) NOT NULL DEFAULT '',
 `password` varchar(65) NOT NULL DEFAULT '',
 `email` varchar(255) NOT NULL DEFAULT '',
 `attivazione` varchar(255) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Clicchiamo sul bottone a destra ‘Esegui’

mysql-0008

Creazione Form Login

main_login.php

Invierà i dati a “checklogin.php”, in particolare:
– name=”myusername”
– name=”mypassword”

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Login</strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Check Login

checklogin.php

Interroga il database se
– login corretto -> login_success.php
– login errato -> checklogin.php restituisce un messaggio di errore

<?php

$host="localhost"; //lasciare com'è se utilizzate bluehost
$username="lucedigi_user"; 
$password="mypassword"; 
$db_name="lucedigi_testphp"; // database name
$tbl_name="members"; //Indicate la tabella presente nel database a cui si deve collegare 

// Connetti al server e seleziona il database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("DB non connesso");

// ricevo username e password dal form HTML
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

//Protezione da MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

// Invio una query di ricerca nella tabella, all'interno campi username e password
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

//
$count=mysql_num_rows($result);

//
if($count==1){

// Verifica dati e reindirizza a  "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Username o Password errati!";
}
?>

Login Success

login_success.php

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Correttamente Loggato!
</body>
</html>
By |MySQL, PHP|Commenti disabilitati su PHP – My SQL – Login

PHP – MySQL – Delete Records

PHP – MySQL – Delete Records

Statement:

DELETE FROM table_name
WHERE some_column = some_value

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Delete records START
mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");
// Delete records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin

BEFORE

mysql-0006

AFTER

mysql-0007

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Delete Records

PHP – MySQL – Update Records

PHP – MySQL – Update Records

Syntax:

UPDATE table_name
SET column1=value, column2=value2,…
WHERE some_column=some_value

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Update records START
// Set the Age of Peter Griffin to 36
mysqli_query($con,"UPDATE Persons SET Age=36
WHERE FirstName='Peter' AND LastName='Griffin'");
// Update records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin

BEFORE:

mysql-0004

AFTER:

mysql-0006

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Update Records

PHP – MySQL – Order By Keyword

PHP – MySQL – Order By Keyword

Statement:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Order records START
$result = mysqli_query($con,"SELECT * FROM Persons ORDER BY age");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'];
  echo " " . $row['LastName'];
  echo " " . $row['Age'];
  echo "<br>";
  }
// Order records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin:

mysql-0004

The result:

Glenn Quagmire 33
Peter Griffin 35
Andrea Tonin 39

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Order By Keyword

PHP – MySQL – Filter Results

PHP – MySQL – Filter Results

Statement:

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

<?php
// Create connection
// Statement: mysqli_connect(host,lucedigi_user,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Filter records START
$result = mysqli_query($con,"SELECT * FROM Persons
WHERE FirstName='Peter'");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br>";
  }
// Filter records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin

mysql-0004

The result:

Peter Griffin

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Filter Results