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>