<?php header("Content-type:text/html;charset=utf-8"); $host = 'localhost'; $database = 'cs'; $username = 'root'; $password = '123123'; // $json = @$_POST["data"]; // $res = json_decode($json); // $name = $res['name']; // $tel = $res['tel']; // $company = $res['company']; // $sys = $res['sys']; $name = @$_POST['name']; $tel = @$_POST['tel']; $company = @$_POST['company']; $sys = @$_POST['sys']; if(!$name){ echo("Error:There is no name found"); exit; } //转义SQL字符串中的特殊字符 去掉HTML及PHP标记 单双引号、反斜线及NULL加上反斜线转义 去掉HTML及PHP标记 // $name = mysqli_real_escape_string(strip_tags( addslashes(stripslashes($name)))) ; $name = strip_tags( addslashes(stripslashes($name))); $tel = strip_tags( addslashes(stripslashes($tel))); $company = strip_tags( addslashes(stripslashes($company))); $sys = strip_tags( addslashes(stripslashes($sys))); //连接到数据库 $connection = mysqli_connect($host, $username, $password); //数据库编码格式 mysqli_query($connection,"set names utf8"); //诊断连接错误 if (!$connection) { die("could not connect to the database.\n" . mysql_error()); } //选择数据库 $selectedDb = mysqli_select_db($connection,$database); if (!$selectedDb) { die("could not to the database\n" . mysql_error()); } //构建查询语句 $query = "insert into a_user (name,tel,company,sys) values ('$name','$tel','$company','$sys')"; //执行查询 $result = mysqli_query($connection,$query); // dump($result);exit; if(!$result){ echo("fail to insert data"); }else{ echo("sucess in insert data"); } @ mysqli_free_result($result); //关闭连接 mysqli_close($connection);
原生PHP语句连接数据库并插入数据
最新推荐文章于 2024-05-10 16:29:18 发布