Jump to content

PHP Array - Insert into MySQL Database


TOMMURRAY

Recommended Posts

Hi All

 

I have a piece of code in the form of an array, I am wanting to insert this information into a MySQL database into one row. At this present time I wish to insert only the score field.

 

The database has a column for each of the Hole's (1 to 18), the value is that of the score input.

 

Can anyone point me in the right direction?

 

Thank you in advance

 

Tom   

 


for ($i = 1; $i < 19; $i++)
{
echo "<tr>\n";
echo "<td>Hole #{$i}</td>\n";
echo "<td><input type=\"text\" size=\"4\" name=\"score[]\" /></td>";
echo "<td><input type=\"text\" size=\"4\" name=\"si[]\" /></td>";
echo "<td><input type=\"text\" size=\"4\" name=\"par[]\" /></td>";
echo "</tr>\n";
}

Link to comment
https://forums.phpfreaks.com/topic/50477-php-array-insert-into-mysql-database/
Share on other sites

Might be easier to just use a single field for all the holes, and put the data there as a single string.

For instance, in hole 1 you have a score of 3, then 8 for hole 2 and 5 for hole 3, it would be put in the database as 3,8,5 and you could use the explode() function to make it an array again.

 

Right now, you'd simply have to do it like this:

 

$scores = $_POST["score"];

$hole_1_score = $scores[0];
$hole_2_score = $scores[1];
$hole_3_score = $scores[2];
//etc...

 

The same would apply for your query, having to write down each separate field name.

Hi thank you for your reply, sorry if I sound thick are you implying that all i need to do is along the lines of this?

 

insert into "table" (hole_1, hole_2, hole_3) Values ($hole_1_score, $hole_2_score, $hole_3_score )

 

I'm new to arrays, so I dont have any real understanding of them.

 

Thank you for your help.

 

Tom

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.