选择作品,插入mysql不插入php

时间:2012-07-01 19:51:27

标签: php mysql curl phpmyadmin

我有一张表格如下

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 

我可以在phpmyadmin中使用sql语句

插入以下值
INSERT INTO pictures(name, description, url, users_id) VALUES ('try','try','try', 12345)

数据库没问题

但是,如果我使用以下php脚本来选择和插入与我的数据库的交互,它不会向数据库插入任何内容

<?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();

?>

当我将以下参数写入终端窗口时

curl -F "name=deneme" -F "description=deneme" -F "url=deneme" -F "users_id=705735067" http://website.local/index.php

结果我看到选择从图片工作,但当我去数据库并检查新行是否在那里我不能看到该行。

php脚本有什么问题?为什么在插入不起作用时选择作品

------ EDIT -----------------------

我已安排插入以获取错误消息

// 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();

输出

name: deneme
description: deneme
url: deneme
usersid: 705735067
Errormessage: 

2 个答案:

答案 0 :(得分:0)

计数中是否存在问题? ?中有4个prepare,您在bind_param()中有5个参数?

$stmt = $this->db->prepare("INSERT INTO pictures(name, description, url, users_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("is", $name, $description, $url, $users_id);

解决方案

您是否应该将"is"更改为"ssss"

答案 1 :(得分:0)

尝试检查错误:$mysqli->error()