Reputation: 105
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>
Upvotes: 0
Views: 2908
Reputation: 26034
Your problem is clearly in the MySQL credentials:
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
You are being denied access to the localhost
MySQL server using the username of root
and whatever password
you have. 100% of nobody should use root
on a server outside of their local desktop. And I doubt that the remote server you are using allows root
access. You need to get a DB setup on the server with credentials & a database. A good chance is that it will be a localhost
connection, but if not ou need that info to.
So to summarize you need the following:
localhost
but might not be depending on your server setup.Get those, replace them in your script & you should be good to go.
Upvotes: 1
Reputation: 111
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!
Upvotes: 0