How to Insert Input Array Form Fields into a Database Table using PHP -
i have 2 php files. 1 hold form/table data , have requesters input form data , other php file process form data , have insert information inputted on main page database. have connection info right left out privacy reasons. question is, how or should submit information form , make array string before submitting target table? when try insert is, returns error message saying syntax wrong session variables being read because returns input fields values. below copy of have tried far. i'm going have 10 initial fields option add more rows but, sake of length, shortened table 2 initial rows.
here main form page: santable.php
<?php session_start(); // if initial trip, set $_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"> </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 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 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']); ($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 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 retrieve values pulled input fields on html page in php page process insert query?
im no senior guy, see 2 main not clear situations start in code:
1) dont see how able send insert.php more 1 form @ time or how want set multiple sessions simultaniously... line
$number = count($_session['host']);
...with loop unecessary. if dont need sessions, set post stuff right variables start testing...
2) apparently not way send data through jquery.ajax. have .serialize form , when receive in insert.php, convert can use. try documentation see better:
https://api.jquery.com/serialize/ http://api.jquery.com/jquery.ajax/
hope works :)
Comments
Post a Comment