-1

Can't insert data into DB . When i remove user_id then data is inserted. Please check below my code and help me.

function adddata($data) {
    global $db;
    if (is_array($data)) {
        $stmt = $db->prepare('INSERT INTO `pay` (id, payment, status, itemid, time, user_id) VALUES(?, ?, ?, ?, ?, ?');

        $userid = 2;
        $stmt->bind_param(
             'sdssss',
            $data['txn_id'],
            $data['payment_amount'],
            $data['payment_status'],
            $data['item_number'],
            date('Y-m-d H:i:s'),
            $userid
        );
        $stmt->execute();
        $stmt->close();
        return $db->insert_id;
        }
        return false;
        }
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
  • 3
    Think there is a missing `)` in `VALUES(?, ?, ?, ?, ?, ?');` – Nigel Ren Nov 14 '18 at 20:21
  • This stands to be a typo-based issue for more than just the missing bracket. The "id" column, is that an string based column or integer? – Funk Forty Niner Nov 14 '18 at 20:30
  • sir this is a string column of my id. – Arsalan khatri Nov 14 '18 at 20:32
  • Is your `id` value always the same? Because if it's a primary key then you wouldn't insert. – Michael Nov 14 '18 at 20:42
  • yes got it, thanks nigel Ren. bracket missing in my query. having also one issue can you please solve ? want to pass $_SESSION[userid]; in to my $userid variable. when i hardcode value in to $userid variable then query run successfully. but when pass $_SESSION[userid] query not execute. – Arsalan khatri Nov 14 '18 at 20:54

1 Answers1

0

It's subtle, but your SQL string is missing a closing bracket:

$stmt = $db->prepare('INSERT INTO `pay` (...) VALUES (?, ?, ?, ?, ?, ?)');

Where the VALUES list was not properly closed.

A lot of problems can be detected and resolved by enabling exceptions in mysqli so mistakes aren't easily ignored. This should show up as a SQL error in your logs.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • having also one issue can you please solve ? want to pass $_SESSION[userid]; in to my $userid variable. when i hardcode value in to $userid variable then query run successfully. but when pass $_SESSION[userid] query not execute – Arsalan khatri Nov 14 '18 at 21:02
  • "Will not execute" is not sufficiently detailed. If you can be more specific, like the exact error text you're getting, it's worth opening this up as a new question where that problem can be addressed. – tadman Nov 14 '18 at 21:37