Home > Backend Development > PHP Tutorial > 新人,把多行表单作为数组UP进数据库的各种小疑问

新人,把多行表单作为数组UP进数据库的各种小疑问

WBOY
Release: 2016-06-13 12:53:32
Original
879 people have browsed it

新人求助,把多行表单作为数组UP进数据库的各种小疑问!
因为网站的需求,需要做一个页面提交表单,需要涉及到多行表单 作为数组的形式提交到数据库!
度娘得到一个范例:

<tr> <br />
        <td>1</td> <br />
        <td><input type="text" name="name[]" value="许亮"></td> <br />
        <td><input type="text" name="age[]" value="25"></td> <br />
        <td><input type="text" name="position[]" value="SAP开发工程师"></td> <br />
    </tr> <br />
    <tr> <br />
        <td>2</td> <br />
        <td><input type="text" name="name[]" value="周未红"></td> <br />
        <td><input type="text" name="age[]" value="22"></td> <br />
        <td><input type="text" name="position[]" value="招聘主管"></td> <br />
    </tr> <br />
    <tr> <br />
        <td>3</td> <br />
        <td><input type="text" name="name[]" value="胡睿妮"></td> <br />
        <td><input type="text" name="age[]" value="24"></td> <br />
       <td><input type="text" name="position[]" value="银行职员"></td> <br />
    </tr> <br />
    <tr> <br />
        <td>4</td> <br />
        <td><input type="text" name="name[]" value="张凯"></td> <br />
        <td><input type="text" name="age[]" value="35"></td> <br />
        <td><input type="text" name="position[]" value="经理"></td> <br />
    </tr> 
Copy after login

就是把各个字段设置为 []行程数组上传,处理代码为:
<?php  <br />
$db_name="test";  <br />
$table_name="co_arr";  <br />
$connection= @mysql_connect("localhost","root","smtroot") or die(mysql_error());  <br />
$db= @mysql_select_db($db_name,$connection) or die (mysql_error());  <br />
mysql_query("SET NAMES GBK");  <br />
 <br />
echo "<pre class="brush:php;toolbar:false">";  <br />
print_r($_POST["name"]);  <br />
print_r($_POST["age"]);  <br />
print_r($_POST["position"]);  <br />
 <br />
//计算传递过来的文本框的行数  <br />
$arr_Length = count($_POST["name"]);  <br />
 <br />
$name_arr = $_POST["name"];  <br />
$age_arr = $_POST["age"];  <br />
$position_arr = $_POST["position"];  <br />
 <br />
/*循环插入语句*/ <br />
for($i=0;$i<$arr_Length;$i++){  <br />
    $sql = "insert into co_arr(name,age,position)";  <br />
    $sql .= "values('$name_arr[$i]','$age_arr[$i]','$position_arr[$i]')";  <br />
    $query = @mysql_query($sql,$connection) or die(mysql_error());  <br />
    //echo $_POST["name"][$i]."<br>";  <br />
}  <br />
?> 
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template