Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two php files. One is to hold the form/table data and have requesters to input form data and the other php file is to process the form data and have it insert the information that is inputted on the main page to a database. I have the connection info right so I left it out for privacy reasons. My question is, how do I or should I submit the information from the form and make the array into a string before submitting it to the target table? When I try to insert as is, it returns an error message saying my syntax is wrong but all the session variables are being read because it returns the input fields values. Below is a copy of what I have tried thus far. I'm going to have 10 initial fields with the option to add more rows but, for the sake of length, I shortened the table to two initial rows.

Here is my main form page: 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>

And here's my 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();
?>

How should I retrieve the values pulled from the input fields on the html page in the php page to process the insert query?

share|improve this question
    
Did you set proper datatype for database columns? –  Ali Sheikhpour 23 hours ago
    
also if CABLE_REQUEST_SAN_FIBER_DETAIL_ID is autoincrement, try writing default (with no quote mark) or 0 instead of leaving it empty. –  Ali Sheikhpour 23 hours ago

1 Answer 1

Im no senior guy, but I see two main not clear situations to start in your code:

1) I dont see how you are able to send to the insert.php more than one form at a time or how you want to set multiple sessions simultaniously... So that line

$number = count($_SESSION['host']);

...with the for loop is unecessary. If you dont need those sessions, I would set the post stuff right into the variables to start testing...

2) That apparently is not the way to send data through a Jquery.ajax. You have to .serialize the form and when you receive it in the insert.php, you convert to something you can use. Try this documentation to see it better:

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

hope that works :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.