[英]PHP Website works fine on localhost, but not when I upload to server
因此,我編寫了第一個使用mySQL數據庫的php網站。 它在我的本地主機上完全正常,但是當我通過Filezilla將其上傳到服務器時,它沒有。
是因為在一種情況下我仍然指向本地主機。 如果是這樣,我應該指向什么呢? 有什么我完全想念的嗎? 關於將mySQL數據庫導出到服務器的事情? 任何想法,建議將不勝感激!
<?php
function getConnected($host,$user,$pass,$db) {
$db = new mysqli($host, $user, $pass, $db);
return $db;
}
$db = getConnected('localhost','root','*********','msg_app');
?>
我得到這些錯誤:
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
這是我的代碼:
<!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>
您的問題顯然在MySQL憑證中:
警告:mysqli :: mysqli()[mysqli.mysqli]:(28000/1045):在/ home / hjaramil / public_html / tell_me / db / connect中對用戶'root'@'localhost'的 訪問被拒絕 (使用密碼:是) .php第5行
您將被拒絕使用root
用戶名和password
來訪問localhost
MySQL服務器。 100%的沒人應該在其本地桌面之外的服務器上使用root
。 而且我懷疑您使用的遠程服務器是否允許root
訪問。 您需要使用憑據和數據庫在服務器上進行數據庫設置。 一個很好的機會是它將是一個localhost
連接,但是如果沒有,則需要該信息。
因此,總結一下,您需要以下內容:
localhost
但可能不取決於您的服務器設置。 獲取這些,將其替換為您的腳本,您應該一切順利。
您必須確保通過托管網站的Web服務器實際設置了數據庫-托管服務網站中可能有一個稱為托管工具的選項,該選項將在其中存在。 設置好之后(無需執行任何MySQL服務器下載操作),就可以使用為MySQL服務器指定的IP地址(幾乎可以肯定與您網站的IP地址不同)作為IP地址。主辦。 看來您所有的錯誤都是由於此。 為了安全起見,您可能還想在發布示例時更改用戶名和密碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.