MySQL——插入数据

插入数据

语法:INSERT INTO table_name(field,field...fieldN) VALUES


通过命令提示窗口插入数据

INSERT INTO bird_tbl
(title, author, submission_date) 
VALUES
("PHP","xx",now());

INSERT INTO bird_tbl
(title, author, submission_date) 
VALUES
("MySQL","xx","2021-02-11");


补充说明:

  • 如果插入的数据类型是字符型,需要用单引号或双引号
  • now()是一个MySQL函数,返回日期和时间

使用PHP脚本插入数据

<?php

header("content-type:text/html;charset=utf-8");


$dbhost='localhost';
$dbuser='root';
$dbpass='root';
$conn=mysqli_connect($dbhost,$dbuser,$dbpass);
if(!$conn)
{
    die('连接失败:'.mysqli_error($conn));
}
echo '连接成功';
mysqli_query($conn,"set names utf8");

$title='学习 Python';
$author='xxx';
$sql="INSERT INTO bird_tbl".
    "(title,author,submission_date)".
    "VALUES".
    "('$title','$author',now())";

mysqli_select_db($conn,'bird');
$retval=mysqli_query($conn,$sql);

if(!$retval)
{
    die('无法插入数据'.mysqli_error($conn));
}
echo "数据插入成功\n";
mysqli_close($conn);
?>

posted @ 2021-02-11 20:14  ceiling~  阅读(93)  评论(0编辑  收藏  举报