Insert / Delete / Update PHP-MySql


PHP Insert Data

<?php
//load database connection
include("php-connect.php");
$name = "Elisa";
$email = "elisa@mail.com";
$country = "Canada";

//Command to insert into table
$query = "INSERT INTO people (name,email,country) VALUES ('$name','$email','$country')";

//run the query to insert the person.
$result = mysql_query($query) OR die(mysql_error());

//let them know the person has been added.
echo "Data successfully inserted into the database table ... ";
?>

PHP Update Data

<?php
//load database connection
include("php-connect.php");
$name = "Elisa";
$email = "elisa@mail.com";
$country = "Indonesia";

//Make the query to update the table.
$query = "UPDATE people SET country='$country'  WHERE email='$email'";

//run the query to update the person.
$result = mysql_query($query) OR die(mysql_error());

//let them know the person has been added.
echo "Data successfully updated into the database table ... ";
?>

PHP View Data

<?php
//load database connection
include("php-connect.php");

//make the query to run.
//Sort the last name in an ascending order (A-Z)
$query = "SELECT * FROM people ORDER BY name ASC";
$result = mysql_query($query) OR die(mysql_error());

//now we turn the results into an array and loop through them.
while($row = mysql_fetch_array($result))
{
$name = $row['name'];
$email = $row['email'];
$country = $row['country'];
echo "Data successfully selected in the database table ... ";
echo "<br/>name :".$name;
echo "<br/>email :".$email;
echo "<br/>country :".$country;
}
?>

PHP Delete Data

<?php
//load database connection
include("php-connect.php");

//get the userid of the person we're deleting.
$email = "elisa@mail.com";

//write the query to delete the person.
$query = "DELETE FROM people WHERE email='$email'";

//run the query to delete the person.
$result = mysql_query($query) OR die(mysql_error());

echo "Data successfully deleted in the database table ... ";
?>


Database Creation Script

CREATE TABLE 'people' (
  'id' int(11) NOT NULL auto_increment,
  'name' varchar(100) NOT NULL,
  'email' varchar(100) NOT NULL,
  'country' varchar(25) NOT NULL,
  PRIMARY KEY  ('id')
) ;


No comments:

Post a Comment