Jump to content

Help with getting variable data to update in table


harlequeen

Recommended Posts

Hi

 

I'm trying to do multiple updates to a results table.  I've no real idea of what I'm doing except that I'm trying to work it out using info I can find online.

 

I am very confused by all of this.  Could someone have a look at my code and tell me what it is I'm doing wrong please.

 

<?php include('myconnect.php'); 

$team_id=$_GET['team_id'];
$teamName=$_GET['teamName'];
$Points=$_GET['Points'];


//loop through and update table
for($i=0; $i<2;$i++)
{
$team_id[$i]=($team_id[$i]);
$teamName[$i]=($teamName[$i]);
$Points[$i]=($Points[$i]);


//$query =("UPDATE teams SET Points='$Points[$i]' WHERE team_id=$team_id[$i])"
$query =("UPDATE teams SET Points=$Points[$i] WHERE team_id=team_id[$i]");
}
$result=mysql_query($query);
if ($result) {
echo "<b> Gig successfully Added</b>";
}//end then
else {
echo "<b> Error, unable to post.</b>";
}//end else



mysql_close();
?>

 

My connection is being made to the database, but I don't know enough about the code to help myself.

 

I have used $_GET in my code so that I could see what was happening and this is what is being passed.

 

http://www.llanelli-ladies-darts.co.uk/addpoints.php?Points[]=1&Points[]=2&Points[]=1&submit=Submit

 

Any help would be appreciated.

 

Harlequeen

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi

 

I have the form submitting the values now as you can see below.

checkresults.php?team_id=+5&teamName=+Bristol+Hotel&points=4&team_id=+22&teamName=+Duke+of+Wellington&points=0&team_id=+40&teamName=+Pontyates+RFC&points=2

 

but the output on the action page is this:

0Team: Pontyates RFC

Points: Array

Team Id: 40

1Team: Pontyates RFC

Points: Array

Team Id: 40

2Team: Pontyates RFC

Points: Array

 

I have no idea how I get it to get each of the values that i'm passing so that I can update my table with them.

I have tried to read around this, but I can't seem to understand. 

 

I think I need a foreach or a while loop but I can't see how to do it.  I have looked at a lot of examples, but can't work out where my variables go.

 

Any help would be appreciated.

 

Harlequeen

Link to comment
Share on other sites

The output on the action page that you posted is basically the last set of values, repeated, because you are reusing the same names for each set of fields. I'm also guessing that you have hard-coded a value of 3 in a loop and that is why you got three copies of the same data. You also don't need to submit the team names because you already have the id for each team.

 

You need to use array names for the fields so that each set of data values will be available and the data can be processed in the php code using array functions. Using arrays will make ALL your code, both in the form and in the form processing, much simpler.

 

Once you change to using arrays, the $_GET data will look like this in your php code (without the teamName field) -


Array
(
    [team_id] => Array
        (
            [0] => 5
            [1] => 22
            [2] => 40
        )

    [points] => Array
        (
            [0] => 4
            [1] => 0
            [2] => 2
        )

)

 

Edit: You also have space characters (the + in the URL data) at the start of each team_id and teamName data value that you should find the cause of and eliminate.

 

 

Link to comment
Share on other sites

Hi

 

thanks for your help. You are right, I did hard code the '3' as I wanted to test what was happening. 

 

I don't understand how/where I should be using this structure.

 

Below is the code I use to get the information passed from the form to the resulting page.

How do I structure the code to get the result I want?

 

 

$teamName=$_GET['teamName'];
$Points=$_GET['Points'];
$team_id=$_GET['team_id'];


//loop through and update table
for($i=0; $i<3;$i++){
echo $i;


echo "<b>Team:</b> $teamName<br>";
echo "<b>Points:</b> $Points<br>";
echo "<b>Team Id:</b> $team_id<br>";

 

I don't know why these extra + are being added!

 

thanks

Harlequeen

Link to comment
Share on other sites

In the first post in this thread you were using an array name for the Points[] form field. All you need to do is make the team_id[] field an array name as well.

 

In your form processing code you should use a foreach loop to iterate over the $_POST['team_id'] array so that your code will operate for any number of values.

 

Your code is using an UPDATE query. This will change/edit the existing values in the table. It will not add a new set of scores. To do that you would need to use an INSERT query and you would need a column that uniquely identifies the different sets of scores, such as a date.

Link to comment
Share on other sites

Hi

I'm trying to get the $_GET array printed out by using a foreach loop as below.

 

$teamName=$_GET['teamName'];
$Points=$_GET['Points'];
$team_id=$_GET['team_id'];




foreach ($_GET as $key => $value) 
{
  echo $key . ":" . $value;
}//end foreach


 

I'm afraid I'm really struggling with this.  And this is the output.

 

team_id: 40teamName: Pontyates RFCpoints:2

 

I need the team names to show on the form as that is what I'll be using to identify the teams when the scores go in, much easier for a human to recognise names than number, I think.

 

I just don't understand this.  I'm trying to make my life as a darts league secretary easier but this is just beyond me I think.

 

Regards

Harlequeen

Link to comment
Share on other sites

You are only getting one set of values, the last set, because you are not using array names for your form fields.

 

Your form fields should have name attributes like -

 

The team_id (hidden) field - name='team_id[]'

The input Points input field - name='points[]'

 

Link to comment
Share on other sites

Here is the code I'm using from my form.  I thought that using the name='team_id[]'

would allow me to send the information through to the processing page.

 

This is what gets sent via the url.

 

checkresults3.php?team_id[]=+5&teamName[]=+Bristol+Hotel&points[]=4&team_id[]=+22&teamName[]=+Duke+of+Wellington&points[]=1&team_id[]=+40&teamName[]=+Pontyates+RFC&points[]=1

 

Please let me know if I'm doing it all wrong!  I'm trying to do it myself but am finding it very difficult to understand.  I have only done pages before where one value is being sent.

 

Regards

 

Harlequeen

 

?>
<table width="30%" align="center"  border="1">
<form name="inputpoints" method="GET" action="checkresults3.php">
<tr>
<td width=40%>
<input type="char" [color=purple]name='team_id[]'[/color]size="2" value=" <? echo $row['team_id'];?>"></td><td>
<input type="text" name='teamName[]' size="36" value=" <? echo $row['teamName']; ?>"></td>
<td>
<SELECT name='points[]'>
<OPTION value=1>1</OPTION> 
<OPTION value=2>2</OPTION>
<OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION>
<OPTION value=0>0</OPTION><?php echo $row['Points'];?>"> 
</select>
</td>

<?php
}
?>
<input type="submit" value="submit" action="submit">
</table>
</form>

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.