php動(dòng)態(tài)添加數(shù)據(jù) php動(dòng)態(tài)變量

php 動(dòng)態(tài)數(shù)組添加問(wèn)題

$count = count($_POST['color']);

十多年的虞城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整虞城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“虞城網(wǎng)站設(shè)計(jì)”,“虞城網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

$final_result = array();

for($i=0;$i$count;$i++){

//壓進(jìn)數(shù)組

$single_record = array(

0=$_POST['color'][$i],

1=$_POST['size'][$i],

2=$_POST['number'][$i]

);

array_push($final_result, $single_record);

//插入數(shù)據(jù)庫(kù)

$sql = "INSERT INTO table VALUES('".$_POST['color'][$i]."','".$_POST['size'][$i]."','".$_POST['number'][$i]."')";

mysql_query($sql);

}

php兩層循環(huán),動(dòng)態(tài)向數(shù)組添加數(shù)據(jù)的時(shí)候,第二次會(huì)把第一次的追加進(jìn)去了,如何去掉?

1

2

3

4

5

6

7

8

9

10

$data= [1,2,3,4,5];

for( $i=0; $icount( $data); $i++) {

if( $i== 0 ) {

echo$data[$i+1];

} elseif( $i==1 ){

echo$data[$i-1];

} else{

echo$data[$i];

}

}

原本12345

循環(huán)后

輸出21345

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中 jquery如何獲取js動(dòng)態(tài)添加的文本框數(shù)組值,并插入mysql數(shù)據(jù)庫(kù)?

建2個(gè)頁(yè)面

1、test.php頁(yè)面,代碼如下:

title/title

script src="jquery-1.4.2.min.js" type="text/javascript"/script

script type="text/javascript"

$(function(){

x=100000;

y=1;

$("#dosubmit").click(function(){

var rand=parseInt(Math.random()* (x - y ));

var rand1=String.fromCharCode(Math.floor( Math.random() * 26) + "a".charCodeAt(0));

var col="input name=info["+rand1+rand+"] type='text' class='text' value='"+rand+"' /";

$("input[id=submit]").before(col);

})

$("#submit").click(function(){

$("form input[class=text]").each(function(){

var val=$(this).val();

})

})

})

/script

meta http-equiv="Content-Type" content="text/html; charset=utf-8"

div class="con"

form name="form1" action="testsave.php" method="post"

input name="info[name]" class="text" value="10" type="text"

input name="" class="text1" value="顯示" id="submit" type="submit"

/form

input name="" class="text1" value="增加" id="dosubmit" type="submit"br

/div

2、保存提交過(guò)來(lái)的值頁(yè)面testsave.php頁(yè)面。代碼如下:

script src="jquery-1.4.2.min.js" type="text/javascript"/script

?php

$value="";

$test='test';

$conn=mysql_connect('localhost','root','0000','test');

mysql_select_db($test,$conn);

foreach($_POST['info'] as $key){

$value=$value.','.$key;

}

$sql="insert into base (name) values ('$value')";

if(mysql_query($sql)){

echo "添加成功br /";

}

?

input type="button" name="button" id="button" value="顯示/隱藏內(nèi)容"

input type="button" name="domit" id="domit" value="返回"

div class="content" style="display:none"

?php

$sql1="select id,name from base";

$query=mysql_query($sql1);

while($result=mysql_fetch_array($query)){

echo $result['id']."========".$result['name']."br /";

}

?

/div

?php

mysql_close($conn);

?

script type="text/javascript"

$(function(){

$("#button").bind("click",function(){

var show=$("div.content");

if(show.is(":visible")){

show.hide();

}

else{

show.show();

}

})

$("#domit").click(function(){

window.history.go(-1);

})

})

/script

新聞名稱:php動(dòng)態(tài)添加數(shù)據(jù) php動(dòng)態(tài)變量
文章出自:http://muchs.cn/article10/dophido.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航搜索引擎優(yōu)化、服務(wù)器托管、網(wǎng)站維護(hù)網(wǎng)站營(yíng)銷(xiāo)、做網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)