Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqli insert statement

Tags:

php

insert

mysqli

Ive been going over this for a couple days and i just cant find the reason it is erroring out. Im not getting a php warning, just the error handler "somethign went wrong" instead of inserting. I know the fine young lads here will probably spot it in a few seconds, esp considering its just a simple insert statement, but im buggered. Thanks in advance.

include('core.inc.php');
$sql = 'INSERT INTO $resultsTable (
    id, 
    firstName, 
    lastName, 
    email, 
    birthday, 
    anniversary,
    location,
    campaign
) 
VALUES (NULL,?,?,?,?,?,?,?)';
$stmt = $mysql->stmt_init();
if ($stmt->prepare($sql)) { 
// bind parameters and execute statement
$stmt->bind_param(
    'sssssss', 
    $_POST['fname'], 
    $_POST['lname'],
    $_POST['email'],
    $_POST['birthday'],
    $_POST['anniversary'],
    $_POST['location'],
    $campaign
);

$OK = $stmt->execute();}
 // return if successful or display error
    if ($OK) {$response = "Success";}
    else {$response ="Something went wrong.";}
}
like image 471
killer on the road Avatar asked Apr 21 '12 15:04

killer on the road


1 Answers

ok i echoed stmt error and it told me that $resultsTable didnt exist. In core.inc, I have a definition for that variable

Use concatenation or double quotes.

$sql = 'INSERT INTO ' . $resultsTable . ' (
    id, 
    firstName, 
    lastName, 
    email, 
    birthday, 
    anniversary,
    location,
    campaign
) 
VALUES (NULL,?,?,?,?,?,?,?)';


$sql = "INSERT INTO $resultsTable (
    id, 
    firstName, 
    lastName, 
    email, 
    birthday, 
    anniversary,
    location,
    campaign
) 
VALUES (NULL,?,?,?,?,?,?,?)";
like image 77
Stanislav Shabalin Avatar answered Oct 13 '22 00:10

Stanislav Shabalin