blen Posted November 10, 2006 Share Posted November 10, 2006 ok so im pretty new to php. I was doing some tutorials and created a login form, to go to a page where I can update the news on my page. This is what I have so far, however, when I go to post it keeps saying that I'm not logged in. any ideas to improve this code?[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Add an entry!</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><?php$conn = mysql_connect("localhost","usr","pwd");$db = mysql_select_db("kendall_users");$username = $_POST["username"];$password = $_POST["password"];$result = MYSQL_QUERY("SELECT * from bk_login WHERE username='$username'and password='$password'")or die ("Name and password not found or not matched");$worked = mysql_fetch_array($result);$username = $worked[username];$password = $worked[password];if($worked){//logged inif($_POST['submit']) {//submit is hit//then connect as user//change user and password to your mySQL name and passwordmysql_connect("localhost","usr","pwd");//select which database you want to editmysql_select_db("kendall_news");//convert all the posts to variables:$title = $_POST['title'];$content = $_POST['content'];$date = $_POST['date'];$time = $_POST['time'];//Insert the values into the correct database with the right fields//mysql table = news//table columns = id, title, content, who, date, time//post variables = $title, $content, '$who, $date, $time$result=MYSQL_QUERY("INSERT INTO news (id, title, content, date, time)"."VALUES ('NULL', '$title', '$content', '$date', '$time')");//confirmecho "You posted a news entry.";}else{//put out form?><form method="post" action="add.php"><TABLE><TR><TD>Title:</TD><TD><INPUT TYPE='TEXT' NAME='title' VALUE='Random Update' size=60></TD></TR><TR><TD>Content:</TD><TD><INPUT TYPE='TEXT' NAME='content' VALUE='' size=60></TD></TR><br><TR><TD>Date:</TD><TD><INPUT TYPE='TEXT' NAME='date' VALUE='<? echo date("M.j.y"); ?>' size=60></TD></TR><TR><TD>Time:</TD><TD><INPUT TYPE='TEXT' NAME='time' VALUE='<? echo date("g:i a"); ?>' size=60></TD></TR><TR><TD></TD><br><TD><INPUT TYPE="submit" name="submit" value="submit"></TD></TR></TABLE></form><?php}}else{//not logged inecho "You are not logged in.";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/ Share on other sites More sharing options...
jsladek Posted November 10, 2006 Share Posted November 10, 2006 I would recommend rethinking the program flow. Maybe somthing like thisif($_POST['submit']) {....code to check the database for the user} else {....code to display the Form for the user to fill out}Also the values for uername and password are going to have to be included in the form in order for them to be gotten from the $_POST["username"]; And if they wind up being what is in the database why would you have to store the same thing into the same var? Regards,John Sladek Link to comment https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/#findComment-122438 Share on other sites More sharing options...
blen Posted November 10, 2006 Author Share Posted November 10, 2006 that is how the flow is, however its nested in another if statement, so I can check if the user is validly logged in. Link to comment https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/#findComment-122481 Share on other sites More sharing options...
exoskeleton Posted November 10, 2006 Share Posted November 10, 2006 hi maybe you must interchange these lines of code here:if($worked){//logged inif($_POST['submit']) {maybe it should look like this:if($_POST['submit']) {if($worked){//logged inhope this will help...GOod Luck!!More power... Link to comment https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/#findComment-122519 Share on other sites More sharing options...
jsladek Posted November 10, 2006 Share Posted November 10, 2006 Lets say the user has never visited this page, why run all the php checks in the database which will definatly fail because there is no username or password, so why run that code every time? The first time a user visits the page the if($_POST['submit']) will not be true and the user will be displayed the form to submit their infomation. Once the user fills out the form and submits it, I'm assuming to the same php script. Then the if($_POST['submit']) will be true and you can do your validation and insert stuff.I don't see anywhere in your form that you give the user to put in their password and username. So how are the following lines going to work? [code]$username = $_POST["username"];$password = $_POST["password"];[/code]Regards,John Sladek Link to comment https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/#findComment-122758 Share on other sites More sharing options...
exoskeleton Posted November 11, 2006 Share Posted November 11, 2006 i just assumed that he knows the next step sir, sorry.... Link to comment https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/#findComment-122981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.