Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // Create a database connection.
- $dbhost = "localhost";
- $dbuser = "blogtest";
- $dbpass = "Kapaldo123";
- $dbname = "blog";
- $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
- //Test if connection occurred.
- if (mysqli_connect_errno()) {
- die("Database connection failed: " .
- mysqli_connect_error() .
- " (" . mysqli_connect_errno() . ")"
- );
- }
- //Perform database query
- $name = $_POST['name'];
- $title = $_POST['title'];
- $body = $_POST['body'];
- //This function will clean the data and add slashes.
- // Since I'm using the newer MySQL v. 5.7.14 I have to addslashes
- $name = mysqli_real_escape_string($connection, $name);
- $title = mysqli_real_escape_string($connection, $title);
- $body = mysqli_real_escape_string($connection, $body);
- //This should retrive HTML form data and insert into database
- $query = "INSERT INTO posts (name, title, body)
- VALUES ('".$_POST["name"]."','".$_POST["title"]."','".$_POST["body"]."')";
- $result = mysqli_query($connection, $query);
- //Test if there was a query error
- if ($result) {
- //SUCCESS
- header('Location: activity.php');
- } else {
- //FAILURE
- die("Database query failed. " . mysqli_error($connection));
- //last bit is for me, delete when done
- }
- //mysqli_close($connection);
- ?>
- <?php
- //This will fetch the data from the database
- $query = "SELECT * FROM reviews";
- $result = mysqli_query($connection, $query);
- //Test if there was a query error
- if (!$result) {
- die("Database query failed.");
- }
- // This will let me display the data.
- // The loop will be spilt so I can format with HTML
- while ($row = mysqli_fetch_assoc($result)) {
- //output data from each row
- ?>
- Name: <?php echo $row["name"] . "<br />"; ?>
- Title: <?php echo $row["title"] . "<br />"; ?>
- Review: <?php echo $row["body"] . "<br />"; ?>
- echo "<hr>";
- <?php
- } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement