Jump to content

jepperask

New Members
  • Posts

    3
  • Joined

  • Last visited

jepperask's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi guys - I've had problems with this for a couple of hours now, so now im asking you! ;-) I'm creating a blog, where i can log in in the header.php file (which i include on all the pages, as a sticky "move-around" -toolbar. But I would like the form to be removed when I am logged in, and instead have a log out button and preferences (or just to be removed as a start). I've tried to solve it by adding !$_session['user_id']{*FORM-CODE-WITH-INPUT-AREA-HERE}; meaning, that if the user is logged in, the above thing wont even come up. But i couldn't really make it work. Heres my code: <?php session_start(); if(isset($_POST['submit'])) { $user = $_POST['username']; $pwrd = $_POST['pwrd']; //include database connection include 'db_connect.php'; if(empty($user) || empty($pwrd)){ echo("Missing Information"); }else{ $user = strip_tags($user); $user = $db->real_escape_string($user); $pwrd = strip_tags($pwrd); $pwrd = $db->real_escape_string($pwrd); $pwrd = md5($pwrd); $query = $db->query("SELECT user_id, username FROM user WHERE username='$user' AND password='$pwrd'"); if($query->num_rows == 1){ while($row = $query->fetch_object()){ $_SESSION['user_id'] = $row->user_id; } header('Location: admin-page.php'); exit(); }else{ echo "Missing Information!"; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://code.jquery.com/jquery-1.5.min.js"></script> </head> <body> <div id="container"> <form action="login.php" method="post"> <p> <label>Username</label><input type="text" name="username"> <label>Password</label><input type="password" name="pwrd"> <input type="submit" name="submit" value="LogIn"> </p> </form> </div> </body> </html> And the code is by the way just the login.php, which i already included in the header.php.
  2. Hi, thanks for the reply. But i actually got another answer from a friend, who showed me that it could be the alignment of all my code. So he changed it a bit, and this worked: <?php session_start(); mysql_connect("localhost", "root", "hejmeddig") or die(mysql_error()); mysql_select_db("pentakill_dk_db") or die(mysql_error()); include 'config.php'; if (isset($_POST['post_id'])) { $post_id = $_POST['post_id']; if (isset($_POST['delete_id'])) { mysql_query("DELETE FROM posts WHERE post_id = " . $post_id); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> #delete-post { width: 100%; margin: auto; background-color: #999; } </style> <script src="http://code.jquery.com/jquery-1.5.min.js"></script> </head> <body> <div> <table border cellpadding="3"> <?php $data = mysql_query("SELECT * FROM posts") or die(mysql_error()); while($info = mysql_fetch_array($data)){ ?> <tr> <th> <form method="post" action=""> <input type="hidden" name="post_id" value="<?php echo $info['post_id']; ?>" /> <input type="submit" name="delete_id" value="Delete" /> </form> </th> <th> Post-Title: </th> <td> <?php echo $info['title']; ?> </td> <th> Body: </th> <td> <?php echo $info['body']; ?> </td> </tr> <?php } ?> </table> </div> </body> </html> The thing that changed it all, was moving the database connection, to right after the session-start.
  3. Hello! I'm making a blog and I'm almost done with it. I'm at the point, where i have a page and I've created a while-loop, that shows every post on the page. I even asigned the delete-button, but for some reason it doesn't work. Could you take a look at my code and help me out? Thanks in advance. <?php session_start(); if(!isset($_SESSION['user_id'])){ header('location: index.php'); exit(); } if (isset($_POST['post_id'])) { $post_id = $_POST['post_id']; if (isset($_POST['delete_id'])) { mysql_query("DELETE FROM posts WHERE post_id = " . $post_id); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> #delete-post { width: 100%; margin: auto; background-color: #999; } </style> <script src="http://code.jquery.com/jquery-1.5.min.js"></script> </head> <body> <div> <table border cellpadding="3"> <?php mysql_connect("localhost", "root", "hejmeddig") or die(mysql_error()); mysql_select_db("pentakill_dk_db") or die(mysql_error()); $data = mysql_query("SELECT * FROM posts") or die(mysql_error()); while($info = mysql_fetch_array($data)){ ?> <tr> <th> <form method="post" action=""> <input type="hidden" name="post_id" value="<?php echo $info['post_id']; ?>" /> <input type="submit" name="delete_id" value="Delete" /> </form> </th> <th> Post-Title: </th> <td> <?php echo $info['title']; ?> </td> <th> Body: </th> <td> <?php echo $info['body']; ?> </td> </tr> <?php } ?> </table> </div> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.