php写数据到mysql js传输数据到php
php表单写入mysql数据库的代码
!--表单文件,拷入index.php--
企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联面向各种领域:建筑动画等网站设计、成都全网营销解决方案、网站设计等建站排名服务。
!DOCTYPE html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd students/title
/head
body
!-- 数据库用mysqli 面向过程调用方法--
form method="post" action="write2db.php"
!--数据库用mysqli 面向过程调用方法
form method="post" action="write2db_sqlio.php"
--
!--数据库用PDO调用方法
form method="post" action="write2db_pdo.php"
--
labelFirst Name/label
input type="text" name="first_name" /
br /
labelLast Name/label
input type="text" name="last_name" /
br /
labeldepartment/label
input type="text" name="department" /
br /
labelEmail/label
input type="text" name="email" /
br /
input type="submit" value="Add students"
/form
/body
/html
------------------------------
?php
//拷贝命名为write2db.php,数据库用mysqli 面向过程调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//your database name
$dbname = "test";
$tablename ="student";
// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname);
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
//Execute the query
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if (mysqli_query($connect, $sql)) {
echo "Hooray! New record is inserted to database successfully. Please check database.";
} else {
echo "Error: " . $sql . "br /" . mysqli_error($connect);
}
mysqli_close($connect);
?
?php
//拷贝命名为write2db_sqlio.php,数据库用mysqli 面向对象调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//database name
$dbname = "test";
$tablename ="student";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn-connect_error) {
die("Connection failed: " . $conn-connect_error);
}
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if ($conn-query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "br" . $conn-error;
}
$conn-close();
?
?php
//拷贝为文件write2db_pdo.php,数据库用PDO调用方法
//print_r($_POST);
a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//your database name
$dbname = "test";
$tablename ="student";
// Create connection
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
// use exec()
$conn-exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "br" . $e-getMessage();
}
$conn = null;
?
--创建数据库test, 将此文件存为test.sql 导入数据库,或者手动创建表结构
-- phpMyAdmin SQL Dump
-- version 4.7.4
--
--
-- Host: 127.0.0.1:3306
-- Generation Time: Mar 12, 2018 at 04:04 AM
-- Server version: 5.7.19
-- PHP Version: 7.1.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`;
CREATE TABLE IF NOT EXISTS `student` (
`id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`department` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `student`
--
INSERT INTO `student` (`id`, `first_name`, `last_name`, `department`, `email`) VALUES
(1, 'first1', 'last1', 'cs', '1985@qq.com');
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
用PHP批量添加数据到Mysql
首先接受过来的数据都在$_POST里,通过foreach我们来处理,我举个最简单的例子。
比如一些处理sql注入或js注入的函数叫check();
return 的是被处理后的结果,而非真或假。
下面是例子:
foreach($_POST as $key=$value){
$data[$key] = check($_POST[$key]);
$sql = "INSERT INTO test_table values('{$_POST[$key]}')";
mysql_query($sql);
}
html通过php把数据写入mysql
meta http-equiv="Content-Type" content="text/html; charset=utf-8"
form method="post" action="?do=ok"
ul
li用户名input name="yhm" type="text"/li
li电话input name="dh" type="text"/li
liinput name="提交" type="submit" value="提交"/li
/ul
/form
?php
if($_GET['do']=='ok'){
mysql_connect('localhost','数据库用户名','密码');
mysql_select_db('数据库名');
$result = mysql_query("INSERT INTO 表名 (用户名字段,电话字段) VALUES ('".$_POST['yhm']."','".$_POST['dh']."')");
if($result){
echo '插入成功';
}else{
echo '插入失败';
}
}
?
保存为php文件
php源码写入mysql数据库问题
$sql="insert into sfb_zhanghu(Account,Password) values ($zh,$mm)"; // 这出错
加单引号
values ( '$zh' , '$mm') // 数字可以不用放,字母一定要
php向mysql写入数据
if(!$sql)
{
echo('注册失败!');
}
else
{
echo('注册成功!');
}
这个判断明显是错误的,$sql 这肯定是有值的,它的值就是你给的sql语句。所以不管你怎么注册,都是输出注册成功。而应该判断它的反回值$query=mysql_query($sql,$con);如果注册不成功,$query=flase;否则反之.
php把数组写入mysql的方法
您好,您可以参考如下代码:
?php
/* 获取HTML表单提交的数据 */
$id = _POST["id"];
$catid = _POST["catid"];
$ly = array("data***");
/* 连接数据库,如果出错显示消息结束程序,否则继续执行数据库命令 */
$con = mysql_connect("127.0.0.1","user","password");
if($con == false)
{
echo "错误:系统数据库连接失败!错误信息:".mysql_error($con);
exit();
}else{
/* 选择需要操作数据库 */
mysql_select_db("mydb",$con);
/* 根据ly数组的长度决定执行多少次SQL语句 */
for($i=0;$icount($ly);$i++)
{
/* 设置SQL查询语句 */
$sql = "INSERT INTO `biaoge` VALUES('{$id}','{$catid}',".$ly[$i].")";
/* 如果SQL语句执行失败,返回错误代码; */
if(mysql_query($sql,$con) == false)
{
echo "数据库查询出错!错误代码:".mysql_errno($con);
mysql_close($con);
exit();
}
}
mysql_close($con);
echo "记录添加成功!";
}
?
至于遍历数组,有很多种方式,如楼上用的foreach语句也是可以的。这里用for的原因是为了方便提问者理解。如果代码仍有不足或不明白的地方,请您继续追问,直到给您一个满意的答复为止~
新闻名称:php写数据到mysql js传输数据到php
网站URL:http://myzitong.com/article/dodhhpj.html