如何使用PHP将输入数组表单字段插入数据库表

时间:2015-04-01 00:24:52

标签: php arrays insert

我有两个php文件。一种是保存表单/表格数据并让请求者输入表单数据,另一种php文件是处理表单数据并将其输入主页面的信息插入数据库。我有正确的连接信息所以我出于隐私原因将其保留了下来。我的问题是,我如何或应该从表单提交信息并将数组转换为字符串,然后再将其提交到目标表?当我尝试按原样插入时,它会返回一条错误消息,指出我的语法错误但正在读取所有会话变量,因为它返回输入字段值。以下是我迄今为止所尝试的内容的副本。我将有10个初始字段,可以选择添加更多行,但为了长度,我将表缩短为两个初始行。

这是我的主要表单页面:SANTable.php

<?php
session_start();
// If this is the initial trip, set up the $_SESSION.
if (!isset($_SESSION['initial_pass'])){
    $_SESSION['initial_pass']=true;
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SAN Fiber Request</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="wrapper">
<div id="maincontent">

<hr class="GreyHorizon">
<form id="sanFiber" action="insert.php" method="post">
<table class="frame" id="sanRequest" style="display: block;">
    <tr>
        <th style="border:none">&nbsp;</th>
        <th><b class="RedAsterik">*</b>Host Name</th>
        <th><b class="RedAsterik">*</b>Network Type</th>
        <th><b class="RedAsterik">*</b>Description</th>
        <th><b class="RedAsterik">*</b>Server Port Used</th>
        <th><b class="RedAsterik">*</b>Speed</th>
        <th><b class="RedAsterik">*</b>Mgmt Arms/Swings RT or LT</th>
        <th><b class="RedAsterik">*</b>Primary Function</th>
        <th><b class="RedAsterik">*</b>Comments</th>
    </tr>
    <tr>
        <td style="border:none"><img src="images/plus2.png" name="myImage" id="swap1" class="toggler" /></td>
        <!--25-->
        <td><input name="host[]" class="field" type="text" size="15" /></td>
        <!--25-->
        <td><select name="NETTYPEdropdown[]" id="NETTYPEdropdown">
            <option selected="selected" value=""></option>
            <option value="FiberChannel">Fiber Channel</option>
        </select></td>
        <!--50-->
        <td><select name="Descriptiondropdown[]" id="Descriptiondropdown" class="Description_dropdown">
            <option selected="selected" value=""></option>
            <option value="PriA">Primary Fabric A</option>
            <option value="PriB">Primary Fabric B</option>
            <option value="SecA">Secondary Fabric A</option>
            <option value="SecB">Secondary Fabric B</option>
            <option value="BackA">Backup Fabric A</option>
            <option value="BackB">Backup Fabric B</option>
            <option value="ilo-Manage">ilo-Management</option>
            <option value="UTCS265">UTCS Private 265</option>
            <option value="addOther">Other...</option>
        </select></td>
        <!--30-->
        <td><input name="PortUsed[]" class="field" type="text" id="PortUsed" size="15" /></td>
        <!--15-->
        <td><select name="Speeddropdown[]" id="Speeddropdown">
            <option selected="selected" value=""></option>
            <option value="1GB">1GB</option>
            <option value="4GB">4GB</option>
            <option value="8GB">8GB</option>
            <option value="10GB">10GB</option>
            <option value="16GB">16GB</option>
        </select></td>
        <!--15-->
        <td><select name="MGMTdropdown[]" id="MGMTdropdown">
            <option selected="selected" value=""></option>
            <option value="YesR">Yes - Right</option>
            <option value="YesL">Yes - Left</option>
            <option value="No">No</option>
        </select></td>
        <!--50-->
        <td><input name="primary[]" class="field" type="text" id="primary" size="25" /></td>
        <!--250-->
        <td><textarea name="comments[]" class="field" type="text" id="comments" size="20"></textarea></td>
    </tr>
</table>
<br>
<fieldset id="buttons">
    <input class="fsSubmitButton" type="submit" value="Review" /> 
    <input class="fsSaveButton" id="save" type="submit" value="Save" />
    <a href="#bottom" name="bottom"><input class="fsAddButton" type="button" value="Add New Row" /></a>
    <input class="fsNewHostButton" type="submit" value="Add New Host" />
</fieldset> 
</form>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#save").click(function(){
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "insert.php",
data: datastring,
cache: false,
success: function(){
    alert('success');
},
error: function(e){
    alert('nope.');
}
});
return false;
});
});
</script>

