Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't figure out how to track down an error in $.ajax/PHP/MySQL insert
    text
    copied!<p>I am having a great amount of difficulty tracking down the cause of an error on an $.ajax/PHP/MySQL database insert.</p> <p>Here's the jQuery side:</p> <pre><code>function SaveNewBranch(newBranchName, dataString) { console.log('In function SaveNewBranch(newBranchName, dataString). Value of newBranchName: ' + newBranchName + '. Value of dataString: ' + dataString + '.'); $.ajax({ type: 'POST', url: '../scripts/branchAdmin.php', data: dataString, dataType: 'json', success: function(newBranchID) { console.log('from server: ' . newBranchID); // Clear the page ClearPageForNewSelection(); PopulateBranchDropdown(newBranchID); }, error: function(xhr, status, error) { console.log('datastring: ' + dataString); alert('An error occurred while attempting to save the branch to the database. jqXHR object: ' + xhr + '. Status: ' + status + '. Error message: ' + error + '. An error log with more details has been created on the server. If the error persists, contact your site administrator.'); } }); } // End Save New Branch </code></pre> <p>Here's the PHP code:</p> <pre><code>if(isset($_POST['newBranchName']) &amp;&amp; isset($_POST['newBranchAddr1']) &amp;&amp; isset($_POST['newBranchAddr2']) &amp;&amp; isset($_POST['newBranchCity']) &amp;&amp; isset($_POST['newBranchState']) &amp;&amp; isset($_POST['newBranchZip']) &amp;&amp; isset($_POST['newBranchPhone']) &amp;&amp; isset($_POST['newBranchFax']) &amp;&amp; isset($_POST['newBranchUrl'])) { require_once('dbConnect.php'); $log-&gt;lwrite('name: ' . mysqli_real_escape_string($dbc,$_POST['newBranchName']) . ', addr1: ' . mysqli_real_escape_string($dbc,$_POST['newBranchAddr1']) . ', addr2: ' . mysqli_real_escape_string($dbc,$_POST['newBranchAddr2']) . ', city: ' . mysqli_real_escape_string($dbc,$_POST['newBranchCity']) . ', state: ' .$_POST['newBranchState'] . ', zip: ' . mysqli_real_escape_string($dbc,$_POST['newBranchZip']) . ', phone: ' . mysqli_real_escape_string($dbc,$_POST['newBranchPhone']) . ', fax: ' . mysqli_real_escape_string($dbc,$_POST['newBranchFax']) . ', url: ' . mysqli_real_escape_string($dbc,$_POST['newBranchUrl'])); $_POST['newBranchState'] == '0' ? $newBranchState = '' : $newBranchState = strtoupper(mysqli_real_escape_string($dbc,$_POST['newBranchState'])); $queryInsertNewBranch = "INSERT INTO branches (name, address1, address2, city, state, zipCode, phone, fax, url) VALUES ('" . mysqli_real_escape_string($dbc,$_POST['newBranchName']) . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchAddr1']) . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchAddr2']) . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchCity']) . "', '" . newBranchState . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchZip']) . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchPhone']) . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchFax']) . "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchUrl']) . "')"; $log-&gt;lwrite('new branch insert: ' . $queryInsertNewBranch); $resultInsertNewBranch = @mysqli_query($dbc, $queryInsertNewBranch); ... </code></pre> <p>I've confirmed that good data is getting passed to the php script via the console output of "dataString".</p> <p>Output to the console for a test insert:</p> <pre><code>newBranchName: A Test Branch, datastring: newBranchName=A Test branch&amp; newBranchAddr1=123 StateSt.&amp;newBranchAddr2=#123&amp; newBranchCity=Anywhere&amp;newBranchState=MN&amp;newBranchZip=12343&amp; newBranchPhone=555-555-1212&amp;newBranchFax=555-555-2121&amp;newBranchUrl=minnesota </code></pre> <p>I also confirmed that the php script is getting good data by checking the first $log->lwrite line in the php script.</p> <p>Output to the log for the same insert test: </p> <pre><code>new branch insert: INSERT INTO branches (name, address1, address2, city, state, zipCode, phone, fax, url) VALUES ('A Test Branch', '123 State St.', '#123', 'Anywhere', 'MN', '12343', '555-555-1212', '555-555-2121', 'minnesota') </code></pre> <p>The last $log->lwrite entry (new branch insert) NEVER gets written to the log file.</p> <p>Also on the jQuery side the error function is getting fired, displaying the alert box. The values of the three objects there are:</p> <pre><code>jqXHR object: [object Object] Status: parsererror Message: Unexpected token e </code></pre> <p>No matter what I enter into the form that takes the data, I always get the same result: the INSERT never runs and the alert box in the error function shows - with the same message every time.</p> <p>I've gone over and over many times looking at every detail of the code, looking for an erroneous space or single quote where a double quote should be, etc. And I can't for the life of me find what the issue is!</p> <p>If the jqXHR object may hold a clue, I don't know how to get the elements of that. How do you do that?</p> <p>I (and my client) would be IMMENSELY appreciative of any help I could get in figuring this out!</p> <p>Thanks for any help...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload