【问题标题】:Why are my php crud operations working in localhost, but not server?为什么我的 php crud 操作在本地主机中工作,而不是服务器?
【发布时间】:2016-07-28 19:44:22
【问题描述】:

我有一个在本地主机上运行良好的创建函数,但在我的服务器上却不能运行。我的意思是没有任何东西存储在数据库中,而它在本地主机上存储。顺便说一句,我使用winhost。代码如下:

public function create ($title, $content, $date, $price){      

$db = Dbclass::getDB();
$query = "INSERT INTO upcoming_albums (albums_title, albums_content, albums_date, albums_price, albums_img_path)
         VALUES (:title, :content, :date, :price, :path)";
$statement = $db->prepare($query);
$statement->bindParam(':title', $title, PDO::PARAM_STR, 50);
$statement->bindParam(':content', $content);
$statement->bindParam(':date', $date);
$statement->bindParam(':price', $price, PDO::PARAM_STR, 10);
$statement->bindValue(':path', $this->target_path, PDO::PARAM_STR);

$statement->execute();

} 

这是我的表格:

<form action="" enctype="multipart/form-data" method="post" id="form_c">
        <label for="title">Title:</label>
            <input type="text" name="title" id="title"/>
        <label for="content">Content:</label>
            <textarea name="content" id="content"></textarea>
        <label for="date">Release Date (RRRR-MM-DD):</label>
            <input type="text" name="date" id="date"/>
        <label for="price">Price:</label>
            <input type="text" name="price" id="price"/>
        <label for="album_img">Price:</label>
            <input type="file" name="file" id="album_img"/>
            <span>
                <?php     
                    if (isset($_POST['btn_submit'])) {
                        $upcoming_album->uploadImg();  
                    }
                ?>
            </span>
        <button type="submit" name="btn_submit">Submit</button>
    </form>

这是与设置创建方法的表单位于同一文件中的代码:

<?php
require_once "../../controllers/admin/Album.php";
$upcoming_album = new Album;
if (isset($_POST['btn_submit'])) {
    $upcoming_album->setFile();
    $upcoming_album->create($_POST['title'],$_POST['content'],$_POST['date'],$_POST['price']);
}
$albums = $upcoming_album->read();
?>

更新方法也不起作用。但是,删除方法就像一个魅力。 CRUD 操作在 localhost 中运行良好。

【问题讨论】:

  • error_reporting(E_ALL); ini_set('display_errors', '1');
  • 首先为您的数据库查询实现一些基本的错误控制/错误处理——现在您忽略任何可能发生的错误。
  • @abracadaver 我试过把它放在多个位置,但什么也没发生。我应该把它放在哪里?

标签: php server localhost


【解决方案1】:

好吧!但是在您看来,PHP crud 在本地服务器上运行良好,但在服务器上却不行。问题可能是一些问题,例如:-

  1. 您正在使用 PDO 在 db 中插入值(即)您在 localhost 上启用了扩展。所以它工作正常。但它可能未在服务器中启用。所以请尝试在您的 PHP 配置中启用。
  2. 或者你可以检查这一行。$db = Dbclass::getDB(); 数据库是否处于活动状态

    回声''; print_r($db);退出;

【讨论】:

  • 1.我不知道如何在 winhost 上修改我的 php 配置。奇怪的是,之前在我的服务器上进行了 crud 操作。 2. 我试过了,现在我得到一个空白页,上面写着“PDO Object ( )”,没有展开它的选项
  • 1.首先检查您的数据库连接。 2. 通过 if (!defined('PDO::ATTR_DRIVER_NAME')) { echo 'PDO 不可用'; 检查 pdo }
  • 我试过了,但没有收到任何消息。 Crud 正在研究我的另一个功能。我可以存储电子邮件地址。我有一种感觉,这可能是我没有发现的一些愚蠢的编码错误……但我把所有东西都复制了一遍。
  • 所以我意识到我今天早些时候重命名的文件仍然存在。我基本上将crud.php复制到album.php中并引用了album.php。现在,当我尝试删除 crud.php 时,它无论如何都找不到t even work in localhost...trying to find the connection to crud but cant。希望我只是错过了一些东西。
  • 所以,正如预期的那样,这是一个非常愚蠢的错误。我没有填写表格中的所有信息,而且我的表格不需要空值。奇怪的是,这是怎么发生的,因为我从本地主机复制了确切的 SQL,我可以在那里输入空值......
猜你喜欢
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2016-08-04
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多