新聞中心
php中怎么把查詢出來的數(shù)據(jù)插入到另外一張表中
先查出A中的一條記錄,將記錄存入一個數(shù)組
成都創(chuàng)新互聯(lián)公司服務項目包括福田網(wǎng)站建設、福田網(wǎng)站制作、福田網(wǎng)頁制作以及福田網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,福田網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到福田省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
$list = select * from a where id=1;
再將$list里的數(shù)據(jù)插入B中(假設表中有id、name字段)
$sql = "insert into B values({$list['id']} , {$list['name']})";
exec($sql);
我感覺思路就是這樣吧,就直接查第一個表里的數(shù)據(jù)同時再插入到另外一個表中。
php數(shù)據(jù)庫添加、刪除、修改數(shù)據(jù)(mysql)
一、PHP操作MySql數(shù)據(jù)庫
新增數(shù)據(jù)
?php
$query
=
"INSERT
INTO
grade
(name,email,point,regdate)
VALUE
('
李三','[email protected]',,NOW())"
;
@mysql_query($query)
or
die(
'添加數(shù)據(jù)出錯:'
.mysql_error());
?
修改數(shù)據(jù)
?php
$query
=
"UPDATE
grade
SET
name='小可愛'
WHERE
id=6"
;
@mysql_query($query)
or
die(
'修改出錯:'
.mysql_error());
?
刪除數(shù)據(jù)
?php
$query
=
"DELETE
FROM
grade
WHERE
id=6";
@mysql_query($query)
or
die(
'刪除錯誤:'
.mysql_error());
?
顯示數(shù)據(jù)
?php
$query
=
"SELECT
id,name,email,point
FROM
grade";
$result
=
@mysql_query($query)
or
die(
'查詢語句出錯:'
.mysql_error());
while
(!!
$row
=
mysql_fetch_array($result))
{
echo
$row[
'id'
].
'----'
.$row['name'
].'----'
.$row
['email'
].
'----'
.$row['point'
];
echo
'br
/
';
}
?
二、其他常用函數(shù)
mysql_f
etch_row()
:從結(jié)果集中取得一行作為枚舉數(shù)組
mysql_f
etch_assoc()
:
從結(jié)果集中取得一行作為關聯(lián)數(shù)組
mysql_f
etch_array()
:
從結(jié)果集中取得一行作為關聯(lián)數(shù)組,或數(shù)字數(shù)組,或二者兼有
mysql_f
etch_lengths
()
:
取得結(jié)果集中每個輸出的長度
mysql_f
ield_name():
取得結(jié)果中指定字段的字段名
mysql_num_rows():
取得結(jié)果集中行的數(shù)目
mysql_num_f
ields():取得結(jié)果集中字段的數(shù)目
mysql_get_client_inf
o()
:
取得
MySQL
客戶端信息
mysql_get_host_info():
取得
MySQL
主機信息
mysql_get_proto_info():
取得
MySQL
協(xié)議信息
mysql_get_server_inf
o()
:
取得
MySQL
服務器信息
PHP在網(wǎng)站上實現(xiàn)跟數(shù)據(jù)庫添加數(shù)據(jù)
把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
現(xiàn)在,我們創(chuàng)建一個 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 表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
下面是 "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查詢MYSQL的內(nèi)容,并輸出結(jié)果
1、用navicat新建一個數(shù)據(jù)庫database1。
2、在database1數(shù)據(jù)庫中新建一個表table2。
3、在table2中添加新的數(shù)據(jù),新建一個名稱為mysql_query的數(shù)據(jù)庫。
4、在頁面中用mysql_connect 函數(shù)與數(shù)據(jù)庫建立連接。
5、用mysql_select_db函數(shù)選擇要查詢的數(shù)據(jù)庫。
6、添加一個查詢 table2表的查詢語句“$sql=select * from table2“。
7、將查詢語句$sql添加到查詢數(shù)據(jù)庫函數(shù)mysql_query中,返回值賦值給變量query。
8、最后將mysql_query。php文件在瀏覽器中打開,查看查詢到數(shù)據(jù)庫中的內(nèi)容的結(jié)果。
PHP將一張里查詢出來的一條數(shù)據(jù)加上輸入的數(shù)據(jù)一起添加的另一個數(shù)據(jù)表
如果是操作的Mysql數(shù)據(jù)庫,建議對這個操作進行事務管制。查詢出來的數(shù)據(jù)給一個變量,然后將之前的數(shù)據(jù)變量unset掉,輸入的數(shù)據(jù)如果是通過POST提交過來的,可以trim一下,正則匹配一下,然后用字符串拼接的方式和數(shù)據(jù)變量進行拼接給一個變量,unset沒用的兩個變量。最后,進行數(shù)據(jù)添加。
php數(shù)據(jù)查詢和數(shù)據(jù)插入
?php
$con = mysql_connect("localhost","用戶名","密碼");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("數(shù)據(jù)庫", $con);
$result = mysqli_query("SELECT 字段名 FROM 表名 where 字段名=判斷的變量");
if(mysqli_num_rows(result) 0)
{
echo "用戶名已存在";
} else {
mysqli_query("inset語句");
}
mysqli_close($con);
?
網(wǎng)頁標題:php查詢數(shù)據(jù)添加數(shù)據(jù),php頁面添加信息到數(shù)據(jù)庫
文章分享:http://fisionsoft.com.cn/article/hsjsoi.html