Minervaz Mine Minervaz Mine - 3 years ago 57
PHP Question

how to update value of the specific column from a table after deleting data

guy's i need help for this problem guy's
i have a table with 4 column.

no, name, address, phone
. the case is after i do deleting one data i must decrease column
no
with how many data i delete.

last time i make a program using vb.net i can do this. i use this code i can do it well

Dim ab As Integer
ab = lvrak.Items(i).SubItems(4).Text
Dim stok As New SqlClient.SqlCommand("Update tbl_barangluar set jumlah_barang=jumlah_barang - '" & ab & "' where kode_barang='" & _
lvrak.Items(i).SubItems(0).Text & "'")
stok.Connection = koneksi
stok.ExecuteNonQuery()


but now i need to do that in php function. and im really dont know how to do it.
i try just do
ex. update temp5 set no='$no' - 1
and it not going well. someone please help me

This my Trial PHP



<?php
include("../../Connections/koneksi.php");


$no= $_POST['no_check'];

// Attempt insert query execution
$sql = "DELETE FROM temp5 WHERE no='$no'";

if(mysqli_query($db, $sql)){
echo "Records were deleted successfully.";

} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
}

// Close connection
mysqli_close($db);
?>
<?php
include("../../Connections/koneksi.php");


$no= $_POST['no_check'];

// Attempt insert query execution
$sql = "alter table temp5 auto_increment = 1";

if(mysqli_query($db, $sql)){
echo "Records were deleted successfully.";

} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
}

// Close connection
mysqli_close($db);
?>
<?php
include("../../Connections/koneksi.php");

$no= $_POST['no_check'];
$min = 1;

// Attempt insert query execution
$sql = "UPDATE temp5 SET no=no -1 where no";
if(mysqli_query($db, $sql)){
echo "Records inserted successfully.";

} else{
echo "Records inserted failed ";
}

mysqli_close($db);
?>





I will add a picture before and after delete function has do.
the problem is there. i have try to do limiting them but is give more eror.
in the 1st picture is contain all the data before delete. and i will do delete the data with
no 4
. and the result is in picture 2. that result is wrong.. the result must be
1,2,3,4


enter image description here

enter image description here

Answer Source

You need to update the count after delete success

UPDATE temp5 SET no=no -1

PHP :

<?php include("../../Connections/koneksi.php");
       $no= $_POST['no_check']; 
       $sql = "DELETE FROM temp5 WHERE no='$no'"; 
      if(mysqli_query($db, $sql)){ 

         $sql1 = "UPDATE temp5 SET no=no -1"; 
         mysqli_query($db, $sql1);

      } else{

        echo "ERROR: Could not able to execute $sql. " . mysqli_error($db); 

       }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download