Hello i want to make like button so i need to INSERT, SELECT and check does already exists the record i have this code:
likes.php

<?php
    require 'connect.php';
    require 'core.php';
    require_once 'functions.php';

    $user = getUserData('users', 'UserUsername');

    if(isset($_GET['type'], $_GET['id'])){


        $type   = $_GET['type'];
        $id     = (int)$_GET['id'];

        switch($type) {
            case 'ticket':
                $sql = "
                INSERT INTO likes (Username, ticketID)
                SELECT {$user}, {$id} 
                FROM kladilnica 
                WHERE EXISTS (
                    SELECT ID
                    FROM kladilnica 
                    WHERE ticket_id = {$id} 
                    OR Username = {$user}) 
                AND NOT EXISTS (
                    SELECT ID 
                    FROM likes 
                    WHERE Username = {$user} 
                    AND ticketID = {$id}) 
                LIMIT 1
                ";
                $result = $conn->query($sql);
                if(!$result) {
                    echo "can't connect!";
                    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
                } else {
                    echo "connected";
                }
            break;
        }
    }

    header('Location: shared.php');
?>

but its gives me error

Fatal error: Wrong SQL: INSERT INTO likes (Username, ticketID) SELECT StefanRafa, 156677 FROM kladilnica WHERE EXISTS ( SELECT ID FROM kladilnica WHERE ticket_id = 156677 OR Username = StefanRafa) AND NOT EXISTS ( SELECT ID FROM likes WHERE Username = StefanRafa AND ticketID = 156677) LIMIT 1 Error: Unknown column 'StefanRafa' in 'field list' in C:\xampp\htdocs\bootstrap\likes.php on line 35
What is the problem here i dont understand ?

Looks like StefanRafa is not a valid column name in your table kladilnica

Try adding quotes around '{User}' on line 18.

commented: Thanks that helped :D +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.