INSERT INTO <tbl_name> SET <col>=<expr> ... is perfectly valid syntax. The problem is "show" is a SQL keyword. Enclose it in backquotes so the parser parses it as a table name:
Code:
INSERT INTO `show` SET
perf = '$perf',
showname = '$showname',
showid = '$id',
totalstats = '$totalstats',
petname = '$petname',
petid = '$petid'
Your script is open to SQL injection via the unfiltered $_GET['id'] and $_POST['petid'] variables. Use the filter functions or the DB driver's escape function (e.g. mysql_escape_string, mysqli_real_escape_string, pg_escape_string). Even better is to use prepared statements. Try the mysqli driver and mysqli_prepare:
PHP Code:
class DBException extends RuntimeException {
protected $_logMsg;
function __construct($logMsg, $msg='', $code=0) {
parent::__construct($msg, $code);
$this->_logMsg=$logMsg;
}
function getLogMessage() {
return $this->_logMsg;
}
}
try {
$dbConn = new mysqli('localhost', $dbUser, $dbPassword, $dbName);
if ($dbConn->connect_errno) {
throw new DBException("DB connect error: {$dbConn->connect_error}",
"Couldn't connect to database. Something is probably temporarily wrong with the server. Please try again later.", $dbConn->errno);
}
$stmt = 'SELECT showname, id, fee, hosterid FROM `shows` WHERE id = ?';
if (! ($showQuery = $dbConn->prepare($stmt))) {
throw new DBException("Prepare error: {$dbConn->error}; statement: '$stmt'",
"Couldn't create database query.", $dbConn->errno);
}
if (! $showQuery->bind_param('i', $_GET['id'])) { // change 'i' to 's' if ids aren't integers
throw new DBException("Bind param error: {$dbConn->error}; param: '$_GET[id]'.",
"Couldn't create database query.", $dbConn->errno);
}
if (! $showQuery->execute()) {
throw new DBException('Fetch error: ' . $dbConn->error,
"Couldn't fetch any information from database", $dbConn->errno);
}
if (! $showQuery->bind_result($showname, $id, $fee, $hosterid)) {
throw new DBException("Bind param error: {$dbConn->error }; param: '$_GET[id]'.",
"Couldn't create database query", $dbConn->errno);
}
if (FALSE === ($success = $showQuery->fetch()) ) {
throw new DBException("Fetch error: {$dbConn->error }; param: '$_GET[id]'.",
"Couldn't create database query", $dbConn->errno);
} elseif (is_null($success)) {
throw new RuntimeException("No results found for show '$_GET[id].'");
}
$showQuery->close();
// $showname, $id, $fee, $hosterid now hold the appropriate values.
//...
} catch (DBException $exc) {
echo 'Error: ', $exc->getMessage(), '. ';
if (error_log('[script ' . __FILE__ . '@' . __LINE__ . '] ' . $exc->getLogMessage())) {
echo "An error has been logged. We'll look into it.";
} else {
// echo "I couldn't log the error. Please e-mail at $email and let us know the time and error message you're getting.";
}
} catch (RuntimeException $exc) {
echo $exc->getMessage();
}
You can tighten up the above code by extending class MySQLi and MySQLi_stmt to throw exceptions rather than returning FALSE.