-1

So I've been trying to change my code to any other ways that can fit but nothing happened... I want to change status_data_user to be nonactive from active but it cannot works.. on my other delete codes is working without any troubles.

I've been changing it to

$hapus = mysqli_query($conn, "UPDATE tb_user SET status_data_user='$status_data_user',created_at='$created_at', updated_at='$updated_at' WHERE id_user='$id_user'");
$x="UPDATE tb_user SET status_data_user='$status_data_user',created_at='$created_at', updated_at='$updated_at' WHERE id_user='$id_user'";
$hapus = mysqli_query($conn, $x);
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
Andie
  • 3
  • 1
  • 2
    You should add error checking. You should also learn how to use prepared statements instead of substituting variables, to prevent SQL injection. – Barmar Aug 30 '19 at 07:02
  • 1
    See https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments and https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Barmar Aug 30 '19 at 07:03
  • what does `$id_user` have in it ,check onces. – Swati Aug 30 '19 at 07:04
  • Can you please check what is the data type of **status_data_user** field in database? Are you passing proper value in status_data_user field in update query? – Rajdip Chauhan Aug 30 '19 at 07:10
  • @RajdipChauhan its varchar – Andie Aug 30 '19 at 07:15
  • @Swati did u mean this?? $id_log = ""; $id_user = $row_user['id_user']; $aksi = "edit user"; $ip_address = $_SERVER['REMOTE_ADDR']; $status_data_user = "nonaktif"; $status_data_log = "aktif"; $created_at = date("Y-m-d H:i:s"); $updated_at = date("Y-m-d H:i:s"); – Andie Aug 30 '19 at 07:23
  • if (isset($_GET['id'])) { $id_user = $_GET['id']; }else{ die("error no id selected"); } $data="SELECT * FROM tb_user WHERE id_user=$id_user"; $q = mysqli_query($conn, $data); $hasil = mysqli_fetch_array($q); $id_user = $hasil['id_user']; $nama = $hasil['nama']; $username = $hasil['username']; $password = $hasil['password']; $lvl_user = $hasil['lvl_user']; $status_data_user="nonaktif"; – Andie Aug 30 '19 at 07:23
  • [https://docs.google.com/document/d/1T8ipLKrNcgqJWuCNxTOAQrxUfdEv7ygrmVGV1mljbqk/edit?usp=sharing] – Andie Aug 30 '19 at 07:34
  • Follow this steps. 1) Just change the table name (Something like tb_user1) and run the query, 2) Now u will get the error with your sql query and variable values. 3) Now copy this query and rename table name to original name and run this in phpmyadmin panel, here you will get clear idea – Lets-c-codeigniter Aug 30 '19 at 07:47

3 Answers3

0

Try to use like this

$x="UPDATE tb_user SET status_data_user='".$status_data_user."',created_at='".$created_at."', updated_at='".$updated_at."' WHERE id_user='".$id_user."'";
$hapus = mysqli_query($conn, $x);
Lets-c-codeigniter
  • 682
  • 2
  • 5
  • 18
-1

Issue is that, you are passing php variable value in single comma, You can pass it like below

$x="UPDATE tb_user SET status_data_user='".$status_data_user."',created_at='".$created_at."', updated_at='".$updated_at."' WHERE id_user='".$id_user."'";
$hapus = mysqli_query($conn, $x);
Rajdip Chauhan
  • 345
  • 2
  • 11
  • There is no issue in update query I mention above, may be you have any other issue related data passing in it. may be other's field value. – Rajdip Chauhan Aug 30 '19 at 07:33
-2

The thing is that you are using the php variables in the string itself, as it will be interprated by PHP as char values but not Variables. So try to use like

"UPDATE tb_user SET status_data_user='".$status_data_user."',created_at='".$created_at."', updated_at='".$updated_at."' WHERE id_user='".$id_user."'";

The method you are using by putting variables in string is often used in PHP Laravel Framework, because it has Blade extensions that can interpret variables within the string.

Hope it helps