</body>
</html>

这是我的insert.php:

<?php 
if (session_id() == '') {
session_start();
}
else {
var_dump(session_id());
}

/*
   ***Connection info***
*/


//for SAN Fiber Request
$_SESSION['host'] = $_POST['host'];
$_SESSION['netType'] = $_POST['NETTYPEdropdown'];
$_SESSION['description'] = $_POST['Descriptiondropdown'];
$_SESSION['used'] =  $_POST['PortUsed'];
$_SESSION['speed'] = $_POST['Speeddropdown'];
$_SESSION['mgmt'] = $_POST['MGMTdropdown'];
$_SESSION['primary'] = $_POST['primary'];
$_SESSION['comments'] = $_POST['comments'];

    $number = count($_SESSION['host']);
    for ($i=0; $i<$number; $i++)
    {
        $hostno = $_SESSION['host'][$i];
        $netTypeno = $_SESSION['netType'][$i];
        $descriptno = $_SESSION['description'][$i];
        $usedno = $_SESSION['used'][$i];
        $speedno = $_SESSION['speed'][$i];
        $mgmtno = $_SESSION['mgmt'][$i];
        $primaryno = $_SESSION['primary'][$i];
        $commentsno = $_SESSION['comments'][$i];

        if ($_SESSION['host'][$i] <> '') { 
            $sql = "INSERT INTO cable_request_san_fiber_detail (CABLE_REQUEST_SAN_FIBER_DETAIL_ID, CABLE_REQUEST_ID, NETWORK_TYPE, HARDWARE_PORT_ID, SERVER_PORT, SPEED,  MANAGEMENT_ARM, PRIMARY_FUNCTION, CABLE_LABEL, SWITCH_HARDWARE_PORT_ID, DESCRIPTION, HARDWARE_ID, COMMENTS) VALUES (, 1, $netTypeno, ,$usedno, $speedno, $mgmtno, $primaryno, $hostno, ,$descriptno, , $commentsno)";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            }
            else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
    }

$conn->close();
?>

我应该如何检索从php页面的html页面上的输入字段中提取的值来处理插入查询?

2 个答案:

答案 0 :(得分:0)

我不是老手,但我看到你的代码中有两个主要的不明确的情况:

1)我没有看到你如何能够一次向insert.php发送多个表单,或者你想如何同时设置多个会话......那就行

$ number = count($ _ SESSION ['host']);

...使用for循环是不必要的。如果您不需要这些会话,我会将帖子设置为变量以开始测试......

2)显然不是通过Jquery.ajax发送数据的方式。你必须.serialize表单,当你在insert.php中收到它时,你转换为你可以使用的东西。试试这个文档可以更好地看到它:

https://api.jquery.com/serialize/ http://api.jquery.com/jquery.ajax/

希望有效:)

答案 1 :(得分:0)

经过一些试验和错误并仔细查看我的代码后,我通过更改查询的写入来使其工作。在我为自动增量字段添加默认值并正确组织我的引号以引用字符串变量后,一切正常。单引号和双引号可能非常棘手。这是我的解决方案:

$sql = "INSERT INTO cable_request_san_fiber_detail(CABLE_REQUEST_SAN_FIBER_DETAIL_ID,
CABLE_REQUEST_ID, NETWORK_TYPE, HARDWARE_PORT_ID, SERVER_PORT, SPEED,  MANAGEMENT_ARM, 
PRIMARY_FUNCTION, CABLE_LABEL, SWITCH_HARDWARE_PORT_ID, DESCRIPTION, HARDWARE_ID, 
COMMENTS) VALUES (default, 1, '".$netTypeno."','','".$usedno."','".$speedno."',
 '".$mgmtno."', '".$primaryno."', '".$hostno."','',
'".$descriptno."','','".$commentsno."')";