Php-mysql Course

The code presented in this tutorial shows how to Update and Delete data in MySQL table with data from HTML form. For details, see the comments in code.

• To Download the script with the code, click: Update and Delete MySQL data with Form (2 KB)

For this example it is used a table named "sites" with 4 columns: id, url, title, and description.

- SQL code to create the table:

CREATE TABLE sites (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, url VARCHAR(250) NOT NULL DEFAULT '/',
title VARCHAR(100), description VARCHAR(150)) CHARACTER SET utf8 COLLATE utf8_general_ci

- SQL code to insert some data:

INSERT INTO sites (url, title, description) VALUES
('https://coursesweb.net/php-mysql/update-delete-mysql-form_s2', 'Update and Delete MySQL data with data from Form', 'PHP-MySQL tutorial about Update and Delete MySQL data with data from HTML Form'),
('https://coursesweb.net/php-mysql/', 'PHP-MySQL Course', 'PHP-MySQL Course with tutorials, scripts and code snippets'),
('https://marplo.net/jocuri', 'Flash Games', 'Flash Games online from various categories; for smart people')

- PHP-MySQL code to Update and Delete with form:

<?php
//Update - Delete Data Using PHP, from: https://coursesweb.net/php-mysql/
$res ='';
$upd_html ='';
$del_html ='';

//Connect to mysql
$conn = new mysqli('localhost', 'root', 'password', 'dbname');

//check connection and set to use utf8
if(mysqli_connect_errno()) exit('Error connect: '. mysqli_connect_error());
else $conn->query('SET character_set_client="utf8",character_set_connection="utf8",character_set_results="utf8";');


//if form submited
if(isset($_POST['sbmt']) && isset($_POST['id'])){
  //if request to update, make update with data from field
  if($_POST['sbmt'] =='Update'){
    $id = intval($_POST['id']);
    $url = $conn->real_escape_string($_POST['url']);
    $title = $conn->real_escape_string($_POST['title']);
    $description = $conn->real_escape_string($_POST['description']);
    if($conn->query("UPDATE sites SET url='$url', title='$title', description='$description' WHERE id=". $id)) $res .='Row updated<br>';
    else $res .='Error update: '. $conn->error;
  }
  else if($_POST['sbmt'] =='Delete'){
  //if request to delete, delete the row with received $id
    $id = intval($_POST['id']);
    if($conn->query("DELETE FROM sites WHERE id=". $id)) $res .='Row deleted<br>';
    else $res .='Error delete: '. $conn->error;
  }
}
else if(isset($_GET['id'])){
  $id = intval($_GET['id']);
  $upd_html .='<form method="post" action="'.$_SERVER["PHP_SELF"].'"><input type="hidden" name="id" value="'. $id .'">';

  //gets name and email associated to $id for update
  $resql = $conn->query("SELECT * from sites WHERE id=".$id);
  while($row = $resql->fetch_assoc()) {
    $upd_html .='URL: <input type="text" name="url" value="'. $row['url'] .'"><br>
Title: <input type="text" name="title" value="'. $row['title'] .'"><br>
Description: <input type="text" name="description" value="'. $row['description'] .'"><br>';
  }
  $upd_html .='<input type="submit" name="sbmt" value="Update"></form><br>';
}
else {
//sets list with links for update and options for delete
  $resql = $conn->query("SELECT * from sites");
  while($row = $resql->fetch_assoc()){
    $upd_html .='<li><a href="'. $_SERVER["PHP_SELF"] .'?id='. $row['id'] .'" title="'. $row['title'] .'">'. $row['title'] .'</a></li>';
    $del_html .='<option value="'. $row['id'] .'">'. $row['title'] .'</)>';
  }

  $upd_html ='<ul>'. $upd_html .'</ul>';  //finishes html with list
  $del_html ='<form method="post" action="'.$_SERVER["PHP_SELF"].'"><select name="id">'. $del_html .'</select><br>
  <input type="submit" name="sbmt" value="Delete"></form>';  //finishes html with list
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update - Delete Data Using PHP</title>
</head>
<body>
<h1>Update - Delete Data Using PHP</h1>
<?php echo $res; ?>
<h2>Update</h2>
<?php echo $upd_html; ?>
<h2>Delete</h2>
<?php echo $del_html; ?>
</body>
</html>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
Update and Delete MySQL data with data from Form

Last accessed pages

  1. Using v-model in form input fields (1051)
  2. jQuery UI draggable - Drag elements (11445)
  3. Display data from PHP Array, or MySQL in HTML table (26980)
  4. Redirects (4978)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3519)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (310)
  2. The Mastery of Love (48)
  3. CSS cursor property - Custom Cursors (36)
  4. Read Excel file data in PHP - PhpExcelReader (35)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (31)