zero-perfoliate
zero-perfoliate

Author Topic: PHP - INSERT using multiple SELECT statements with MySQL  (Read 2363 times)

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
PHP - INSERT using multiple SELECT statements with MySQL
« on: March 08, 2009, 04:15:33 PM »
Hi there,

This is my first post and I quite new to PHP so this is probably quite a common problem that is easily sorted so bare with me please if I'm being too much of a noob.

I have a small 5 a side football league website and Im trying insert data into the league table. All of the queries below return the correct results when I run them individualy in MySQL and when a echo the results, however when I try the INSERT statement ($UPDATE) every entry shows up in the league_table as 0 apart from $ID and $NAME because they are currently text.

To me this looks like the correct syntax but im obviously missing something and your opinion on the subject would be most appreciated.

$ID = mysql_query("SELECT team_id FROM team WHERE team_name = 'Avengers'");
$NAME = mysql_query ("SELECT team_name FROM team WHERE team_name = 'Avengers'");
$P = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name='Avengers'");
$W = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name = 'Avengers' AND result = 'Win' ");
$D = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name ='Avengers' AND result='Draw'");
$L = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name ='Avengers' AND result='Lost'");
$GF = mysql_query ("SELECT SUM(goals_scored) FROM performance WHERE team_name='Avengers'");
$GA = mysql_query (" SELECT SUM(goals_scored) FROM performance WHERE NOT team_name = 'Avengers' AND game_id =6 ");

$UPDATE = mysql_query ("INSERT INTO league_table (team_id, team_name, played, won, drawn, lost, goals_for, goals_against)
                       VALUES ('$ID', '$NAME', '$P', '$W', '$D', '$L', '$GF', '$GA'");

(PS - I will be changing the team name in the queries to a SESSION ID later once I have this figured)

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #1 on: March 08, 2009, 04:38:13 PM »
the insert statements works for inserting new data not for updating existing data for that you want update statements like this:

Code: [Select]
$UPDATE = mysql_query("UPDATE leage_table SET team_name='$NAME', played='$P', won='$W', drawn='$D', lost='$L', goals_for='$GF', goals_against='GA' WHERE team_id='$ID'");

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #2 on: March 08, 2009, 04:41:31 PM »
Sorry I wasn't making myself clear at this stage I just want to add those values to the table I will be working updating later as at the moment there is only one false record in the table. Sorry for any misconception

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #3 on: March 08, 2009, 04:56:04 PM »
yeah got the wrong end of it.

Have you tired just echoing out the values instead of inserting into a db to see the values of each var?

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #4 on: March 08, 2009, 05:03:22 PM »
Yeah for each var i used the mysql_fecth_array like below and it deffinately shows the correct values as I've tested it many times:

while($row = mysql_fetch_array($W)){
   
    echo "<p>The Team has won: " .$row['COUNT(team_name)'] . " Games</p>";   
   
}

It's just left me a bit puzzeled as the syntax and logic looks fine

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #5 on: March 08, 2009, 05:33:26 PM »
I agree it looks fine can you post the full code your using?

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #6 on: March 09, 2009, 04:50:23 AM »
Hi there sorry for late reply, heres the code im using for this page:


<?php # update_league_table.php

include ('includes/config.inc.php');

$page_title = 'Update League Table';

include ('includes/header.html');

if(isset($_POST['submitted'])) {
   
   require_once (MYSQL);
 

$ID = mysql_query("SELECT team_id FROM team WHERE team_name='Avengers'");
$P = mysql_query ("SELECT COUNT(team_name) FROM performance
WHERE team_name='Avengers'");
$W = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name = 'Avengers' AND result = 'Win' ");
$D = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name ='Avengers' AND result='Draw'");
$L = mysql_query ("SELECT COUNT(team_name) FROM performance WHERE team_name ='Avengers' AND result='Lost'");
$GF = mysql_query ("SELECT SUM(goals_scored) FROM performance WHERE team_name='Avengers'");
$GA = mysql_query (" SELECT SUM(goals_scored) FROM performance WHERE NOT team_name = 'Avengers' AND game_id =6 ");

   
    if (mysqli_affected_rows($dbc) == 0) {

$UPDATE = mysql_query ("INSERT INTO league_table (team_id, team_name, played, won, drawn, lost, goals_for, goals_against)
                    VALUES ('1','Avengers' ,'$P', '$W', '$D', '$L', '$GF', '$GA')");

    }else{

        echo '<p>The information could not be entered into the database<p>';

    }
   
   
mysqli_close($dbc);

}

?>


<h1>Update</h1>

<form action="update_league_table.php" method="post">
       
       
    <div align="center"><input type="submit" name="submit" value="Update" /></div>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php


    include ('includes/footer.html');



?>

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #7 on: March 09, 2009, 09:05:50 AM »
again can't see anything wrong with your code as long as each var has a value added to them they should go into the database.

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #8 on: March 09, 2009, 09:24:40 AM »
Yeah that's what I thought, well at least you agree the syntax is correct I'll start looking at lower end things that could be causing 0's to be inserted rather than me just getting it wrong. Any way thanks for your help and if I get it sorted I'll post it up here.

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #9 on: March 09, 2009, 02:34:58 PM »
I recently got an error message saying that the query was empty, however I fixed it without fixing the root problem so I can't post it up here sadly. Apart from running the queries through MySQL and printing the query results in PHP how else would I check that the vars have the query results assigned to them?

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #10 on: March 09, 2009, 02:38:12 PM »
I would do it via php and put the vars in a die command to see what is outputted.

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #11 on: March 09, 2009, 02:45:37 PM »
Hi, thanks again for your input but even with the die command in the query still runs but inserts 0's instead of the required values.

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #12 on: March 09, 2009, 02:56:14 PM »
what about using print_r();

Offline chris

  • PHP Workers
  • **
  • Posts: 10
  • Karma: +0/-0
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #13 on: March 09, 2009, 03:07:22 PM »
Right I used print_r($GF) which returned "Resource id #11". $GF works out the goals for, which should return 3. Any ideas? Thanks again btw and the query for $GF is :

SELECT SUM(goals_scored) FROM performance
WHERE team_name='Avengers';

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: PHP - INSERT using multiple SELECT statements with MySQL
« Reply #14 on: March 09, 2009, 03:25:08 PM »
just a thought but it looks like the result is coming back as true/false on/off 1/0

so how about doing a little different by acually trying to pull data from the query then adding that to a var and then submitting it to the database. Here's an example for the team_id

Code: [Select]
<?php
$GetID 
mysql_query("SELECT team_id FROM team WHERE team_name='Avengers'");
$row mysql_fetch_object($GetID);
$ID $row->team_id;
?>


 

zero-perfoliate