我有以下疑问:
INSERT INTO ipi_messages (Message_userID, Message_fromName, Message_fromEmail, Message_subject, Message_body) VALUES(0, 'hope', 'thisworks@gmail.com', 'i hope', 'this works')
我得到以下MySQL错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
下面是我的表架构:
<?php
require 'lib/class.Database.php';
require 'lib/class.Messages.php';
$messageDatabase = new Database('ipi2');
$messageDatabase->newDatabaseUsers([
['Query','ipi_query','k2QvHmtxGhVN'],
['admin','ipi_admin','r0HHRsQ76kS2']
]);
$messageDatabase->makeConnection('admin');
var_dump($_POST);
if(array_key_exists('sendMessage', $_POST)){
// $query = "INSERT INTO ipi_messages (Message_userID, Message_fromName, Message_fromEmail, Message_subject, Message_body) VALUES(?,?,?,?,?)";
// $result = $messageDatabase->query($query, $_POST['to'], $_POST['fromName'], $_POST['fromEmail'], $_POST['subject'], $_POST['message']);
$messages = new Messages($messageDatabase);
$messages->postMessage([
'Message_userID' => (int)$_POST['to'],
'Message_fromName' => $_POST['fromName'],
'Message_fromEmail' => $_POST['fromEmail'],
'Message_subject' => $_POST['subject'],
'Message_body' => $_POST['message']
]);
}
if(array_key_exists('getMessages', $_POST)){
$query_get = "SELECT * FROM ipi_messages WHERE Message_userID = ?";
$result_get = $messageDatabase->query($query_get, $_POST['user']);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Messages</title>
<style type="text/css">h5, h6{margin: 0;padding: 0;}</style>
</head>
<body>
<h3>Send Message</h3>
<form name="sendMessage" method="post" action="">
<h5>To:</h5>
<select name="to">
<option value=""></option>
<option value="0">Fillip Peyton</option>
<option value="1">Fillswitch</option>
</select>
<br/>
<h5>Name:</h5>
<input type="text" name="fromName">
<h5>Email:</h5>
<input type="text" name="fromEmail">
<h5>Subject:</h5>
<input type="text" name="subject">
<h5>Message:</h5>
<textarea name="message"></textarea>
<br/>
<input type="submit" name="sendMessage" value="Send">
</form>
<h3>Your Messages:</h3>
<form name="getMessages" method="post" action="">
<select name="user">
<option value=""></option>
<option value="0">Fillip Peyton</option>
<option value="1">Fillswitch</option>
</select>
<input type="submit" name="getMessages" value="Get Messages">
<?php
if (isset($result_get)) {
foreach ($result_get as $result) {
?>
<p>
<b><?php echo $result['Message_fromName']; ?></b><br/>
<?php echo $result['Message_fromEmail']; ?><br/>
<i><?php echo $result['Message_subject']; ?></i><br/>
<?php echo $result['Message_body']; ?>
</p><hr>
<?php
}
}
?>
</form>
</body>
</html>
<?php
require_once 'lib/classFunctions.php';
class Messages
{
protected $database;
protected $userID = -1;
public function __construct(Database $messageDatabase) {
$this->database = $messageDatabase;
}
public function getMessages($userID = null){
}
public function postMessage(Array $columnsValues){
// $query = "INSERT INTO ipi_messages (Message_userID, Message_fromName, Message_fromEmail, Message_subject, Message_body) VALUES(?,?,?,?,?)";
$columns = '';
$values = '';
$queryParams = array();
foreach ($columnsValues as $column => $value) {
$columns .= "$column, ";
$values .= is_int($value) ? "$value, " : "'$value', ";
$queryParams[] = $value;
}
$columns = substr($columns, 0, strlen($columns) - 2);
$values = substr($values, 0, strlen($values) - 2);
$query = "INSERT INTO ipi_messages ($columns) VALUES($values)";
// array_unshift($queryParams, $query);
// $result = $messageDatabase->query($query, $_POST['to'], $_POST['fromName'], $_POST['fromEmail'], $_POST['subject'], $_POST['message']);
echo $query . '<br/>';
return $result = call_user_func_array(array($this->database,'query'), refValues($queryParams));
}
public function deleteMessage(){}
}
<?php
require_once 'lib/classFunctions.php';
class Database
{
protected $DATABASE = 'database';
protected $SERVER = 'localhost';
protected $DATABASEUSERS;
protected $CONNECTION;
public function __construct($database, $server = 'localhost'){
$this->DATABASE = $database;
$this->SERVER = $server;
}
public function makeConnection($userType){
$user = $this->DATABASEUSERS[strtolower($userType)];
$this->CONNECTION = new mysqli($this->SERVER, $user->getUsername(), $user->getPassword(), $this->DATABASE) or die('Cannot make connection to database...');
}
public function escapeSpecialChars($string){
return $this->CONNECTION->real_escape_string($string);
}
public function newDatabaseUser($userType, $username, $password){
$userType = strtolower($userType);
$this->DATABASEUSERS[$userType] = new DatabaseUser($userType, $username, $password);
}
public function newDatabaseUsers($usersCollection){
foreach ($usersCollection as $user) {
$userType = strtolower($user[0]);
$username = $user[1];
$password = $user[2];
$this->DATABASEUSERS[$userType] = new DatabaseUser($userType, $username, $password);
}
}
public function closeConnection(){
if($this->CONNECTION)
$this->CONNECTION->close();
else
throw new Exception("No connection available.", 1);
}
public function query($query, $vars = null){
$stmt = $this->CONNECTION->stmt_init();
$args = func_get_args();
if($stmt->prepare($query)){
if($vars != null){
$queryParams = array();
$queryTypes = '';
$argsCount = count($args);
for( $i = 1 ; $i < $argsCount ; $i++ ){
$var = $args[$i];
$varType = gettype($var);
switch ($varType) {
case 'string':
$queryTypes .= 's';
break;
case 'integer':
$queryTypes .= 'i';
break;
case 'double':
$queryTypes .= 'd';
break;
case 'blob':
$queryTypes .= 'b';
break;
default:
throw new Exception("Could not bind parameter of type: " . $varType, 1);
break;
}
}
$queryParams[] = $queryTypes;
for( $i = 1 ; $i < $argsCount ; $i++ )
$queryParams[] = $args[$i];
// $stmt->bind_param($queryParams);
call_user_func_array(array($stmt,'bind_param'), refValues($queryParams));
}
$isExecuted = $stmt->execute();
if($isExecuted)
return $result = $stmt->get_result();
else
die("Could not execute query($query):" . $this->CONNECTION->error);
}else{
die("Could not prepare statement: " . $this->CONNECTION->error);
}
$stmt->reset();
}
}
class DatabaseUser
{
protected $USERTYPE = 'query';
protected $USERNAME = 'username';
protected $PASSWORD = 'password';
public function __construct($userType, $username, $password) {
$this->USERTYPE = $userType;
$this->USERNAME = $username;
$this->PASSWORD = $password;
}
public function getUserType(){ return $this->USERTYPE; }
public function getUsername(){ return $this->USERNAME; }
public function getPassword(){ return $this->PASSWORD; }
}
<?php
function refValues($arr){
if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
{
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
return $arr;
}
trigger_error($myqli->error.“[$SQL]”)
die($myqli->error - 最好将查询拆分为多行,以使错误消息更具信息量。
问题内容: 停止使用不推荐使用的mysql_ *函数后,我切换到mysqli。但是后来,我注意到未准备好的语句对于SQL注入是不安全的。然后,我再次更改了代码。 我所拥有的是以下函数,该函数检查数据库中是否存在变量 $ ID 并打印该行的 title 值: 我将其更改为: 我的问题是:这是执行准备好的语句的正确方法吗?另外,我现在可以安全使用SQL注入吗?非常感谢任何愿意回答这个问题的人:) 问题
问题内容: 我总是发现很难编写MySQLi预备语句,因为许多函数的工作方式与旧方法不同。现在我面临一个问题。 问题答案: 您正在尝试通过以下方式获取结果 事实并非如此。因为execute将仅返回布尔值。 做喜欢的。
问题内容: 这个问题已经在这里有了答案 : mysqli_fetch_assoc()期望参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误? (1个答案) 3年前关闭。 我正在努力弄清MySQli,但我对错误报告感到困惑。我正在使用MySQLi’prepare’语句的返回值来检测执行SQL时的错误,如下所示: 但是,prepare语句的返回值是否仅检测SQL语句的前
我正在运行一个2个节点(每个节点8个内核)的Cassandra 3.10集群,复制因子为1。随着时间的推移,我在我的cassandra集群上拥有并且已经拥有许多键空间。 我最近注意到,当我停下来/开始时,回来的时间非常慢。在我的两个系统中,我都看到了数以千计的“准备好的语句重建”堆栈跟踪,如下面的示例所示。log和debug.log。看起来Cassandra正在尝试为密钥空间准备语句,这些密钥空间
很抱歉,我对Java知之甚少。我得到了这个代码来接管。基本上,我在 当我运行代码时。 下面是解析XML的块 这是Improts,不确定是否需要这样做 这是XML文件
问题内容: 我如何使用mysqli用LIKE进行查询并获得所有结果? 这是我的代码,但是不起作用: 此代码似乎不起作用。我已经搜索了很多。也可能返回多于1行。那么,即使返回的结果超过1行,我如何获得所有结果呢? 问题答案: 这是您正确获取结果的方式 或者,您也可以执行以下操作: 希望您意识到我从这里和这里的手册中直接得到了答案,这是您应该首先去的地方。