PHP将表插入数据库

时间:2016-04-22 03:55:05

标签: php insert html-table

我有一个从.csv文件创建的数据库表。我使用php编辑内容以匹配我的数据库中的新表。我现在需要将新表插入新数据库。我在表格和提交按钮周围添加了表单标签,当我提交它时,我只获得表格的1或2个随机行。我猜我可能需要一段时间或一个for循环,但我无法弄清楚如何写它。任何援助将不胜感激。

这是发布到db

的代码
if(isset($_POST['submit'])){

$mr = $_POST['mr'];
$mrs = $_POST['mrs'];
$last_name = $_POST['last_name'];
$marital_status = $_POST['marital_status'];


$query = "INSERT INTO import_template(first_name, last_name, marital_status) VALUES('{$mr}', '{$last_name}', '{$marital_status}')";

$result = mysqli_query($connection, $query);

if(!$result){
    die("QUERY FAILED " . mysqli_error($connection));
}
if(!$connection){
echo "We are NOT connected";
}

$query = "INSERT INTO import_template(first_name, last_name, marital_status) VALUES('{$mrs}', '{$last_name}', '{$marital_status}')";

$result = mysqli_query($connection, $query);

  if(!$result){
    die("QUERY FAILED " . mysqli_error($connection));
}
if(!$connection){
echo "We are NOT connected";
}
}

这就是动态表的样子我只将输入添加到前几个进行测试,我还没有完成剩下的工作。

echo "<tr>";
echo "<td><input type='text' class='form-control' name='mr' value='$mr'></td>";
echo "<td><input type='text' class='form-control' name='last_name' value='$last_name'></td>";
echo "<td><input type='text' class='form-control' name='marital_status' value='$marital_status'></td>";
echo "<td>{$birthday}</td>";
echo "<td>Adult</td>";
echo "<td>{$anniversary}</td>";
echo "<td>{$mr_cell_phone}</td>";
echo "<td>{$home_phone}</td>";
echo "<td>{$mr_email}</td>";
echo "<td>{$street}</td>";
echo "<td>{$city}</td>";
echo "<td>{$state}</td>";
echo "<td>{$zip}</td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type='text' class='form-control' name='mrs' value='$mrs'></td>";
echo "<td><input type='text' class='form-control' name='last_name' value='$last_name'></td>";
echo "<td><input type='text' class='form-control' name='marital_status' value='$marital_status'></td>";
echo "<td></td>";
echo "<td>Adult</td>";
echo "<td>{$anniversary}</td>";
echo "<td>{$mrs_cell_phone}</td>";
echo "<td></td>";
echo "<td>{$mrs_email}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";

1 个答案:

答案 0 :(得分:0)

你可以这样做。我已经使用了三个字段,例如任务,代理和注释。您可以根据表格行添加:

<强> main.php

<form id="first" action="insert_values.php"  method="post">


<table>
<tr>
<td>Task</td>
<td>
    <input type="text" name="task">
    </td>
    </tr>

    <tr>
    <td>Agent</td>
    <td>
   <input type="text" name="agent">
    </td>
    </tr>
    <tr>
    <td>Note</td>
    <td><input type="text" name="Note"></td>
    </tr>

    </table>
     <input type="submit" value="save" name="save">
    </form>

<强> insert_values.php

<?php
error_reporting( error_reporting() & ~E_NOTICE );
session_start();
include 'connect.php';


 $task=$_POST['task'];
  $agent=$_POST['agent'];


  $Note=$_POST['Note'];




$result=mysql_query("insert into table_name(task,agent,t_note) values('$task','$agent','$Note')");



if($result)
{   
$msg="Insert Successfully!!";
echo "<script>alert('Insert Successfully');document.location='main.php'</script>";
?>

<?php
}
else
{

$errmsg=" Insert not success!!";
      echo "<script>alert('Insert Not Successfully');document.location='main.php'</script>";

}

?>