php MySQLi插入,更新和删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php MySQLi插入,更新和删除相关的知识,希望对你有一定的参考价值。
<script
src="https://code.jquery.com/jquery-1.12.4.js"
integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">PHPblog</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="<?php echo ROOT_URL; ?>">Home</a></li>
<li><a href="<?php echo ROOT_URL; ?>addpost.php">Add Post</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<!DOCTYPE html>
<html>
<head>
<title>PHP Blog</title>
<link rel="stylesheet" type="text/css" href="http://bootswatch.com/cerulean/bootstrap.min.css">
</head>
<body>
<?php include('navbar.php'); ?>
<?php
// Create Connection
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check Connection
if(mysqli_connect_errno()){
// Connection Failed
echo 'Failed to connect to MySQL '. mysqli_connect_errno();
}
<?php
define('ROOT_URL', 'http://localhost/phpsandbox/website8/');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '123456');
define('DB_NAME', 'phpblog');
<?php
require('config/config.php');
require('config/db.php');
// Check For Submit
if(isset($_POST['submit'])){
// Get form data
$update_id = mysqli_real_escape_string($conn, $_POST['update_id']);
$title = mysqli_real_escape_string($conn, $_POST['title']);
$body = mysqli_real_escape_string($conn, $_POST['body']);
$author = mysqli_real_escape_string($conn,$_POST['author']);
$query = "UPDATE posts SET
title='$title',
author='$author',
body='$body'
WHERE id = {$update_id}";
if(mysqli_query($conn, $query)){
header('Location: '.ROOT_URL.'');
} else {
echo 'ERROR: '. mysqli_error($conn);
}
}
// get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);
// Create Query
$query = 'SELECT * FROM posts WHERE id = '.$id;
// Get Result
$result = mysqli_query($conn, $query);
// Fetch Data
$post = mysqli_fetch_assoc($result);
//var_dump($posts);
// Free Result
mysqli_free_result($result);
// Close Connection
mysqli_close($conn);
?>
<?php include('inc/header.php'); ?>
<div class="container">
<h1>Add Post</h1>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" value="<?php echo $post['title']; ?>">
</div>
<div class="form-group">
<label>Author</label>
<input type="text" name="author" class="form-control" value="<?php echo $post['author']; ?>">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control"><?php echo $post['body']; ?></textarea>
</div>
<input type="hidden" name="update_id" value="<?php echo $post['id']; ?>">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</form>
</div>
<?php include('inc/footer.php'); ?>
<?php
require('config/config.php');
require('config/db.php');
// Check For Submit
if(isset($_POST['submit'])){
// Get form data
$title = mysqli_real_escape_string($conn, $_POST['title']);
$body = mysqli_real_escape_string($conn, $_POST['body']);
$author = mysqli_real_escape_string($conn,$_POST['author']);
$query = "INSERT INTO posts(title, author,body) VALUES('$title', '$author', '$body')";
if(mysqli_query($conn, $query)){
header('Location: '.ROOT_URL.'');
} else {
echo 'ERROR: '. mysqli_error($conn);
}
}
?>
<?php include('inc/header.php'); ?>
<div class="container">
<h1>Add Post</h1>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control">
</div>
<div class="form-group">
<label>Author</label>
<input type="text" name="author" class="form-control">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control"></textarea>
</div>
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</form>
</div>
<?php include('inc/footer.php'); ?>
<?php
require('config/config.php');
require('config/db.php');
// Check For Submit
if(isset($_POST['delete'])){
// Get form data
$delete_id = mysqli_real_escape_string($conn, $_POST['delete_id']);
$query = "DELETE FROM posts WHERE id = {$delete_id}";
if(mysqli_query($conn, $query)){
header('Location: '.ROOT_URL.'');
} else {
echo 'ERROR: '. mysqli_error($conn);
}
}
// get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);
// Create Query
$query = 'SELECT * FROM posts WHERE id = '.$id;
// Get Result
$result = mysqli_query($conn, $query);
// Fetch Data
$post = mysqli_fetch_assoc($result);
//var_dump($posts);
// Free Result
mysqli_free_result($result);
// Close Connection
mysqli_close($conn);
?>
<?php include('inc/header.php'); ?>
<div class="container">
<a href="<?php echo ROOT_URL; ?>" class="btn btn-default">Back</a>
<h1><?php echo $post['title']; ?></h1>
<small>Created on <?php echo $post['created_at']; ?> by <?php echo $post['author']; ?></small>
<p><?php echo $post['body']; ?></p>
<hr>
<form class="pull-right" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="delete_id" value="<?php echo $post['id']; ?>">
<input type="submit" name="delete" value="Delete" class="btn btn-danger">
</form>
<a href="<?php echo ROOT_URL; ?>editpost.php?id=<?php echo $post['id']; ?>" class="btn btn-default">Edit</a>
</div>
<?php include('inc/footer.php'); ?>
<?php
require('config/config.php');
require('config/db.php');
// Create Query
$query = 'SELECT * FROM posts ORDER BY created_at DESC';
// Get Result
$result = mysqli_query($conn, $query);
// Fetch Data
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
//var_dump($posts);
// Free Result
mysqli_free_result($result);
// Close Connection
mysqli_close($conn);
?>
<?php include('inc/header.php'); ?>
<div class="container">
<h1>Posts</h1>
<?php foreach($posts as $post) : ?>
<div class="well">
<h3><?php echo $post['title']; ?></h3>
<small>Created on <?php echo $post['created_at']; ?> by <?php echo $post['author']; ?></small>
<p><?php echo $post['body']; ?></p>
<a class="btn btn-default" href="<?php echo ROOT_URL; ?>post.php?id=<?php echo $post['id']; ?>">Read More</a>
</div>
<?php endforeach; ?>
</div>
<?php include('inc/footer.php'); ?>
以上是关于php MySQLi插入,更新和删除的主要内容,如果未能解决你的问题,请参考以下文章