Jump to content

[SOLVED] Inserting using an array?


Mr Chris

Recommended Posts

Hello All,

 

I've spent most of last night and this morning searching google and the search facility on this site, but can't find the answer.  So I think it's time to ask for some help.

 

I'm building a site for statistical data for a five aside football team.  So my team has five players

 

- goalkeeper

- player_two

- player_three

- player_four

- player_five

 

and each match we play in is given a distinct match_id.  For each player I want to record the following data:

 

position (ie goalkeeper, player_two)

name

goals

 

So when I come to submitting my data I want my table to eventually look like this after submit is hit on my form:

 

statistics.jpg

 

Now i've started building the design for the addition of this data, but got stuck.  I have two questions

 

1) How can I specify the position of each player that is posted and get that data in the database?

2) When I hit submit 5 rows are saved, but with no data in them?

 

Can anyone help

 

Thank you

 

<?php

if(isset($_POST['Submit']))
{
  $name = $_POST['name'];
  $goals = $_POST['goals'];
  $match_id=mysql_insert_id();

if(!isset($_GET['match_id']))
  {
    foreach($_REQUEST['r'] as $row) 
  {
      $result = mysql_query("Insert into player_stats(name,goals,match_id)   
                             values('$name','$goals','$match_id')");
  }
  }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Player stats</title>
</head>

<body>
<form name="statistics" method="post" action="">
<table width="100%">
  <tr>
    <td width="33%">Player</td>
    <td width="33%">Name</td>
    <td width="33%">Goals</td>
  </tr>
  <tr>
    <td width="33%">Goalkeeper:</td>
    <td width="33%"><input name="r[1]name" type="text"></td>
    <td width="33%"><input name="r[1]goals" type="text" /></td>
  </tr>
  <tr>
    <td width="33%">Player Two</td>
    <td width="33%"><input name="r[2]name" type="text" /></td>
    <td width="33%"><input name="r[2]goals" type="text" /></td>
  </tr>
  <tr>
    <td width="33%">Player Three</td>
    <td width="33%"><input name="r[3]name" type="text" /></td>
    <td width="33%"><input name="r[3]goals" type="text" /></td>
  </tr>
  <tr>
    <td width="33%">Player Four</td>
    <td width="33%"><input name="r[4]name" type="text" /></td>
    <td width="33%"><input name="r[4]goals" type="text" /></td>
  </tr>
  <tr>
    <td width="33%">Player Five</td>
    <td width="33%"><input name="r[5]name" type="text" /></td>
    <td width="33%"><input name="r[5]goals" type="text" /></td>
  </tr>
</table>
<input type="submit" name="Submit" value="Submit"/>
</form>
</body>
</html>

Link to comment
Share on other sites

change your form to look like

<tr>
    <td width="33%">Goalkeeper:</td>
    <td width="33%"><input name="r[Goalkeeper][name]" type="text"></td>
    <td width="33%"><input name="r[Goalkeeper][goals]" type="text" /></td>
  </tr>
  <tr>
    <td width="33%">Player Two</td>
    <td width="33%"><input name="r[Player_Two][name]" type="text" /></td>
    <td width="33%"><input name="r[Player_Two][goals]" type="text" /></td>
  </tr>
  etc,

and your insert cide

f(isset($_POST['Submit']))
{
  $match_id=mysql_insert_id();
  if(!isset($_GET['match_id']))
  { $match_id = $_GET['match_id'];
    foreach($_REQUEST['r'] as $position => $row) 
    
  {   $row = implode("', '",$row);
      $result = mysql_query("Insert into player_stats(position,name,goals,match_id)   
                             values('$position','$row','$match_id')");
  }
  }
}

not tested

Link to comment
Share on other sites

First of all you need a hidden form field to include the position:

 

 

<tr>
    <td width="33%">Goalkeeper:</td>
    <td width="33%"><input type="hidden" name="position[0]" value="Goalkeeper"><input name="name[0]" type="text"></td>
    <td width="33%"><input name="goals[0]" type="text"></td>
</tr>
<tr>
    <td width="33%">Player Two:</td>
    <td width="33%"><input type="hidden" name="position[1]" value="Player2"><input name="name[1]" type="text"></td>
    <td width="33%"><input name="goals[1]" type="text"></td>
</tr>
...etc

 

 

and then when the form is submitted you will need to cycle through the array:

 

<?php
if(isset($_POST['submit'])) {
  //get last insert id  
  $match_id=mysql_insert_id();

  //build SQL statements and execute each one
  for($i=0;$i<5;$i++) {
    $sql = "insert into into player_stats(position, name,goals,match_id) values('" . $_POST['position'][$i] . "','" . $_POST['name'][$i]  . "','" . $_POST['goals'][$i]  . "','$match_id')";
    @mysql_query($sql) or die("<tt>problem with SQL: $sql</tt>\n");
  }
}   
?>

 

Something like that should do the trick - note this has not been tested, but it should give you the idea...

 

:)

Link to comment
Share on other sites

The reason there is no data being saved is that your not addressing the $_POST fields correctly. You've named them in this format.... r[2]name , but are trying to retrieve that data by  $name = $_POST['name'];

 

In order to get the field called r[2]name, you need to address it like this.

 

$name=$_POST[r[2]name];

 

I would suggest this....

 

<?php

if(isset($_POST['Submit']))
{
   $match_id=mysql_insert_id(); // I assume this comes from a previous insert query??
   
  $gk_name=$_POST['goalkeeper_name'];
  $gk_goals=$_POST['goalkeeper_goals'];
  
  $p2_name=$_POST['player2_name'];
  $p2_goals=$_POST['player2_goals'];
  
  $p3_name=$_POST['player3_name'];
  $p3_goals=$_POST['player3_goals'];
  
  $p4_name=$_POST['player4_name'];
  $p4_goals=$_POST['player4_goals'];
  
  $p5_name=$_POST['player5_name'];
  $p5_goals=$_POST['player5_goals'];
  
if(!isset($_GET['match_id']))
{
	  
		$query="Insert into player_stats(name,goals,match_id)VALUES
					 ('$gk_name','$gk_goals','$match_id'),
					 ('$p2_name','$p2_goals','$match_id'),
					 ('$p3_name','$p3_goals','$match_id'),
					 ('$p4_name','$p4_goals','$match_id'),
					 ('$p5_name','$p5_goals','$match_id')";
	$result = mysql_query($query);
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Player stats</title>
</head>

<body>
<form name="statistics" method="post" action="">
<table width="100%">
  <tr>
    <td width="33%">Player</td>
    <td width="33%">Name</td>
    <td width="33%">Goals</td>
  </tr>
  <tr>
    <td width="33%">Goalkeeper:</td>
    <td width="33%"><input name="goalkeeper_name" type="text" id="goalkeeper_name"></td>
    <td width="33%"><input name="goalkeeper_goals" type="text" id="goalkeeper_goals" /></td>
  </tr>
  <tr>
    <td width="33%">Player Two</td>
    <td width="33%"><input name="player2_name" type="text" /></td>
    <td width="33%"><input name="player2_goals" type="text" id="goalkeeper_goals" /></td>
  </tr>
  <tr>
    <td width="33%">Player Three</td>
    <td width="33%"><input name="player3_name" type="text" id="player2_name" /></td>
    <td width="33%"><input name="player3_goals" type="text" id="player2_goals" /></td>
  </tr>
  <tr>
    <td width="33%">Player Four</td>
    <td width="33%"><input name="player4_name" type="text" id="player2_name" /></td>
    <td width="33%"><input name="player4_goals" type="text" id="player2_goals" /></td>
  </tr>
  <tr>
    <td width="33%">Player Five</td>
    <td width="33%"><input name="player5_name" type="text" id="player2_name" /></td>
    <td width="33%"><input name="player_goals" type="text" id="goalkeeper_goals" /></td>
  </tr>
</table>
<input type="submit" name="Submit" value="Submit"/>
</form>
</body>
</html>

 

Obviously not tested as I don't have this database structure set up, but it should work.

 

First of all you need a hidden form field to include the position:

 

Why???

 

Hope this helps

Link to comment
Share on other sites

Oops, I missed the position part... it was really late. No the $_POST is right as the form's method is POST so you have to use POST to grab those variables. POST sends through the header, and GET sends through the URL, there is nothing being passed through the URL.

 

Also there should really be a mysql_real_escape_string() around all the POST fields as you should sanitize them before using them in a query. e.g.

$p2_goals=mysql_real_escape_string($_POST['player2_goals']);

 

CODE UPDATE:

 

$query="Insert into player_stats(name,position,goals,match_id)VALUES
					 ('$gk_name','goalkeeper','$gk_goals','$match_id'),
					 ('$p2_name','player_two','$p2_goals','$match_id'),
					 ('$p3_name','player_three','$p3_goals','$match_id'),
					 ('$p4_name','player_four','$p4_goals','$match_id'),
					 ('$p5_name','player_five','$p5_goals','$match_id')";
	$result = mysql_query($query);

 

There may be a few more lines of code, but this runs 1 query rather than 5, so choose your trade off... increased server load or more typing / not so pretty code.

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.