Jump to content

records in DB can not be updated


BigD

Recommended Posts

I used a select statement and found a record in DB, When I tried to update the record, returned 0 rows and not updated. Here is the code. Please help.$query="SELECT classtype,startdate,enddate,classstatus from class where classname = '$classnameval'"; $result = mysql_query($query); echo "select class sql $query</br>"; $num_rows = mysql_num_rows($result); if (!$num_rows) { echo nl2br("Class $classname not in DB\n"); goto bye; } if ($result) { $row = mysql_fetch_array($result, MYSQL_ASSOC); $classtypeo = $row['classtype']; $classstatuso = $row['classstatus']; $sdateo = $row['startdate']; $edateo = $row['enddate']; echo "<h2>Class '$classname' found</h2>\n";var_dump($classstatuso,$sdateo,$edateo,$classtypeo); } else { echo "<h2>Sorry, unable to change class $classname </h2>\n"; goto bye; }var_dump($classstatusval,$sdate,$edate,$classtype); if ($classstatusval == '') $classstatusval = $classstatuso; if ($classtype == '') $classtype = $classtypeo; if ($edate == '') $edate = $edateo; if ($sdate == '') $sdate = $sdateo; $query="UPDATE class SET classstatus = '$classstatusval', startdate = '$sdate', enddate = '$edate', classtype = '$classtype', where classname = '$classnameval'"; $result = mysql_query($query); echo "update class sql $query</br>"; $num_rows = mysql_num_rows($result); if (!$num_rows)// if (!$mysql_num_rows($result)) { echo nl2br("Class $classname not in DB\n"); goto bye; }

Link to comment
Share on other sites

I changed code to use mysql_affected_rows(), it should 0 rows affected. The search Key on Update SQL is exactly the same key on SELECT SQL. I am still puzzled why Update did not update. No SQL_error though. just no update.

Link to comment
Share on other sites

No SQL_error though. just no update.
you did not add mysqli_error(). and you are not checking the result resource. you shoukd do that both to debug.
Link to comment
Share on other sites

Enjoyed the comics. Yes goto should be discouraged to use, but there are times it is the simplest thing and the right thing to do. Can you share what the unexpected result might be?

Link to comment
Share on other sites

I'll agree that it can sometimes be the "simplest" thing to do, but I don't agree that it's ever the "right" thing to do. It's a little heavy-handed, it's not very elegant. The problem with using goto is that you can end up with spaghetti code that is difficult to both maintain and debug.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...