/** @return PDOStatement|bool */ public function Query($string, $returnID = false, $prettify = MYSQLLOGPRETTIFY) { if (!$this->connected) { $this->Connect(); } $this->LastQueryString = $string; $start = microtime(true); $this->LastQuery = $this->Conn->query($string); $finish = microtime(true) - $start; $this->QueryTime += $finish; $this->HitCount++; if ($this->logQueries) { $this->QueryLog .= "\r\nQuery #" . $this->HitCount . " Query duration: " . number_format($finish, 4); if ($this->LastQuery) { $this->QueryLog .= "\r\n\tRows: " . $this->LastQuery->rowCount(); } else { $this->QueryLog .= "\r\n\tError: " . implode(' - ', $this->Conn->errorInfo()); } $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $function = !empty($trace) ? $trace[0] : array(); foreach ($trace as $function) { if (array_key_exists('class', $function) && $function['class'] == 'MySQLDatabase') { continue; } else { break; } } $traceString = $function && array_key_exists('class', $function) ? $function['class'] . "::" : ''; $traceString .= $function && array_key_exists('function', $function) ? $function['function'] . '()' : ''; if (strlen($traceString)) { $traceString .= "\r\n\t"; } $traceString .= $function && array_key_exists('file', $function) ? $function['file'] . ':' . $function['line'] : ''; $this->QueryLog .= "\r\n\t{$traceString}"; if ($prettify) { $formatter = new SqlFormatter(); $string = $formatter->format($string, false); } $this->QueryLog .= "\r\n\t------\r\n\t" . str_replace("\n", "\n\t", trim(str_replace("\t", ' ', $string), "\r\n")) . "\r\n\t------"; $this->QueryLog .= "\r\n"; } return $returnID ? $this->Conn->lastInsertID() : $this->LastQuery; }
$connection2->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { echo $e->getMessage(); } @session_start(); //Set timezone from session variable date_default_timezone_set($_SESSION[$guid]["timezone"]); $gibbonPersonID = $_POST["gibbonPersonID"]; $date = date("Y-m-d"); $type = "Positive"; $descriptor = "Quick Star"; $level = ""; $comment = ""; $gibbonPlannerEntryID = $_POST["gibbonPlannerEntryID"]; if ($gibbonPersonID == "" or $date == "" or $type == "" or $descriptor == "" or $gibbonPlannerEntryID == "") { print _("Error"); } else { //Write to database try { $data = array("gibbonPersonID" => $gibbonPersonID, "date" => $date, "type" => $type, "descriptor" => $descriptor, "level" => $level, "comment" => $comment, "gibbonPlannerEntryID" => $gibbonPlannerEntryID, "gibbonPersonIDCreator" => $_SESSION[$guid]["gibbonPersonID"], "gibbonSchoolYearID" => $_SESSION[$guid]["gibbonSchoolYearID"]); $sql = "INSERT INTO gibbonBehaviour SET gibbonPersonID=:gibbonPersonID, date=:date, type=:type, descriptor=:descriptor, level=:level, comment=:comment, gibbonPlannerEntryID=:gibbonPlannerEntryID, gibbonPersonIDCreator=:gibbonPersonIDCreator, gibbonSchoolYearID=:gibbonSchoolYearID"; $result = $connection2->prepare($sql); $result->execute($data); } catch (PDOException $e) { //Fail 2 $URL .= "&addReturn=fail2"; header("Location: {$URL}"); break; } print "<a href='" . $_SESSION[$guid]["absoluteURL"] . "/index.php?q=/modules/Behaviour/behaviour_manage_edit.php&gibbonBehaviourID=" . $connection2->lastInsertID() . "&gibbonPersonID=&gibbonRollGroupID=&gibbonYearGroupID=&type='><img style='margin-top: -30px; margin-left: 60px' src='" . $_SESSION[$guid]["absoluteURL"] . "/themes/" . $_SESSION[$guid]["gibbonThemeName"] . "/img/like_on.png'></a>"; }
/** * Create or update an object in the database if it already exists * @param array $update Values to update * @param array $exclude Attributes not to set in create * @param \PDO $db Database connection to use, default to master resource * @return boolean */ public function createOrUpdate($update, $exclude = array(), &$db = FALSE) { // Get columns and values $create = $this->createColumnsAndValues($exclude); // Generate update values $updateVals = NULL; foreach ($update as $column => $value) { // Add column $updateVals .= ', `' . $column . '` = '; // If the updated value is an array treat first item // as literal query insert and the second a params to bind $updateVals .= is_array($value) ? $value[0] : $value; } // Trim the first character (,) from the update $updateVals = substr($updateVals, 2); // Get database master for write if ($db === FALSE) { $db =& $this->getDbMaster(); } // Prepare query $query = $db->prepare(' INSERT INTO `' . static::$dbTable . '` (' . $create['columns'] . ') VALUES ( ' . $create['values'] . ') ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(' . static::$pk . '), ' . $updateVals . ' '); // Bind attributes $this->bindAttributes($query, $exclude); // Bind update parameters foreach ($update as $value) { if (is_array($value) && isset($value[1]) && is_array($value[1])) { foreach ($value[1] as $column => $newVal) { $query->bindValue($column, $newVal); } } } // Execute if (!$this->executeCreateUpdateQuery($query)) { return FALSE; } // Set the pk if (!$this->attributeHasValue(static::$pk) || !$this->iget(static::$pk)) { $this->iset(static::$pk, $db->lastInsertID()); } // return TRUE return TRUE; }
<?php $host = 'localhost'; $user = '******'; $pass = ''; $dbname = 'mydb'; try { $conn = new PDO("msql:host={$host};dbname={$dbname}", $user, $pass); $statement = $conn->prepare("INSERT INTO finaldb(Gender, Birthdate, Texty) values (:gender, :bdat, :tex)"); $statement->bindValue(":gender", $_POST['gender']); $statement->bindValue(":Birthdate", $_POST['bdat']); $statement->bindValue(":Texty", $_POST['tex']); $statement->execute(); $numRowsAffected = $statement->rowCount(); $insertedPrimaryKey = $conn->lastInsertID(); $conn = null; header("Location: index.php"); } catch (PDOException $e) { echo $e->getMessage(); }