Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

PHP MYSQL

DATABASE UPDATE
Example
<?php
$con = mysqli_connect(“localhost”, “root”, “”, “first”);

$sql = “UPDATE students SET first_name = ‘George’, last_name


= ‘Uber’ WHERE id = 1”;

mysqli_query($con, $sql);
?>
DATABASE UPDATE
<?php
$con = mysqli_connect(“localhost”, “root”, “”, “first”); $sql_rets = “UPDATE students first_name = ‘George’,
last_name = ‘Uber’ WHERE id = 1”;
$sql = "SELECT * FROM students";
$result = mysqli_query($con, $sql); mysqli_query($con, $sql_rets);
$row = mysqli_fetch_assoc($results); ?>
echo $row[‘id’] . "<br>";
echo $row[‘first_name’] . "<br>";
echo $row[‘last_name’] . "<br>";
DATABASE UPDATE
<?php

$con = mysqli_connect(“localhost”, “root”, “”, “first”); <form action=“update.php” method=“post”>

$id = $_POST[‘id’];

$first_name = $_POST[‘first_name’]; <?php while($row = mysqli_fetch_assoc($results)) { ?>

$last_name = $_POST[‘last_name’]; <input type=“hidden” name=“id” value=“echo $row[‘id’];”><br>

<input type=“text” name=“first_name” value=“echo $row[‘first_name’];”><br>

$sql = "SELECT * FROM students"; <input type=“text” name=“last_name” value=“echo $row[‘last_name’];”><br>


$result = mysqli_query($con, $sql);
<input type=“submit”>
$row = mysqli_fetch_assoc($results);

</form>
?>
<?php } ?>
DATABASE UPDATE
update.php
<?php
$con = mysqli_connect(“localhost”, “root”, “”, “first”);
$id = $_POST[‘id’];
$first_name = $_POST[‘first_name’];
$last_name = $_POST[‘last_name’];
$sql_rets = “UPDATE students SET first_name = ‘$first_name’, last_name = ‘$last_name’ WHERE id = ‘$id’”;
mysqli_query($con, $sql_rets);

header("refresh:3;URL=index.php");
?>
DATABASE DELETE
Example
<?php
$con = mysqli_connect(“localhost”, “root”, “”,
“first”);
$sql = “DELETE FROM students WHERE id = 1";
mysqli_query($con, $sql);
?>
DATABASE DELETE
<?php
$con = mysqli_connect(“localhost”, “root”, “”, “first”); $sql_del = “DELETE FROM students WHERE id = 1”;
$sql = "SELECT * FROM students"; mysqli_query($con, $sql_del);
$result = mysqli_query($con, $sql);
?>
$row = mysqli_fetch_assoc($results);
echo $row[‘id’] . "<br>";
echo $row[‘first_name’] . "<br>";
echo $row[‘last_name’] . "<br>";
DATABASE DELETE
<?php <?php while($row = mysqli_fetch_assoc($results)) { ?>
$con = mysqli_connect(“localhost”, “root”, “”, “first”); <input type=“hidden” name=“id” value=“echo $row[‘id’];”><br>
$id = $_POST[‘id’]; <input type=“text” name=“id” value=“echo
$row[‘first_name’];”><br>
$first_name = $_POST[‘first_name’];
<input type=“text” name=“id” value=“echo $row[‘last_name’];”><br>
$last_name = $_POST[‘last_name’];
<input type=“submit”>
$sql = "SELECT * FROM students";
$result = mysqli_query($con, $sql); <a href="delete.php?id=<?php echo $row['id']?>">Delete</a>
$row = mysqli_fetch_assoc($results);
?> </form>
<?php } ?>
DATABASE DELETE
delete.php
$con = mysqli_connect(“localhost”, “root”, “”, “first”);
$id = $_GET[‘id’];

$sql_rets = “DELETE FROM students WHERE id = ‘$id’”;


mysqli_query($con, $sql_rets);

header("refresh:3;URL=index.php");

You might also like