新聞中心
在php中如何同時(shí)向2個(gè)表插入數(shù)據(jù)
1、這個(gè)可以直接在同一個(gè)方法或者一個(gè)執(zhí)行里面寫兩條插入SQL的語(yǔ)句的。

我們提供的服務(wù)有:做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、臺(tái)山ssl等。為上千多家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的臺(tái)山網(wǎng)站制作公司
2、如果是有相關(guān)聯(lián)的話 可以啟用事務(wù)的模式。這樣如果有一條沒有執(zhí)行成功,那么都無(wú)法成功
Thinkphp怎么批量添加數(shù)據(jù)
?php
class FileAction extends Action {
public function Index()
{
//print_r(__URL__);
$file = M('file');
$list = $file-select();
$this-assign('filelist',$list);
$this-display();
}
public function upload()
{
//文件上傳的地址上傳給它,并且上傳完成后返回一個(gè)信息,讓其寫入數(shù)據(jù)庫(kù)
//如果$_FILES為空的畫,我就讓action給出一個(gè)錯(cuò)誤提示,告訴用戶必須選擇上傳文件。如果有上傳文件,則調(diào)用up方法
//$_FILES = $this-_post('file');
//print_r($_FILES);
if (empty($_FILES)) {
$this-error('必須選擇上傳文件');
}else {
$a = $this-Up();
//print_r($a);
if (isset($a)) {
//寫入數(shù)據(jù)庫(kù)方法
if($this-c($a)) {
$this-success('上傳成功');
}else {
$this-error('寫入數(shù)據(jù)庫(kù)失敗');
}
}else {
$this-error('上傳文件有異常請(qǐng)與系統(tǒng)管理員聯(lián)系');
}
}
}
private function c($data)
{
//print_r($data);
$file=M('file');
$num = '0';
for($i = 0; $i count($data)-1; $i++) {
$data['filename']=$data[$i]['savename'];
if( $file-data($data)-add())
{
$num++;
}
}
if($num==count($data)-1)
{
return true;
}else {
return false;
}
}
// private function c($data)
// {
// $file = M('file');
// $data['filename'] = $data[0]['savename'];
// if ($file-data($data)-add()) {
// return true;
// }else {
// return false;
// }
// }
//在這個(gè)方法當(dāng)中,完成與thinkphp相關(guān)的,文件上傳類的調(diào)用
private function Up()
{
//echo '模擬上傳';
//基本上傳功能
//批量上傳功能
//生成圖片縮略圖
//自定義參數(shù)上傳
//上傳檢測(cè)(大小,后綴,mime類型)
//支持覆蓋方式上傳
//上傳類型,附件大小,上傳路徑定義
//支持hash或者日期子目錄保存上傳文件
//上傳圖片的安全性檢測(cè)
//對(duì)上傳文件的hash檢測(cè)
//上傳文件名自定義規(guī)范
import('@.ORG.UploadFile');
import('@.ORG.Image');
$upload = new UploadFile();
$upload-maxSize = '1000000'; //指上傳文件大小,默認(rèn)為-1,不限制大?。╞ytes)
$upload-savePath = './Public/'; //上傳保存到什么地方?路徑建議保存到入口文件平級(jí)或平級(jí)目錄的子目錄
$upload-saveRule = 'uniqid'; //上傳文件的文件名保存規(guī)則 time uniqid(默認(rèn)) com_create_guid
$upload-hashType = 'md5_file';
$upload-autoCheck = true; //是否自動(dòng)檢測(cè)附件 默認(rèn)true
$upload-uploadReplace = true; //如果存在同名文件是否進(jìn)行覆蓋
$upload-allowExts = array('jpg','jpeg','png','gif'); //允許上傳的文件后綴
$upload-allowPath = array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg'); //檢測(cè)mime類型
$upload-thumb = true; // 是否開啟圖片文件縮略
$upload-thumbMaxWidth = '300,500';//縮略圖最大寬度
$upload-thumbMaxHeight = '200,400';//最大高度
$upload-thumbPrefix = 's-,m-';//縮略圖文件前綴
//$upload-thumbSuffix = '_s,_m';//文件后綴
//$upload-thumbPath = '';//如果留空直接上傳至
//$upload-thumbFile 在數(shù)據(jù)庫(kù)中也存一個(gè)文件名即可
$upload-thumbRemoveOrigin = 1; //如果生成縮略圖,是否刪除原圖
//$upload-autoSub 是否使用子目錄進(jìn)行保存上傳文件
//$upload-subType='' 子目錄創(chuàng)建方式默認(rèn)為hash 也可以為date
//$upload-dateFormat 子目錄方式date的指定日期格式
//$upload-hashLevle
//upload() 如果上傳成功返回true,失敗返回false
if ($upload-upload()) {
$info = $upload-getUploadFileInfo();
return $info;
}else {
//是專門來(lái)獲取上傳的錯(cuò)誤信息的
$this-error($upload-getErrorMsg());
}
}
}
?
PHP在網(wǎng)站上實(shí)現(xiàn)跟數(shù)據(jù)庫(kù)添加數(shù)據(jù)
把來(lái)自表單的數(shù)據(jù)插入數(shù)據(jù)庫(kù)
現(xiàn)在,我們創(chuàng)建一個(gè) HTML 表單,這個(gè)表單可把新記錄插入 "Persons" 表。
這是這個(gè) 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
當(dāng)用戶點(diǎn)擊上例中 HTML 表單中的提交按鈕時(shí),表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫(kù),并通過(guò) $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語(yǔ)句,一條新的記錄會(huì)添加到數(shù)據(jù)庫(kù)表中。
下面是 "insert.php" 頁(yè)面的代碼:
?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代碼怎么往數(shù)據(jù)庫(kù)里自定義插入數(shù)據(jù)
現(xiàn)在,我們創(chuàng)建一個(gè)
HTML
表單,這個(gè)表單可把新記錄插入
"Persons"
表。
這是這個(gè)
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
當(dāng)用戶點(diǎn)擊上例中
HTML
表單中的提交按鈕時(shí),表單數(shù)據(jù)被發(fā)送到
"insert.php"。"insert.php"
文件連接數(shù)據(jù)庫(kù),并通過(guò)
$_POST
變量從表單取回值。然后,mysql_query()
函數(shù)執(zhí)行
INSERT
INTO
語(yǔ)句,一條新的記錄會(huì)添加到數(shù)據(jù)庫(kù)表中。
如果要實(shí)現(xiàn)PHP與MYSQL的連接,利用phpMyAdmin建立數(shù)據(jù)庫(kù)和表后,怎樣在表中添加數(shù)據(jù)?
在admin中可以直接添加數(shù)據(jù),或在php程序中用insert語(yǔ)句添加數(shù)據(jù)。
admin是圖形操作界面,應(yīng)該很簡(jiǎn)單;在php程序中如:insert into user(id,name) values (1,'Bobo');
就是指在user表中插入id值為1,name值為Bobo的一行數(shù)據(jù)。當(dāng)然前提是執(zhí)行該語(yǔ)句前,php程序已經(jīng)和mysql數(shù)據(jù)庫(kù)服務(wù)器及數(shù)據(jù)庫(kù)連接成功。
網(wǎng)站名稱:php執(zhí)行添加數(shù)據(jù)方法,php執(zhí)行添加數(shù)據(jù)方法有哪些
轉(zhuǎn)載源于:http://www.dlmjj.cn/article/hsjses.html


咨詢
建站咨詢
