0% found this document useful (0 votes)
28 views9 pages

Integrative Programming and Technologies 9

The document discusses PHP code examples for performing database updates and deletes with MySQL. It shows connecting to a database, writing SQL statements to update or delete a record based on its ID, and executing the SQL query. For updates, a form is used to submit new data and a redirect refreshes the page. For deletes, a link passes the ID to delete.php which performs the delete query and redirects.

Uploaded by

jelyntabuena977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

Integrative Programming and Technologies 9

The document discusses PHP code examples for performing database updates and deletes with MySQL. It shows connecting to a database, writing SQL statements to update or delete a record based on its ID, and executing the SQL query. For updates, a form is used to submit new data and a redirect refreshes the page. For deletes, a link passes the ID to delete.php which performs the delete query and redirects.

Uploaded by

jelyntabuena977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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