Reputation:
I'm having a bit of trouble with a foreach loop in PHP. I'm trying to insert a bunch of values from a html form. The way the table in the form works is that the user is able to add a row if they want to. For whatever reason, the foreach loop is only inserting the first row of the table and after a few hours I can't seem to find a solution.
Below is the foreach loop:
$sql = "INSERT INTO map_input (user_id, point_desc, lat, lon) VALUES ";
//inserting into the map_input table
foreach($_GET['input_Lat'] as $i => $Lat)
{
//get values from form
$in_Desc = mysql_real_escape_string($_GET['input_Desc'][$i]);
$Lat = mysql_real_escape_string($Lat);
$Lon = mysql_real_escape_string($_GET['input_Lon'][$i]);
$sql = $sql."('$user_id', '$in_Desc', '$Lat', '$Lon]'),";
}
The value for each textbox in the form table is "input_Desc[]", "input_Lat[]", "input_Lon[]".
Any help would be greatly appreciated!
Edit:'Desc' has been changed to 'in_Desc' so that it doesn't clash with the reserved word. The semi-colon at the end of the concatenated sql statement has been changed to a comma. The result is still the same.
echo-ed the sql statement and this is the result INSERT INTO map_input (user_id, point_desc, lat, lon) VALUES ('4', 'Start', '53.066363', ' -6.293127]'),
**Edit:**HTML added below for the table and adding extra rows. Table/Form
<form action="mapInput.php" method="get">
<table id="gpsPoints">
<tr>
<th>Description</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>
<tr>
<td> <input type="text" name="input_Desc[]"> </td>
<td><input type="text" name="input_Lat[]" ></td>
<td><input type="text" name="input_Lon[]"></td>
</tr>
</table>
<br>
<br>
<input type="submit" value="Save Route" >
</form>
<br>
<br>
<button onClick="addRow()" >Add new point </button>
Function to add row:
function addRow()
{
var table = document.getElementById("gpsPoints");
var row = table.insertRow(2);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
//adding text boxes to each cell
var element1 = document.createElement("input");
element1.type = "textbox";
element1.name = "input_Desc[]";
cell1.appendChild(element1);
var element2 = document.createElement("input");
element2.type = "textbox";
element2.name = "input_Lat[]";
cell2.appendChild(element2);
var element3 = document.createElement("input");
element2.type = "textbox";
element2.name = "input_Lon[]";
cell3.appendChild(element3);
}//end function addRow()
</script>
EditThe values are definitely being passed in as they are in the url bar, however they are not going into the foreach loop and being inserted.
Upvotes: 2
Views: 2033
Reputation: 31
You are closing your request on sql concatenation on line :
$sql = $sql."('$user_id', '$Desc', '$Lat', '$Lon]');";
Replace ";" by ","
And add after loop :
$sql = substr($sql, 0, -1) . ";";
Upvotes: 2
Reputation: 515
you query string will end up looking like
INSERT INTO map_input (user_id, desc, lat, lon) VALUES ('user_id', 'desc1', 'lat1', 'lon1'); ('user_id', 'desc2', 'lat2', 'lon2');
Replace the ; at the end of the line
$sql = $sql."('$user_id', '$Desc', '$Lat', '$Lon]');"; to read $sql = $sql."('$user_id', '$Desc', '$Lat', '$Lon]'),";
also change
$sql = "INSERT INTO map_input (user_id, desc, lat, lon) VALUES ";
to be
$sql = "INSERT INTO map_input (user_id, `desc`, lat, lon) VALUES ";
this is because desc is a reserved word in mysql.
Upvotes: 2