Mysqli insert statement

2020-06-17 06:06发布

问题:

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.";}
}

回答1:

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,?,?,?,?,?,?,?)";