【问题标题】:this php code works in localhost but not on server?这个 php 代码在 localhost 中有效,但在服务器上无效?
【发布时间】:2020-07-02 21:35:01
【问题描述】:

我们正在制作一个即将存储网站名称、类别和详细信息的拼贴项目,因此我们完成了网站,现在我们想要制作将通过 API 与服务器通信的应用程序。当数据被添加到数据库 echo json_encode(true) 时,我们在 php 脚本中有这样的简单代码。正如android程序员所知,改造库使用键值对类型机制,所以我们更新了以下代码。

<?php

    $websiteName = $_POST['website_name'];
    $websiteCategory = $_POST['website_cat'];
    $websiteDetails = $_POST['website_details'];

    try {
        $pdo = new PDO('mysql:host=localhost;dbname=website' , 'root' , '');
        $sql = 'INSERT INTO website_data SET website_name = :website_name , website_cat = :website_cat , website_details = :web_del';

        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':website_name' ,$websiteName);
        $stmt->bindValue(':website_cat' ,$websiteCategory);
        $stmt->bindValue(':web_del' ,$websiteDetails);
        $stmt->execute();

        echo json_encode(['response' => 'true']);
    } catch (PDOException $e) {
        $msg = $e->getMessage();
        echo json_encode(['response' => $msg]);
    }
?>

这里是使用 ajax 执行操作的 javascript 代码

function sendData(websiteName , categoryName , websiteDetails){
    var params = 'website_name='+websiteName+'&website_cat='+categoryName+'&website_details='+websiteDetails;
    var xml = new XMLHttpRequest();
    xml.onreadystatechange = function(){
        if(this.status == 200 && this.readyState == 4){
            var response = this.responseText;
            var responseJson = JSON.parse(response);
            console.log('resonse from server' , responseJson['response']);
            if(responseJson['response'] == 'true'){
                addToCurrent(websiteName , categoryName , websiteDetails);
            }else{
                alert('unfortunatley data could not added succesfully');
            }
        } else{
            console.log('there is some problem with sever');
        }
    }
    xml.open('POST' , '../php/addNewWebsite.php' , true);
    xml.setRequestHeader('Content-type' , 'application/x-www-form-urlencoded');
    xml.send(params);
}

这在 localhost 中的缩进效果很好,但它在我们的免费服务器中不起作用,它显示 alert('unfortunatley data could not added succesfully');i 不知道为什么会发生这种情况。我们有来自 awardspace.com 的免费服务器和域

更新

这是我从server SQLSTATE[HY000] [2002] No such file or directory 收到的错误消息 谢谢。

【问题讨论】:

  • 确保路径 '../php/addNewWebsite.php' 正确,并检查服务器的 .htaccess 文件中的默认目录是否正确设置。
  • 您是否添加了正确的连接服务器凭据。在这一行 new PDO('mysql:host=localhost;dbname=website' , 'root' , '');
  • @Gaurav 是的,我已经添加了正确的凭据,否则它将无法在 localhost (xampp) 中工作
  • @Shaam 由于免费服务器,我无法访问 .htaccess 文件,但在更新代码之前它运行良好,我正在考虑用旧文件替换这些文件。
  • 服务器和本地主机是否具有相同的数据库凭据??

标签: php sql json ajax xml


【解决方案1】:

我过去也遇到过类似的问题。您应该尝试将localhost 更改为127.0.0.1,假设您的 MySQL 服务器在同一个机器上运行。

我认为您遇到的问题是“localhost”使用 UNIX 套接字并且无法在标准目录中找到数据库。但是“127.0.0.1”使用 TCP ,这实质上意味着它通过机器上的“本地互联网”运行,比 UNIX 套接字可靠得多。

【讨论】:

  • @buttface614 你说的对 localhost 但在我的情况下它没有用我的代码在 localhost 上工作正常但在服务器上没有。
  • @communityPeople 所以我别无选择,我在该站点创建了另一个帐户并上传了代码并猜测它工作正常。我无法弄清楚旧网站发生了什么,但在新网站中代码按预期运行。两台服务器都有相同的代码文件,虽然它不工作。感谢所有试图帮助我并给我建议的社区人士,谢谢
猜你喜欢
  • 1970-01-01
  • 2018-04-02
  • 2013-01-25
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多