选择工程,插入到mysql不插入php [英] Select works, Insert into mysql does not insert with php
问题描述
我有一个如下的表格
CREATE TABLE如果不存在`pictures`(
`id` int (11)NOT NULL AUTO_INCREMENT,
`name` varchar(200)NOT NULL,
`description` varchar(200)NOT NULL,
`url` varchar b $ b`users_id` bigint(20)NOT NULL,
`totalvoteup` int(11)NOT NULL,
`totalvotedown` int(11)NOT NULL,
`totalvoteneutral` int 11)NOT NULL,
PRIMARY KEY(`id`),
KEY`users_id`(`users_id`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 3
我可以使用sql语句在phpmyadmin中插入以下值:
INSERT INTO INSERT INTO pictures(name,description,url,users_id)VALUES('try','try','try',12345)
所以在数据库中没有问题
但是如果我使用下面的php脚本来选择和插入与我的数据库的交互它不会向数据库中插入任何内容
<?php
//获取字符串的帮助方法HTTP状态代码的描述
//来自http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
function getStatusCodeMessage($ status )
{
//这些可以存储在.ini文件中,并通过parse_ini_file()加载
//但是,这将足以满足
//的示例
$ codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => '非授权信息',
204 => 'No Content',
205 => '重置内容',
206 => 'Partial Content',
300 => 'multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'see Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => '不接受',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => '不支持的媒体类型',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return(isset($ codes [$ status]))? $ codes [$ status]:'';
}
//发送HTTP响应代码/消息的帮助方法
function sendResponse($ status = 200,$ body ='',$ content_type ='text / html ')
{
$ status_header ='HTTP / 1.1'。 $ status。 ''。 getStatusCodeMessage($ status);
header($ status_header);
header('Content-type:'。$ content_type);
echo $ body;
}
class RedeemAPI {
private $ db;
//构造函数 - 打开DB连接
function __construct(){
$ this-> db = new mysqli('localhost','root','password' 'testBasParmak');
$ this-> db-> autocommit(FALSE);
}
//解构函数 - 关闭DB连接
function __destruct(){
$ this-> db-> close
}
//兑换代码的主要方法
function redeem(){
//检查所需的参数
if isset($ _ POST [users_id])&&&&&&& isset($ _ POST [name])&& ])){
//将参数放入局部变量
$ users_id = $ _POST [users_id];
$ name = $ _POST [name];
$ url = $ _POST [url];
$ description = $ _ POST [description];
//插入我也尝试使用插入.....而不是插入....
$ stmt = $ this- > db-> prepare(INSERT INTO pictures(name,description,url,users_id)VALUES(?,?,?,?));
$ stmt-> bind_param(sssi,$ name,$ description,$ url,$ users_id);
$ stmt-> execute();
$ stmt-> close();
// select
$ stmt = $ this-> db-> prepare('SELECT name,description,url,users_id FROM pictures') ;
$ stmt-> execute();
$ stmt-> bind_result($ name,$ description,$ url,$ users_id);
while($ stmt-> fetch()){
echo图片名称为$ name url of picture is $ url;
}
$ stmt-> close();
return true;
}
sendResponse(400,'Invalid request');
return false;
}
}
//这是在加载页面时调用的第一件事
//创建一个新的RedeemAPI类的实例并调用redeem方法
$ api = new RedeemAPI;
$ api-> redeem();
?>
当我将以下参数写入终端窗口
curl -Fname = deneme-Fdescription = deneme-Furl = deneme-Fusers_id = 705735067http://website.local/index。因此,我看到从图片选择工作,但是当我去数据库,并检查是否是新的。行是我看不到行。
php脚本有什么问题?为什么在插入无效时选择工作
------编辑--------------------- -
我已排列insert以获取错误消息
// insert
$ stmt = $ this-> db-> prepare(INSERT INTO pictures(name,description,url,users_id)VALUES(?,?,?,?
$ stmt-> bind_param(ssss,$ name,$ description,$ url,$ users_id);
$ stmt-> execute();
printf(name:%s\\\
,$ name);
printf(description:%s\\\
,$ description);
printf(url:%s\\\
,$ url);
printf(usersid:%d\\\
,$ users_id);
printf(Errormessage:%s\\\
,$ stmt-> error);
$ stmt-> close();
输出为
name:deneme
description:deneme
url:deneme
usersid:705735067
Errormessage:
解决方案 计数是否有问题?在 prepare
中有4个?
,你在 bind_param )
?
$ stmt = $ this-> db-> prepare(INSERT INTO图片(名称,描述,url,users_id)VALUES(?,?,?,?));
$ stmt-> bind_param(is,$ name,$ description,$ url,$ users_id);
解决方案
是
到ssss
?
I have a table as following
CREATE TABLE IF NOT EXISTS `pictures` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`description` varchar(200) NOT NULL,
`url` varchar(1000) NOT NULL,
`users_id` bigint(20) NOT NULL,
`totalvoteup` int(11) NOT NULL,
`totalvotedown` int(11) NOT NULL,
`totalvoteneutral` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3
I can insert following values in phpmyadmin with sql statement
INSERT INTO pictures(name, description, url, users_id) VALUES ('try','try','try', 12345)
So no problem in database
But if I use following php script for select and insert interactions with my database it doesnt insert anything to database
<?php
// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
function getStatusCodeMessage($status)
{
// these could be stored in a .ini file and loaded
// via parse_ini_file()... however, this will suffice
// for an example
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost', 'root', 'password', 'testBasParmak');
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function redeem() {
// Check for required parameters
if (isset($_POST["users_id"]) && isset($_POST["name"]) && isset($_POST["url"])&& isset($_POST["description"])) {
// Put parameters into local variables
$users_id = $_POST["users_id"];
$name = $_POST["name"];
$url = $_POST["url"];
$description=$_POST["description"];
// insert i also tried with 'Insert .....' rather then "Insert...."
$stmt = $this->db->prepare("INSERT INTO pictures(name, description, url, users_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $name, $description, $url, $users_id);
$stmt->execute();
$stmt->close();
//select
$stmt = $this->db->prepare('SELECT name, description, url, users_id FROM pictures');
$stmt->execute();
$stmt->bind_result($name, $description, $url, $users_id);
while ($stmt->fetch()) {
echo "Name of picture is $name url of picture is $url";
}
$stmt->close();
return true;
}
sendResponse(400, 'Invalid request');
return false;
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?>
When I write following parameters to terminal window
curl -F "name=deneme" -F "description=deneme" -F "url=deneme" -F "users_id=705735067" http://website.local/index.php
As a result I see Select from pictures works but when i go to database and check if the new row is there i cant see the row.
Whats wrong with the php script? Why select works while insert doesnt work
------EDIT-----------------------
I have arranged insert to get an error message
// insert
$stmt = $this->db->prepare("INSERT INTO pictures(name, description, url, users_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $name, $description, $url, $users_id);
$stmt->execute();
printf("name: %s\n", $name);
printf("description: %s\n", $description);
printf("url: %s\n", $url);
printf("usersid: %d\n", $users_id);
printf("Errormessage: %s\n", $stmt->error);
$stmt->close();
output is
name: deneme
description: deneme
url: deneme
usersid: 705735067
Errormessage:
解决方案 Is there an issue here in the count? There are 4 ?
s in the prepare
and you have given 5 params in the bind_param()
?
$stmt = $this->db->prepare("INSERT INTO pictures(name, description, url, users_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("is", $name, $description, $url, $users_id);
Solution
Are you supposed to change the "is"
to "ssss"
?
这篇关于选择工程,插入到mysql不插入php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!