A message board for newcomers to get in touch with "add, delete, modify, and query" in MySQL more quickly. Please give me some advice. There are still many shortcomings. I hope you can learn more during the improvement process and share it together. The table name of the database is weibo, which contains id (remember to select auto_increment in phpmyadmin for automatic growth), nickname (alias varchar), dates (dateTime), content (content, text), hits (int)
include("conn.php");
mysql_set_charset('utf8');
if (!empty($_POST['sub'])) {
$name = $_POST ['nickname']; //The obtained fields are converted into variables
$content = $_POST['content'];
$sql = "INSERT INTO `weibo` (`nickname`,`dates`,`content`) VALUES ('$name',now(),'$content')"; //Database statements correspond to nickname, dates (display publication time) in the database, content
//Execute the mysql statement, if the execution is successful, Return message notification.
if (mysql_query($sql)) {
echo("Insertion successful");
}else {//Same as failure
echo("Insertion failed").mysql_error();
}
}
?>
Message page
Your nickname:< ;/td>
Talk about:
< ;/form>
Copy code
//Set the administrator, password, and connected data table to connect to the database
$local = 'localhost';
$dbname = 'root';
$passwd = '3363064';
$db = 'weibo';
mysql_connect($local,$dbname,$passwd) or die("falie");
mysql_select_db($db);
//Test the connection. If the database connection is successful, output yes , fail fail
// if ($query) {
// echo("yes");
// }else {
// echo "faile";
// }
?>
Copy code
//Delete page
include("conn.php");
//Delete database words
if (!empty($_GET['del'])) { //Use get Get the fields of del
$del = $_GET['del']; //Convert the obtained fields into variables
$sql = "delete from `weibo` where id='$del'"; //Delete mysql statement , delete from table name, the condition is that id is equal to 'del' obtained from index.php
$query=mysql_query($sql);
if ($query) {//Determine whether the mysql statement is executed successfully, and prompt.
echo("Deletion successful");
header("refresh:5;url='index.php'");
}else {
echo("Failed...").mysql_error();
}
}
?>
Copy code
include("conn.php");
if (!empty($_GET['id'])) { //If the obtained id is not empty, start executing the following statement
$sql = "select *from weibo where id = '".$_GET['id']."'";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
}
if (!empty($_POST['sub'])) { //If the sub obtained from the form is not empty, execute the following statement
$name = $_POST['nickname']; //The obtained fields Convert to variables
$content = $_POST['content'];
$hid = $_POST['hid'];
$mysql = "UPDATE `WEIBO` SET `nickname`='$name',`content`= '$content' where id='$hid'";
if (!empty($_GET['id'])) { //Get the id in the index, use get
$sql = "select *from weibo where id = '".$_GET['id']."'"; //Execute the database statement, select the database as weibo, and the condition is that the id is equal to the id obtained from index.php
$query = mysql_query($sql);
$rs = mysql_fetch_array($query); //Read the table in the database in array form.
//The following is a database statement for clicks. Hits are added to the table, and hits are equal to one visit per visit + 1.
$sqlhits = "update weibo set hits=hits+1 where id = '".$_GET[' id']."'";
mysql_query($sqlhits);
}
?>
Username:
Published time:
Clicks:
Content :
Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn