Use PHP to perform simple curd operations with SQL

Source: Internet
Author: User

1, starting from the database, first set up test data, the MySQL used here, through the script mode to create test data.

SET NAMES utf8;drop DATABASE IF EXISTS disk; CREATE DATABASE disk Charset=utf8; Use disk; CREATE TABLE udisk (uid INT PRIMARY KEY auto_increment,uname varchar), pic VARCHAR, Price FLOAT (9,2), Addedtime BIGINT); INSERT into Udisk VALUES (null, ' Kingston se9h ', ' img/1.jpg ', ' 49.90 ', ' 1234567890123 '), (null, ' Kingston Dt100g3 ', ' IMG/2. JPG ', ' 47.90 ', ' 1234567890123 '), (null, ' Right still transshow ', ' img/3.jpg ', ' 39.90 ', ' 1234567890123 '), (null, ' SanDisk (SanDisk) Cool treatments ( CZ73) ', ' img/4.jpg ', ' 79.90 ', ' 1234567890123 '), (NULL, ' Kingston 32GB ', ' img/5.jpg ', ' 99.90 ', ' 1234567890123 ');

2, create init.php is mainly used to save the PHP connection database related code.

<?php/* Database Link Initialization page */$conn = mysqli_connect (' 127.0.0.1 ', ' root ', ' ', ' Tarena ', 3306); $sql = ' SET NAMES UTF8 '; mysqli_ Query ($conn, $sql);

3, set up add.php implement PHP operation database to add data function.

<?php@ $uname = $_request[' uname ') or Die (' uname required '), @ $pic = $_request[' pic ') or Die (' pic required '); @ $price = $_ request[' price ' or die (' price required '), require (' disk_inti.php '); $addTime = time () *1000; $sql = "INSERT into Udisk VALUES (NULL, ' $uname ', ' $pic ', ' $price ', ' $addTime ') '; $result = Mysqli_query ($conn, $sql); if ($result ===false) {echo " Add failed, please check the SQL statement ";} Else{echo "added successfully, product number is". mysqli_insert_id ($conn); echo "<a href= ' udisk_select.php ' > view so USB stick </a>";}

4, create a view (add.html file) to implement the added functionality a simple data add entry test.

<!doctype html>

  

5. Use PHP to create the data lookup function main interface and use JS binding delete function

<?php//Execute SQL Lookup statement require (' disk_inti.php '); $sql = "SELECT * from Udisk"; $result = Mysqli_query ($conn, $sql); if ($resu Lt===false) {//Find results echo "Data query failed, please check SQL statement";} else{$list = Mysqli_fetch_all ($result, 1);} Var_dump ($list);? ><!doctype html>

  

6, create delete.php complete Delete function

<?php@ $uid = $_request[' uid ') or Die (' uid required '), require (' disk_inti.php '); $sql = "DELETE from Udisk WHERE uid= $uid "; $result = Mysqli_query ($conn, $sql), if ($result ===false) {echo" Delete failed ";} Else{echo "

  

Use PHP to perform simple curd operations with SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.