1

So I just got done with figuring out some javascript issues with my form in this topic: How Can I Insert Multiple Rows Into a DB from my HTML Form with Multiple Rows Dynamically?

But since I had such a multi-part question I have been asked to create a new topic. Below is my new code that I am using where I am stuck and wanting to be able to submit my form, which has the capability if adding multiple rows, to my database and add those HTML rows to rows in the SQL database, respectively.

PREVIEW: http://cl.ly/image/0K0Z202O1Q3e/Screen%20Shot%202013-03-14%20at%203.00.19%20PM.png

HTML

<html>
<header>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" language="javascript" src="/jquery/js/jquery-1.9.1.js">      
</script>
<script src="http://www.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?   
key=Gmjtd%7Cluua2q6bn9%2C8g%3Do5-lzbsh"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<title>Central Office Company Survey</title>
</header>
<body onload="get_company_name();">
<h1>Central Office Company Survey</h1>
<div id='map' style='width:0px; height:0px; position:absolute'></div>

<input type="hidden" id="co_city" name="co_city">
<input type="hidden" id="co_state" name="co_state">
<input type="hidden" id="co_zipcode" name="co_zipcode">

<table>
<th>Company</th>
<th>CO Name</th>
<th>Get Current Location</th>
<th>Lat</th>
<th>Long</th>
<th>Address</th>
<tr>
    <td><select id="company_name" name="company_name" /></select></td>
    <td><input name="co_name" type="text"></td>
    <td><input type="submit" value="Get GPS" onclick="gpslookup();" /></td>
    <td><input id="co_lat" name="co_lat" type="text" /></td>
    <td><input id="co_long" name="co_long" type="text" /></td>
    <td><input id="co_address" name="co_address" type="text" /></td>
</tr>
</table>
<table id="tabledata">
<thead>
    <th>Select</th>
    <th>Border Location Name</th>
    <th>Cable Location</th>
    <th>Direction of Vault Wall</th>
    <th>Cable Type</th>
    <th>Cable Size (pairs)</th>
    <th>Cable Gauge</th>
    <th>Vertical(s) appeared on Verticals</th>
    <th>Approximate Number of Jumpers</th>
    <th>Are Protectors Still In?</th>
    <th>Metered Distance</th>
    <th class="comments">Central Office Comments</th>
</thead>
<tbody id="input"></tbody>
<tbody id="template">
    <tr>
        <td><input type="checkbox" /></td>
        <td><input name="border_location" type="text" /></td>
        <td><input name="cable_location" type="text" /></td>
        <td><input name="vault_direction" type="text" /></td>
        <td><input name="cable_type" type="text" /></td>
        <td><input name="cable_size" type="text" /></td>
        <td><input name="cable_gauge" type="text" /></td>
        <td><input name="vertical" type="text" /></td>
        <td><input name="jumpers" type="text" /></td>
        <td><input name="protectors" type="text" /></td>
        <td><input name="metered_dist" type="text" /></td>
        <td><input name="comments" type="text" /></td>
    </tr>
</tbody>
</table>
<button id="ActionAddRow">Add Row</button>
<button onclick="deleteRow(); return false;">Delete Row</button>
<button id="ActionSubmit">Submit</button>
</body>
</html>

scripts.js

//Button Functions
$(function () {
var addInputRow = function () {
    $('#input').append($('#template').html());
};

addInputRow();
$('#ActionAddRow').on('click', addInputRow);
$('#ActionSubmit').on('click', function () {
    var data = $('#input tr').map(function () {
        var values = {};
        $('input', $(this)).each(function () {
            values[this.name] = this.value;
        });
        return values;
    }).get();
    $.post('./php/upload_survey.php', {
        json: JSON.stringify(data),
        delay: 1
    }).done(function (response) {
        alert("Thank you. Your form has been submitted.");
        console.log(response);
    });
});
});

//Delete Selected Rows
function deleteRow() {
 try {
    var table = document.getElementById("tabledata");
    var rowCount = table.rows.length;

    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 3) {
                alert("Cannot delete all the rows.");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
}
catch(e) {
    alert(e);
}
};

upload_survey.php

//Assign passed parameters
    $values = json_decode($_POST['json']);

    $stringLogInfo =  "INFO: Location: $co_address CO Name = $co_name !!!\n\n";
    log_audit($logAuditFile, $logModule, $stringLogInfo);

//Parse and store the ini file, this will return an associative array
ini_set("display_errors", "1");
error_reporting(E_ALL);

//Insert Survey Form Information into the database
$sql="INSERT INTO table_name (company_name,...,...) 
VALUES ($values)";

mysql_query($sql) or die ("Unable to Make Query:" . mysql_error());

**So far every time I try to get this working the JS fires successfully, but I get an error in Chrome's Developer Toolbox: Unable to Make Query:Column count doesn't match value count at row 1

This refers to this function in js file: console.log(response);

4
  • gah. kill this code now, with fire, or at least nuke it from orbit. SQL injection vulnerabilities galore... Your error message is an SQL error, due to how (horribly badly) you're building your query. Commented Mar 14, 2013 at 20:01
  • i don't like it either, but I don't know any better, sadly. Commented Mar 14, 2013 at 20:07
  • stop being lazy and build the query the long way. you're just begging to get your server destroyed. Commented Mar 14, 2013 at 20:08
  • haha well I know it's a SQL error, but I want to know how to PROPERLY create a prepared SQL statement that will work the way I am wanting it to as explained above. So yes, I know it's SQL error and yes I don't claim I know much of anything with SQL, but I am learning. So far none of this works anyway, so if you're just referring to the ($values) in the VALUE area of the SQL statement, I am not wanting to do it that way that's just there as an example. Commented Mar 14, 2013 at 20:21

1 Answer 1

0

I think there is no such thing as batch inserts in PHP. There is no equivalent for JAVA's addBatch & executeBatch.

So the right way to do it is simply iteratation over array and insert single row with prepared statement.

Sign up to request clarification or add additional context in comments.

1 Comment

so essentially a for each structure with a SQL statement inside it??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.