0

Why am getting error when i think it should work fine

Error

Fatal error: Wrong SQL: INSERT INTO kladilnica (Username, Bet, Win, reg_date, Odds, isActive, ticket_id, isShared, is_winned, Location) VALUES ('Stefan', 100, 3500, '2017-02-06', 66, 1, 351358728, 0, 0, '') Error: in C:\xampp\htdocs\bootstrap\navigation.php on line 257

Code

$sql = "INSERT INTO kladilnica (Username, Bet, Win, reg_date, Odds, isActive, ticket_id, isShared, is_winned, Location) VALUES ('".$user."', ".$bet.", ".$gain.", '".$date."', ".$odds.", 1, $randomTicketID, 0, 0, '".$userLocation."')";
$result = $conn->query($sql);
$TicketID = $conn->insert_id;
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

Edited by Stefan_1

2
Contributors
4
Replies
30
Views
6 Months
Discussion Span
Last Post by cereal
0

Here is the full script

if(isset($_POST['bet']) && isset($_POST['gain']) && isset($_POST['odds']) && isset($_POST['date'])) {
    $bet = htmlentities($_POST['bet']);
    $gain = htmlentities($_POST['gain']);
    $odds = htmlentities($_POST['odds']);
    $date = htmlentities($_POST['date']);

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

    $randomTicketID = mt_rand(000000000,999999999);
    $sql = "INSERT INTO kladilnica (Username, Bet, Win, reg_date, Odds, isActive, ticket_id, isShared, is_winned, Location) VALUES ('".$user."', ".$bet.", ".$gain.", '".$date."', ".$odds.", 1, $randomTicketID, 0, 0, '".$userLocation."')";
    $result = $conn->query($sql);
    $TicketID = $conn->insert_id;
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

    $matches1 = $_POST['match1'];
    $matches2 = $_POST['match2'];
    $tips = $_POST['tip'];
    $countMatches = count($matches1);

    for($i=0; $i < $countMatches; $i++) {
        $match1 = mysqli_real_escape_string($conn, $matches1[$i]);
        $match2 = mysqli_real_escape_string($conn, $matches2[$i]);
        $tip    = $conn->real_escape_string($tips[$i]);

        if ($match1 != "" && $match2 != "" && $tip != "") {
            $match1 = $match1;
            $match2 = $match2;

            $sql2 = "INSERT INTO matches (MatchTitle, Tip, TicketID) VALUES ('".$match1." - ".$match2."', '".$tip."', ".$TicketID.")";
            $result2 = $conn->query($sql2);
        }
    }
}
1

This line will raise an error and kill the execution:

trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

If you want to use it, then you have to create a condition, something like this:

if( ! $result)
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

Otherwise it will run at each execution.

0

Thank you cereal but why the second SQL INSERT doesnt work ? That should insert into database also but can't ? o_0

1

Dunno, add an error check and see what you get:

if( ! $result2)
    print sprintf('Error (%s) %s', $conn->errno, $conn->error);

Note - on lines 22/23 you are accessing mysqli through procedural mode, instead of OOP, this should not make difference in the code execution, but keep it in consideration.

Edited by cereal

This topic has been dead for over six months. 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.