duanhuang7591 2013-11-16 00:50
浏览 37

PHP网站在localhost上工作正常,但是当我上传到服务器时却没有

So I coded my first php website that's using a mySQL database today. It works totally fine on my localhost, but when I upload it to the server via Filezilla, it doesnt.

Is it because I am still pointing to localhost in one case. If so, what should i be pointing to instead? Is there something I am completely missing? Something about exporting the mySQL database to the server? Any ideas, advice would be super appreciated!

<?php

function getConnected($host,$user,$pass,$db) {

   $db = new mysqli($host, $user, $pass, $db);

   return $db;
}

$db = getConnected('localhost','root','*********','msg_app');

?>

I get these errors:

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /home/hjaramil/public_html/tell_me/db/connect.php on line 5

Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in /home/hjaramil/public_html/tell_me/index.php on line 33

Fatal error: Call to a member function bind_param() on a non-object in /home/hjaramil/public_html/tell_me/index.php on line 34

This is my code:

<!DOCTYPE html>
<html class="no-js" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>A Message For You</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link href="css/application.css" rel="stylesheet">
  <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico">
  <link href="//netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.css" rel="stylesheet">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Lusitana|Open+Sans|Varela+Round|Quattrocento+Sans' rel='stylesheet' type='text/css'>
</head>


<?php 
//error_reporting(0);
require 'db/connect.php'; 
require 'functions/security.php'; 
error_reporting(E_ALL);

$records = array();

if(!empty($_POST)){


  if(isset($_POST['first_name'],$_POST['location'],$_POST['message'])) {

    $first_name = trim($_POST['first_name']);
    $location   = trim($_POST['location']);
    $message    = trim($_POST['message']);

    if(!empty($first_name) && !empty($location) && !empty($message)) {
      $insert = $db -> prepare("INSERT INTO message (first_name, location, message, date) VALUES (?,?,?,NOW())");
      $insert->bind_param('sss',$first_name,$location,$message);

      if($insert -> execute()){
        header ('Location: index.php');
        die();
      }
    }

  }
}


if($results = $db->query("SELECT * FROM message ORDER BY id DESC LIMIT 1")) {
  if($results->num_rows) {
    while($row = $results->fetch_object()) {
      $records[] = $row;
    }
    $results -> free();
  }
}

?>



    <?php
      if (!count($records)){
        echo 'No messages';
      } else{

    ?>
      <?php
      foreach($records as $r){
      ?>


<body>
<div class="msg-container">
  <div class="msg-intro">
   <p> I wanted to tell you that:</p>
  </div>
  <div class="msg-content">
    <p><?php echo escape($r->message); ?></p>
  </div>
  <div class="msg-who">
    <p class="name"><?php echo escape($r->first_name); ?></p>
    <p class="location"><?php echo escape($r->location); ?></p>
    <p class="date"><?php echo escape($r->date); ?></p>
  </div>


    <?php
      }}
    ?>

</div>
<div class="form-background">
  <div class="form-container">
  <div class="form-title">
    <p>What do you want to tell?</p>
  </div>
    <form action="index.php" method="post">
          <div class="field msg">
            <!--<label for="message">Message</label>-->
            <textarea name="message" id="message" autocomplete="off"></textarea>
          </div>
          <div class="fields">
          <ul>
            <li> <label for="first_name">Name</label> </li>
            <li> <input type="text" class="s-input" name="first_name" id="first_name" autocomplete="off"> </li>
          </ul>
          </div>
          <div class="fields last">
            <ul>
              <li><label for="location">Location</label></li>
              <li><input type="text" class="s-input" name="location" id="location" autocomplete="off"></li>
            </ul>
          </div>
          <input type="submit" value="Post" id="submit-button">
      </form>
  <div>
</div>






</body>
<script type="text/javascript" src="js/application.js"></script>
</html>
  • 写回答

2条回答 默认 最新

  • douyinyi7766 2013-11-16 00:57
    关注

    You have to make sure that you actually set up a database via your web server that is hosting your website-there is probably some option within your hosting services website called hosting tools where this option will be. Once you set that up(you won't have to perform any sort of download of the MySQL server), you use the IP address given for the MySQL server (which will almost certainly be different than the IP for your web site) as the host. It appears that all of your errors are due to this. You also may want to change your username and password when posting examples-just to be safe!

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行