Forum Moderators: coopster

Message Too Old, No Replies

Please Help

new to php

         

patsman77

3:28 am on Dec 13, 2007 (gmt 0)

10+ Year Member



Hello All,

I am new to PHP and am trying to write a script to UPDATE/INSERT data to a database with a php script. As of right now, it will UPDATE records in the datbase when I load the page, but will not insert any new records

Here is a brief look at the code, hopefully someone can help clean it up for me so that it works...

// Get all the data from the "example" table
$result3 = mysql_query("SELECT * FROM picksheet WHERE Week=$cw ORDER BY user_name")
or die(mysql_error());

echo "<table border='1'<td class='v8'>" ;

echo "<tr><th>PoolMeister</th> <th>$pass[Favorite]<br>$pass[Spread]<br>$pass[Underdog]</th><th>$pass[Favorite2]<br>$pass[Spread2]<br>$pass[Underdog2]</th> <th>$pass[Favorite3]<br>$pass[Spread3]<br>$pass[Underdog3]</th> <th>$pass[Favorite4]<br>$pass[Spread4]<br>$pass[Underdog4]</th> <th>$pass[Favorite5]<br>$pass[Spread5]<br>$pass[Underdog5]</th> <th>$pass[Favorite6]<br>$pass[Spread6]<br>$pass[Underdog6]</th> <th>$pass[Favorite7]<br>$pass[Spread7]<br>$pass[Underdog7]</th> <th>$pass[Favorite8]<br>$pass[Spread8]<br>$pass[Underdog8]</th> <th>$pass[Favorite9]<br>$pass[Spread9]<br>$pass[Underdog9]</th> <th>$pass[Favorite10]<br>$pass[Spread10]<br>$pass[Underdog10]</th> <th>$pass[Favorite11]<br>$pass[Spread11]<br>$pass[Underdog11]</th> <th>$pass[Favorite12]<br>$pass[Spread12]<br>$pass[Underdog12]</th> <th>$pass[Favorite13]<br>$pass[Spread13]<br>$pass[Underdog13]</th> <th>$pass[Favorite14]<br>$pass[Spread14]<br>$pass[Underdog14]</th> <th>$pass[Favorite15]<br>$pass[Spread15]<br>$pass[Underdog15]</th> <th>$pass[Favorite16]<br>$pass[Spread16]<br>$pass[Underdog16]</th><th>TB</th><th> Total Wins</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result3 )) {
// Print out the contents of each row into a table

$Winner=0;
$Ties=0;
$Losses=0;
$Game1Winner=" ";
$Game2Winner=" ";
$Game3Winner=" ";
$Game4Winner=" ";

// if/then/else Example
if (($pass[FavScore]-$pass[Spread]) > ($pass[UndScore])) {
($Game1Winner="$pass[Favorite] -$pass[Spread]");
} elseif (($pass[FavScore] -$pass[Spread]) < ($pass[UndScore])) {
// notice that PHP uses "elseif" instead of Perl's "elsif"
($Game1Winner="$pass[Underdog] +$pass[Spread]");
} else {
($Game1Winner='Tie');
}

if (($pass[FavScore2]-$pass[Spread2]) > ($pass[UndScore2])) {
($Game2Winner="$pass[Favorite2] -$pass[Spread2]");
} elseif (($pass[FavScore2] -$pass[Spread2]) < ($pass[UndScore2])) {
// notice that PHP uses "elseif" instead of Perl's "elsif"
($Game2Winner="$pass[Underdog] +$pass[Spread2]");
} else {
($Game2Winner='Tie');
}

echo "<tr><td class='v8'>";
echo $row['user_name'];

//Game 1
if ($pass[FavScore]==0 and $pass[UndScore]==0)
{
echo '<TH bgcolor="white">';
}
elseif (($pass[FavScore] )==($pass[UndScore] +$pass[Spread]))
{
echo '<TH bgcolor="silver">';($Ties==$Ties++);

}
elseif (($pass[FavScore] +$pass[Spread])==$pass[UndScore])
{
echo '<TH bgcolor="silver">';($Ties==$Ties++);

}
elseif ($row['Game01']=="$Game1Winner")
{
echo '<TH bgcolor="lightgreen">';($Winner==$Winner++);
}
elseif ($row['Game01'] <>"$Game1Winner")
{
echo '<TH bgcolor="red">';($Losses==$Losses++);

}
echo $row['Game01'];

$sel4="select *from Standings";

$result4=mysql_query($sel4);


echo "$Winner";
while($array = mysql_fetch_array($result4)){
//print out the field values and values using print_r
echo '<pre>';
print_r($array);
echo '</pre>';
$num4 = mysql_num_rows($result4);

if ($num4!= 0)
{
$up="UPDATE Standings set W$cw='$Winner', L$cw='$Losses', T$cw='$Ties' where user_name='$row[user_name]'";
if (!mysql_query($up))
{
die('Error: ' . mysql_error());
}

}
else
{

$sql4="INSERT INTO Standings ( user_name, W$cw, L$cw, T$cw)
VALUES
('$row[user_name]','$Winner','$Losses','$Ties')";
if (!mysql_query($sql4))
{
die('Error: ' . mysql_error());
}
}
}
}


