字段数据实例
以下实例中程序接收用户输入的三个字段数据,并插入数据表中:
添加数据
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; // 设置编码,防止中文乱码 mysqli_query($conn , "set names utf8"); $runoob_title = '学习 Python'; $runoob_author = 'RUNOOB.COM'; $submission_date = '2016-03-06'; $sql = "INSERT INTO runoob_tbl ". "(runoob_title,runoob_author, submission_date) ". "VALUES ". "('$runoob_title','$runoob_author','$submission_date')"; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('无法插入数据: ' . mysqli_error($conn)); } echo "数据插入成功\n"; mysqli_close($conn); ?>