Jump to content

Issue with arrays -- saving multiple coordinates


Jax2

Recommended Posts

Hi all,

 

I have a list of map coordinates that need to be saved to a database -- the coordinates look similar to this:

 

1,3  or 129,45 ...etc

 

I have set up a text entry for each set of coordinates (There will be up to 10 total per person) and I am trying to add them to my database.

 

Here is the code for the first section (Data Entry I'll call it)

 

<td align="center">
<input type="text" name="coord[]" size=8>
	</td>
	<td align="center">
<input type="text" name="coord[]" size=8>
	</td>

 

I have 10 of those total.

 

Now, on the actual database code, which I am trying to store it into the database with, I am using:

for ($i = 0; $i < 10; $i++)
{
              if ($coord[$i] != "")
	{
                 $sql = "insert into coords (playerID, coord) values ('$playerID', 'coord[$i]')";
                 $result = mysql_query($sql ,$con);
   		}
}

 

The code inserts the correct number of records, along with the correct playerID, the problem is, instead of coords like 12,140, I am getting the following:

 

coord[0]

coord[1]

coord[2]

coord[3]

...etc, instead of

121,123

141,22

192,102

...etc

 

I am not sure why this is happening. I have made sure to call my variable ( $coord=$_POST['coord'];

 

so it should be showing up correctly. I am using the same code in another script for ingredients in recipes and it works perfectly fine.

 

Any suggestions would be helpful! Could it be the , inbetween the numbers that is causing the problem?

 

 

Link to comment
Share on other sites

In this line:

<?php
$sql = "insert into coords (playerID, coord) values ('$playerID', 'coord[$i]')";
?>

coord[$i] is a literal, since you forgot the "$" in front of it.

<?php
$sql = "insert into coords (playerID, coord) values ('$playerID', '$coord[$i]')";
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.