php连接mysql,如何用php代码将数据写入数据库?》??
连接:mysql_connect("主机","用户","密码");
创新互联专业提供成都移动服务器托管服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买成都移动服务器托管服务,并享受7*24小时金牌售后服务。
mysql_select_db("数据库名");
写入数据:mysql_query("insert
into
表名
(字段1,字段2)
values
("数据1","数据2")");
你的问题太含糊,只能给你这个答复,不明白再追问。
php向数据库插入一条数据代码 急急急!!!!
$rs
=
mysql_query($sql);
这一段改成:
if(mysql_query($sql)){
echo
"script
language=JavaScriptalert('数据库提交成功!');window.location.href='team.php';/script";
}else{
echo
"插入失败,错误原因是{mysql_error()}";
}
然后根据错误原因解决问题,或者把错误原因给大家看看。
如果仍然提示成功,请检查你的权限,还有你的mysql数据库Team这个表里的主键有没有重复?
php 写入数据库 例子
?php
// 以 MySQL 为例:
mysql_connect('127.0.0.1', 'root', 'root', 3306); // 连接数据库
mysql_select_db('test'); // 选择数据库
mysql_query('set names utf8'); // 执行SQL
// 插入数据语句
$sql = "INSERT INTO table (username, password) VALUES ('Jack@163.com', '123456')";
$r = mysql_query($sql);
if (mysql_affected_rows()) {
echo '新增成功';
} else {
echo mysql_error();
}
php写入数据库
PHP向MySQL数据库中写入数据有三个步骤:
1,PHP和MySQL建立连接关系
2,打开MySQL数据库
3,接受页面数据,PHP录入到指定的表中
1、2两步可直接使用一个数据库链接文件即可:conn.php
代码如下
?php
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
?
当然,前提是已经安装WEB服务器、PHP和MySQL,并且建立MySQL表“cnbruce”
mysql_connect()中三个参数分别为MySQL地址、MySQL用户名和MySQL密码
然后就是通过WEB页面传递数据,让PHP通过SQL语句将数据写入MySQL数据库指定的表中,比如新建文件 post.php
代码如下
?php
require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
?
测试页面: ;p=i0514
即可向MySQL数据库hello的members表中插入新的数据“cnbruce”到username字段、“i0514”到password字段
补充:读取表
读取表中的内容,这里我们用while,可以根据具体情况,用for 或其他的.
代码如下
while($row = mysql_fetch_array($result))
{
echo "div style="height:24px; line-height:24px; font-weight:bold;""; //排版代码
echo $row['Topic'] . "br/";
echo "/div"; //排版代码
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把html表单内容写入数据库
1:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)
2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。
具体示例:
(1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。
?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?
(2)其次创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。
html
body
form action="insert.php" method="post"
Firstname: input type="text" name="firstname" /
Lastname: input type="text" name="lastname" /
Age: input type="text" name="age" /
input type="submit" /
/form
/body
/html
(3)接着当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过
$_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。
?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?
当前名称:php写入数据代码 php代码输出文字
链接地址:http://lswzjz.com/article/hhoppd.html