0

I want to insert data to database if there is no data yet in my database, but when there is a data in my database I want update it instead

How to know if there is no data yet or there is already in database here is my php code I have tried countRow etc, but always returning undefined method

public function tambahCatatanDosenPA($no, $catatan, $semester) {

        $stmt = $this->conn->prepare("SELECT * FROM catatan_dosenpa WHERE no_user_id = ?");
        $stmt->bind_param("s", $no);
        $stmt->execute();
        $stmt->bind_result($no_isi, $no_user_id_isi, $catatan_isi, $semester_isi);
        $list_catatan = array();
        while ($stmt->fetch()) {
                $temp = array();
                $temp['no'] = $no_isi;
                $temp['no_user_id'] = $no_user_id_isi;
                $temp['catatan'] = $catatan_isi;
                $temp['semester'] = $semester_isi;
                array_push($list_catatan,$temp);
            }
            $stmt->close();
        if ($list_catatan.count() = 0) {

            $stmt = $this->conn->prepare("INSERT INTO catatan_dosenpa(no_user_id, catatan, semester) VALUES(?, ?, ?)");
            $stmt->bind_param("sss",$no, $catatan, $semester);
            $result = $stmt->execute();
            $stmt->close();
            return true;

        } else {

            $stmt = $this->conn->prepare("UPDATE catatan_dosenpa SET catatan = ? WHERE no_user_id = ? AND semester = ?");
            $stmt->bind_param("sss", $catatan,  $no, $semester);
            $result = $stmt->execute();
            $stmt->close();
            return true;
        }
    }
llinvokerl
  • 938
  • 9
  • 24
Togan J.R
  • 89
  • 10
  • 1
    `$list_catatan.count() = 0` is wrong in multiple ways. 1. `.` concatenate in PHP. 2. `count` takes the array as its parameter. 3. A single `=` assigns. Also you are using `mysqli` not PDO. You could put `count(*)` in your query and return the value but you're probably better off using a `insert into on duplicate update` then you dont need to figure it out, DB will do it for you. – user3783243 Dec 23 '19 at 03:01
  • @user3783243 i see , ill try to use insert into on duplicate update instead – Togan J.R Dec 23 '19 at 03:08
  • You should count records first, as @user3783243 suggested, or, if you want to keep your code use count() php function: **if(count($list_catatan) == 0)** – Triby Dec 23 '19 at 03:14
  • @user3783243 i ve tried change into this $stmt = $this->conn->prepare("INSERT INTO catatan_dosenpa(no_user_id, catatan, semester) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE catatan = ?"); $stmt->bind_param("ssss",$no, $catatan, $semester, $catatan); got error on bind_param can u tell me what is wrong ? – Togan J.R Dec 23 '19 at 03:19
  • but actually i have solved the problem by changing count() thanks to u all, but still wondering what is wrong in my insert into duplicate key update for study – Togan J.R Dec 23 '19 at 03:26
  • What is the error you received? – user3783243 Dec 23 '19 at 03:37
  • @user3783243 bind_param error, i am guessing its about my structure of catatan_dosenpa(no_user_id, catatan, semester) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE catatan = ?"); $stmt->bind_param("ssss",$no, $catatan, $semester, $catatan) table name ,column name is right tho – Togan J.R Dec 23 '19 at 03:49
  • See https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query and get the real error message. – user3783243 Dec 23 '19 at 03:56

0 Answers0