IF/ELSE condition in sql and php -
i'm inserting database id , hours, working successfully. when insert new value id has previous value, old val isn't replaced new, instead they're kept both. i'm trying update function if/else statement (commented part). still same result. if/else statement must check first if hours (a column in table) value empty, if perform sql insert, else perform sql update. please?
if (isset($_post['submit'])) { $hours = $_post['hours']; $selectid = $_post['selectid']; $sql1 = "insert `editedworkhours` (`id`,`h`) values('$selectid','$hours')"; $getresult = mysql_query($sql1); if (mysql_affected_rows() > 0) { } else { } $tempname = $row['field']; $sql2 = "update editedworkhours set h ='" . $_get["hours"] . "' idnumber='" . $_get["selectid"] . "'"; $result2 = mysqli_query($con, $sql2); if ($con->query($sql2) === true) { } else { } } echo $menu;
try this
<?php if(isset($_post['submit'])) { $addedhours = $_post['addedhours']; $selectaf = $_post['selectaf']; $sql1="select * editedworkhours afnumber='$selectaf' , addedwh ='$addedhours'"; $getresult = mysql_query($sql1); $count = count($getresult); if(!empty($count) || $count==1) { $tempname = $row['field']; $sql2 = "update editedworkhours set addedwh ='$addedhours' afnumber='$selectaf'"; $result2 = mysql_query($sql2); if (isset($result2)) { //data inserted } else { //insert failed echo '<script>swal("error", "something went wrong error");</script>'; } echo '<script>swal("success", "changes have been saved", "success");</script>'; } else { $sql3 = "insert editedworkhours (afnumber,addedwh) values('$selectaf','$addedhours')"; $result = mysql_query($sql3); if(isset($result)) { echo 'success'; } else { echo 'failed'; } } } echo $menu;
Comments
Post a Comment