0

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 ?

2
Contributors
1
Reply
19
Views
1 Year
Discussion Span
Last Post by ryantroop
1

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

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

Edited by ryantroop

Votes + Comments
Thanks that helped :D
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.