php聊天代码加数据库 php在线聊天源码

PHP在网站上实现跟数据库添加数据把来自表单的数据插入数据库
现在,我们创建一个 HTML 表单 , 这个表单可把新记录插入 "Persons" 表 。
这是这个 HTML 表单:
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
当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php" 。"insert.php" 文件连接数据库,并通过 $_POST 变量从表单取回值 。然后,mysql_query() 函数执行 INSERT INTO 语句 , 一条新的记录会添加到数据库表中 。
下面是 "insert.php" 页面的代码:
?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代码怎么往数据库里自定义插入数据现在,我们创建一个
HTML
表单,这个表单可把新记录插入
"Persons"
表 。
这是这个
HTML
表单:
123456789101112
htmlbody
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
当用户点击上例中
HTML
表单中的提交按钮时,表单数据被发送到
"insert.php" 。"insert.php"
文件连接数据库 , 并通过
$_POST
变量从表单取回值 。然后 , mysql_query()
函数执行
INSERT
INTO
语句 , 一条新的记录会添加到数据库表中 。
怎么使用php代码建立mysql数据库$link = mysql_pconnect("localhost","root","");
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "成功";
} else {
echo "失败" . mysql_error() . "\n";
}
注:不提倡使用函数mysql_create_db() 。最好用mysql_query()来提交一条SQLCREATEDATABASE语句来替代 。
PHP加数据库把来自表单的数据插入数据库
现在,我们创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表 。
这是这个 HTML 表单:
1
2
3
4
5
6
7
8
9
10
11
12
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
当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php" 。"insert.php" 文件连接数据库,并通过 $_POST 变量从表单取回值 。然后 , mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中 。
下面是 "insert.php" 页面的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
?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把代码插入到mysql数据库里主要你代码里面有“”,'',;之类的符号,会截断代码 , 你试着把那些代码写成一个字符串,再进行插入数据库
一段php 连接数据库的代码,几个函数,再加几个变量 。。貌似代码有问题,请高手帮我看下代码经修改,运行成功:
?php
function dbConnect($hostname, $username, $pass, $db_name, $pconnect = 0)
{
global $connect;
$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
if (!$connect)
{
$connect = @$func($hostname, $username, $pass) or die("font size='2'Mysql_Error : " .
mysql_error() . "brMysql Error Num : " . mysql_errno() . "/font");
}
@mysql_select_db($db_name, $connect) or die("font size='2' Mysql_Error : " .
mysql_error() . "brMysql Error Num : " . mysql_errno() . "/font");
return $connect;
}
function query_error($query)
{
global $connect;
$temp_bar = "br=============================================================================br";
$result = mysql_query($query, $connect) or die("DB ERROR br" . $temp_bar .
"font size='2' Mysql_Query : " . $query . "br Mysql_Error : " . mysql_error
() . "br Mysql Error Num : " . mysql_errno() . "/font" . $temp_bar);
return $result;
}
function do_fetch_array($result, $result_type = MYSQL_ASSOC, $records = "one")
{
$icount = mysql_num_rows($result);
if ($records == "one")
{
return @mysql_fetch_array($result, $result_type);
} else
{
for ($i = 0; $i$icount; $i)
{
$info[$i] = @mysql_fetch_array($result, $result_type);
}
mysql_free_result($result);
return $info;
}
}
function error_msg($msg, $url = "")
{
global $connect;
if ($connect)
{
mysql_close($connect);
}
switch ($url)
{
case "":
$url = "history.go(-1)";
break;
case "close":
$url = "window.close()";
break;
default:
$url = "document.location.href = 'https://www.04ip.com/post/$url'";
break;
}
if (!empty($msg))
{
echo "script language='javascript'alert('$str');$url;/script";
} else
{
echo "script language='javascript'$url;/script";
}
exit;
}
$hostname = "localhost";
$username = "root";
$pass = "root";
$db_name = "books";
$connect = dbConnect($hostname, $username, $pass, $db_name);
$recs = query_error('select * from books');
$rec = do_fetch_array($recs,MYSQL_ASSOC,'all');
echo 'pre';
print_r($rec);
echo '/pre';
?
【php聊天代码加数据库 php在线聊天源码】关于php聊天代码加数据库和php在线聊天源码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读