echo "</table>";

If there are records already in database, they will get updated, but nothing new will be inserted?

Any help would be great!

Patsman77

deMorte

8:38 am on Dec 13, 2007 (gmt 0)

10+ Year Member



Hello.

I think your problem could be related to your last if -clause.


$num4 = mysql_num_rows($result4);

if ($num4!= 0)
{
// UPDATE
}
else
{
// the script will only go here if the Standings -table is
// empty

// INSERT
}

I'm not sure what your script is supposed to do. When do you want to add a record to the table?

patsman77

10:09 am on Dec 13, 2007 (gmt 0)

10+ Year Member



Hello and Thanx for the reply...

This is what is actually happening..

This is a standings page for sports games.
It pulls the values from one table, calculates weather the "game" is a winner, loser, or tie. While calculating weather it is a winner loseer, or tie, it keeps a tally of how many wins losses, or ties each person has. Then as it is looping through, I want it to update the new information into another table, the values, user_name, winner, losses, and ties. I tried to show a little piece from each section thinking it would help you understand, but I guess not writing it yourself makes it hard to see whats going on...LOL If I take out the WHILE statement at the end of the code, then when I goto the page, it will UPDATE 1 record, if there are records in the database, or if there are no records, it will INSERT 1 record. This is why I tried to loop it, but it doesnt seem to get to the INSERT. I thank you for your attempt to help me here~!

Patsman77

patsman77

11:44 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



Well, I think you were definately correct with the num rows being the problem. I now have it to the point where if no records are in the Database, it will insert them into it. If some records are in the database, it will insert new records if there are any, but..... It wont update any current values, I get an error, being....

Error: Duplicate entry 'The Swammi' for key 1 if I set user_name to primary key, and if I take away the primary key feature, then it just adds more records with the same user_name...so I get duplicate records....

here is where I am with the code....

$sel4="select * from Standings";

$result4=mysql_query($sel4);

$num4 = mysql_num_rows($result4);

if (user_name==$row['user_name'])
{

$up="UPDATE Standings set W$cw='$Winner', L$cw='$Losses', T$cw='$Ties' where user_name='$row[user_name]'";
if (!mysql_query($up))
{
die('Error: ' . mysql_error());
}

}
else
{

$sql4="INSERT INTO Standings ( user_name, W$cw, L$cw, T$cw)
VALUES
('$row[user_name]','$Winner','$Losses','$Ties')";
if (!mysql_query($sql4))
{
die('Error: ' . mysql_error());
}
}
}


echo "</table>";

Thanx,

Patsman77

patsman77

2:48 am on Dec 14, 2007 (gmt 0)

10+ Year Member



I think my problem lies in the IF statement... I think it may be in the syntax...I have tried everything..

I am trying to do (IF user_name=='$row[user_name]') where user_name is pulled from the Standings table, and '$row[user_name]' is coming from the "PICKSHEET" table in the code above....I tried changing the STANDINGS table fied to Name incase there was a conflict there to now be...(IF Name=='$row[user_name]').. by doing this I was thinking that is this is a correct statement it would update, if not it would insert... but it doesnt update, but tries to insert... this is why I think it may be a syntax error becaue these 2 values should equal each other.... Anyone enlighten me on this?

Thanx,

Patsman77

deMorte

9:27 am on Dec 14, 2007 (gmt 0)

10+ Year Member



Variables in PHP must have a '$' before them. So your if-clause should be something like this:
if($user_name == $row["row_name"]) {

But I think there might be some issues besides the if -syntax. I think the script gets all the records from the table to $result -variable. This result doesn't actually contain the actual data from the table but a resource. You have to process this resource that PHP understands it. You can find more info in www.PHP.net and searching for mysql_query. (I think posting links here is denied).

I'm making a suggestion on how to handle the Standings -table data, and make the if -clause. I'm assuming that there are many rows in Standings table.


$result4 = mysql_query($sel4);
$i = 0;

// In this while -loop you get the data to a new table
// that is easier to handle because it is organised
while( $aTmpRow = mysql_fetch_array($result4) ) {
// Set the array
$aStand[$i]["User Name"] = $aTmpRow["user_name"];
// If you only need the user name data, you don't have
// to add the other columns
$aStand[$i]["Other Data"] = $aTmpRow["other_data"];
$i++;
}

$r=0;
$update = 0;
// This while goes thru all the records in the standings
// table
while($r<count($aStand)) {

if($aStand[$r]["User Name"] == $row["user_name"] ) {
// For a user (from $row) the script finds in the db
// it updates the data for that user
// SQL UPDATE
$update = 1;
}
$r++;
}

if( $update == 0 ){
// If the script doesn't find any users that match the
// Standings -user, it inserts a new record
// INSERT
}

You should note that this script inserts a new record only if the $row["user_name"] and $aStand[$r]["User Name"], do not match.

I hope this post at least gives some ideas how to fix your script.