I've got a PHP page that contains a javascript that gets values from a few different form elements, and uses them to form a MySql update statement. The page gets submitted to itself and PHP gets the update statement via $_POST[] inside stripslashes() and sends the query to the db, either setting the result to a variable or die, printing mysql_error()
The javascript works fine, throws no errors. The PHP works fine, throws no errors, except-
the resulting single update statement works: UPDATE data SET title = 'foo' WHERE id = '50';
but a multi update statement fails & returns a MySql syntax error: UPDATE data SET title = 'foo' WHERE id = '50'; UPDATE data SET title = 'bar' WHERE id = '51';
When I echo the multi update statement to the browser and manually copy & paste it into a shell session the statement works fine and throws no syntax errors.
Any ideas of what's going wrong?
The scripts are below for reference:
** bulletin board code is stripping forward slashes that exist in JS- the following line has forward slashes:
updateStatement += 'UPDATE data SET ' + dataToUpdate + ' = '' + newData + '' WHERE id = '' + imageToUpdate + '';';
<script type='text/javascript' language='Javascript'>
<!--
// the 'displayedImages' form contains 10 images each with a checkbox
// that contains the image's MySql id as its value
displayedImages = document.forms['displayedImages'].elements;
// the 'updateData' form contains a select list of MySql column names,
// a text input for inputting a new value and a hidden input to set a MySql query to
updateData = document.forms['updateData'].elements;
// get the MySql column name from the select list
dataToUpdate = updateData.elements['dataList'].value;
// get the value of new data from the text input
newData = updateData.elements['newData'].value;
// declare var to contain MySql query
updateStatement = '';
for (i=0;i<displayedImages.length;i++) {
if (displayedImages[i].type == 'checkbox' && displayedImages[i].checked == true) {
// get an image id from a checkbox
imageToUpdate = displayedImages[i].name;
// build the MySql query
updateStatement += 'UPDATE data SET ' + dataToUpdate + ' = \'' + newData + '\' WHERE id = \'' + imageToUpdate + '\';';
}
}
// set 'updateStatement' value to a hidden input 'mysqlQuery'
updateData.elements['mysqlQuery'].value = updateStatement;
// submit the 'updateData' form
updateData.submit();
// -->
</script>
<?php
function updateData() {
$this->update = stripslashes($_POST['mysqlQuery']);
$this->result = mysql_query($this->update) or die('Query failed: ' . mysql_error());
//echo $this->update;
}
?>