I couldn't find a clear answer to my following problem:
I made a guestbook, which works just fine in localhost and connects without problem to the guestbook DB (phpMyAdmin).
- Code: Select all
(mysql_connect ('localhost','root','password') && mysql_select_db ('guestbook'))
But when I put my guestbook.php on my online server, I can't connect to the guestbook DB, even when I change the localhost to the online server.
Beside my guestbook document (guestbook.php), which XAMPP-items (folder, docs, ..) should be uploaded to the webserver?
I'm very very new to (PHP/SQL/webdev, ...), thanks for helping me out!
Regards
Full code guestbook:
- Code: Select all
<?php
if (mysql_connect ('localhost','root','password') && mysql_select_db ('guestbook')) {
$time = time();
$errors = array();
if (isset($_POST['guestbook_name'], $_POST['guestbook_email'],$_POST['guestbook_message'])) {
$guestbook_name = mysql_real_escape_string (htmlentities ($_POST['guestbook_name']));
$guestbook_email = mysql_real_escape_string (htmlentities ($_POST['guestbook_email']));
$guestbook_message = mysql_real_escape_string (htmlentities ($_POST['guestbook_message']));
if (empty ($guestbook_name) || empty ($guestbook_email) || empty ($guestbook_message)) {
$errors[] = 'All fields are required.';
}
if (strlen ($guestbook_name)>25 || strlen ($guestbook_email)>255 || strlen ($guestbook_message)>255) {
$errors[] = 'One or more feelds exceed the character limit.';
}
if (empty ($errors)) {
$insert = "INSERT INTO entries VALUES ('', '$time', '$guestbook_name', '$guestbook_email', '$guestbook_message')";
if (mysql_query ($insert)) {
header ('Location: '.$_SERVER ['PHP_SELF']);
} else {
$errors[] = 'Something went wrong, please try again later. Thanks.';
}
} else {
foreach($errors as $error) {
echo '<p><strong>'.$error.'</strong></p>';
}
}
}
$entries = mysql_query ("SELECT `timestamp`, `name`, `email`, `message` FROM `entries` ORDER BY `timestamp` DESC");
if (mysql_num_rows ($entries)==0) {
echo '<br>No messages posted yet.';
} else {
while ($entries_row = mysql_fetch_assoc ($entries)) {
$entries_timestamp = Date ('D, d-M Y @ h:i:s', $entries_row ['timestamp']);
$entries_name = $entries_row ['name'];
$entries_email = $entries_row ['email'];
$entries_message = $entries_row ['message'];
echo '<p><strong>Message posted by:</strong> <br> <strong>Name:</strong> '.$entries_name.' <br> <strong>Email:</strong> '.$entries_email.' <br> <strong>Date:</strong> '.$entries_timestamp.'</strong> <br> '.$entries_message.'<hr width="600" style="border: 1px dotted #0099CC" color="#66FFFF" size="1"></p>';
}
}
} else {
echo 'Could not establish connection.';
}
?>
<!--<hr>-->
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<!--<strong> Share your thouhgts with us by leaving a message.</strong><br><br>-->
<br><br>Name:<br><input type:"text" name="guestbook_name" maxlength="25"><br>
Email:<br><input type:"text" name="guestbook_email" maxlength="255"><br>
Message:<br><textarea name="guestbook_message" rows="10" cols="70" maxlength="255"></textarea><br>
<input type="submit" value="Submit Comment">
</form>