Forum Moderators: coopster

Message Too Old, No Replies

Cannot update my db

please help me :)

         

cougar

5:44 pm on Mar 2, 2005 (gmt 0)

10+ Year Member



Hi, it's me again :) Again I've written two files. In the first file you need to select the item you want to update and fill in the value that updates the column goals. Then after clicking submit query the information will be sent to the second file. This file should update the column but he doesn't

// HTML
echo"<FORM method='post' action='update-topscoorder-db.php'>";
echo "<TABLE width='100%' bordercolor='#000000' style='border-collapse:collapse' border='1'>";
echo "<TR>";
echo "<TD class='hoofd' colspan='4'>Topscoorder</TD>";
echo "</TR>";
include("dbconnect.php");
$result = mysql_query("SELECT `naam`,`goals`, `id` FROM topscoorder ORDER BY `goals` DESC");
while($row = mysql_fetch_assoc($result))
{
echo "<TR>";
echo "<TD class='menu'><INPUT type='checkbox' name='id[]' value='".$row['id']."'></TD>";
echo "<TD class='menu'>".$row['naam']."</TD>";
echo "<TD class='menu'>".$row['goals']."</TD>";
echo "<TD class='menu'><INPUT type='text' name='score'></TD>";
}
//rest of code

================================================

<?php
error_reporting(E_ALL);
include("dbconnect.php");
include("dbconnect.php");
$result = mysql_query("SELECT `id` FROM topscoorder);

$check = $_POST['id'];
$score = $_POST['score'];
while($row = mysql_fetch_assoc($result))
{
while(".$row['id']." == $check)
{
$query = "UPDATE `topscoorder` SET goals=goals+$score";
$result = mysql_query($query) or die ('Fout: '.mysql_error());

if (mysql_affected_rows()==1)
{
echo "<p>UPDATE voltooid. <a href='view-topscoorder.php'>Klik hier om terug te gaan</a></p>";
}
else
{
echo "<p>UPDATE afgebroken!</p>";
}
}
}
?>

StupidScript

9:19 pm on Mar 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps:

<?php
error_reporting(E_ALL);
include("dbconnect.php");
$check = $_POST['id'];
$score = $_POST['score'];
$getGoals=mysql_query("SELECT goals FROM toscoorder WHERE id='$check'") or die ('Fout: '.mysql_error());
while($row=mysql_fetch_assoc($getGoals))
{
$newGoals=$row["goals"]+$score;
$query = "UPDATE topscoorder SET goals='$newGoals' WHERE id='$check'";
$result = mysql_query($query) or die ('Fout: '.mysql_error());
if (mysql_affected_rows($result)<1)
{
echo "<p>UPDATE afgebroken!</p>";
}
else
{
echo "<p>UPDATE voltooid. <a href='view-topscoorder.php'>Klik hier om terug te gaan</a></p>";
}
}
?>

Also, from the PHP manual:

Note: When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.