Jump to content

PLEASE HELP!


patsman77

Recommended Posts

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

 

 

 

 

Link to comment
Share on other sites

Hello and thanx for the reply.

 

If i take out the WHILE statement at the end of code, I can get it to UPDATE or INSERT 1 record from what it collects above ...as it is right now, this is a standings type page for sports. The page takes the values from each user and prints them on a page, which works fine, it calculates if the values are a winner, a loser , or a tie. Which it does correctly... then when it get to the insert the calculated values, it will only update 1 value, the first one it comes across with the WHILE statement. If i take the WHILE statement out, it will UPDATE or INSERT only 1 value. The print_r feature shows on the screen that it is looping through, but only for one user_name..it sees there are 3 records, which is correct, but gives it for only 1 user. I hope this helps you understand as this is really bugging me!! LOL

 

Thanx,

 

Patsman77

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

I actually changed user_name to Name in Standings table so there wouldn't be a conflict. So when it does the Select * from Standings it would be pulling the Name field and the values associated with it.....and the $row[user_name] value comes from the PICKSHEET table....

 

Hope this helps

 

Patsman77

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.