Here is my code:
<?php
$dbhost = "localhost"; // The Host you are using
$dbuser = "user"; // The username
$dbpass = ""; // Password. If no password is set, leave this option blank.
$dbname = "test"; // The name of the database
mysql_connect("$dbhost", "$dbuser", "$dbpass", "$name") or die(mysql_error()); //We connect to the host
mysql_select_db("$dbname"); //Select the database
?>
<html>
<head>
<title>MikeX PHP</title>
<style type="text/css">
<!--
* {
margin: 0;
padding: 0;
}
form {
width: 350px;
font-family: Arial, Helvetica, sans-serif;
font-weight:bold;
font-size : 12px;
margin-bottom:10px;
margin: auto;
}
#comments {
width: 360px;
height: 500px;
border: 1px solid black;
margin: auto;
padding: 10px;
text-align: left;
}
p {
font-family: "Century Gothic", verdana, ariel, sans-serif;
font-size: 14px;
color: black;
border-bottom: 1px dotted black;
padding-bottom: 10px;
}
h1 {
font-family: "Century Gothic", verdana, ariel, sans-serif;
font-size: 16px;
color: black;
font-style: italic;
}
hr {
border: 1px dotted black;
height: 1px;
}
em {
font-family: "Century Gothic", verdana, ariel, sans-serif;
font-size: 10px;
color: black;
}
-->
</style>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>Namn: </td><td class="error">
</td>
</tr>
<tr>
<td colspan="2"><input class="text" type="text" id="name" name="name" />
</td>
</tr>
<tr>
<td>Meddelande:</td>
<td class="error"></td>
</tr>
<tr>
<td colspan="2"><textarea cols="40" rows="6" name="comments" />
</textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input class="text" type="submit" name="id" value="Skicka" />
</td>
</tr>
</table>
</form>
<div id="comments">
<?php
$date = date("H:i:s");
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$post = $_REQUEST['comments'];
if($_REQUEST['name'] && $_REQUEST['comments']){
$result = mysql_query("INSERT INTO `test` (id, name, post) VALUES('$id', '$name', '$post')") or die(mysql_error());
exit;
}
while($row = mysql_query("SELECT `test` (id, name, post) VALUES('$id', '$name', '$post') ORDER BY `id`")){
echo stripslashes("<h1>$row[0]");
echo "</h1>";
echo "<em>$date</em>";
echo stripslashes("<p>$row[1]");
echo "</p>";
}
?>
<!--<h1></h1>
<p></p>-->
</div>
</body>
</html>
Put the following SQL code in PhpMyadmin to make the table work:
CREATE TABLE `test` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(64) default NULL,
`post` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9;
I can insert data into the database but i can't view them, and I can't seem to figure out what the problem is.
I am pretty new to PHP so there may be obvious misstakes
