php怎樣實(shí)現(xiàn)增加數(shù)據(jù) php增加數(shù)組元素

在PHP中怎么實(shí)現(xiàn)新增數(shù)據(jù),刷新表格,而不刷新整個(gè)頁面。

ajax把增加的數(shù)據(jù)傳到后臺(tái),保存成功的話就然后查詢數(shù)據(jù)庫,把當(dāng)前分頁的內(nèi)容讀取,再傳回前臺(tái),前臺(tái)先關(guān)掉彈出的頁面,然后用js或者jq把原來表格的內(nèi)容直接干掉,然后在把新查詢的數(shù)據(jù)循環(huán)出來用js或者jq寫成表格到相應(yīng)位置啊,還是很好實(shí)現(xiàn)的

超過十多年行業(yè)經(jīng)驗(yàn),技術(shù)領(lǐng)先,服務(wù)至上的經(jīng)營(yíng)模式,全靠網(wǎng)絡(luò)和口碑獲得客戶,為自己降低成本,也就是為客戶降低成本。到目前業(yè)務(wù)范圍包括了:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,成都網(wǎng)站推廣,成都網(wǎng)站優(yōu)化,整體網(wǎng)絡(luò)托管,小程序制作,微信開發(fā),成都app軟件開發(fā)公司,同時(shí)也可以讓客戶的網(wǎng)站和網(wǎng)絡(luò)營(yíng)銷和我們一樣獲得訂單和生意!

PHP在網(wǎng)站上實(shí)現(xiàn)跟數(shù)據(jù)庫添加數(shù)據(jù)

把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫

現(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ù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會(huì)添加到數(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代碼怎么往數(shù)據(jù)庫里自定義插入數(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ù)庫,并通過

$_POST

變量從表單取回值。然后,mysql_query()

函數(shù)執(zhí)行

INSERT

INTO

語句,一條新的記錄會(huì)添加到數(shù)據(jù)庫表中。

PHP怎么 實(shí)現(xiàn)批量添加數(shù)據(jù)

在HTML里這樣寫:

1

2

3

4

5

6

form ........

input type="text" name="haoma[]" /

input type="text" name="haoma[]" /

input type="text" name="haoma[]" /

....

第N個(gè)

foreach($_POST['haoma'] as $v){

$sql="insert into haoma values('',$v)";

.......

}

當(dāng)然這樣寫一條一條的效率是會(huì)慢,再改一下

foreach($_POST['haoma'] as $v){

$values.="('',$v),";

}

$values=substr($values,0,strlen($values)-1);

$sql="insert into haoma values $values";

......

下面的query我就不寫了

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ù)庫

//如果$_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ù)庫方法

if($this-c($a)) {

$this-success('上傳成功');

}else {

$this-error('寫入數(shù)據(jù)庫失敗');

}

}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ù)庫中也存一個(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 {

//是專門來獲取上傳的錯(cuò)誤信息的

$this-error($upload-getErrorMsg());

}

}

}

?

本文標(biāo)題:php怎樣實(shí)現(xiàn)增加數(shù)據(jù) php增加數(shù)組元素
分享路徑:http://muchs.cn/article44/dossghe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名企業(yè)網(wǎng)站制作、網(wǎng)站建設(shè)定制網(wǎng)站、云服務(wù)器做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司