BigD Posted August 26, 2012 Share Posted August 26, 2012 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 More sharing options...
JamesB Posted August 26, 2012 Share Posted August 26, 2012 mysql_affected_rows() should work:http://www.php.net/m...fected-rows.php Link to comment Share on other sites More sharing options...
BigD Posted August 27, 2012 Author Share Posted August 27, 2012 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 More sharing options...
JamesB Posted August 27, 2012 Share Posted August 27, 2012 not sure if this will fix it, but try removing the comma at the end of: classtype = '$classtype', Link to comment Share on other sites More sharing options...
birbal Posted August 27, 2012 Share Posted August 27, 2012 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 More sharing options...
justsomeguy Posted August 27, 2012 Share Posted August 27, 2012 You should also be careful about using goto, it can cause unpredictable results. Link to comment Share on other sites More sharing options...
BigD Posted August 31, 2012 Author Share Posted August 31, 2012 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 More sharing options...
justsomeguy Posted August 31, 2012 Share Posted August 31, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now