Jump to content

Login and post, please help


blen

Recommended Posts

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 in
if($_POST['submit']) {
//submit is hit
//then connect as user
//change user and password to your mySQL name and password
mysql_connect("localhost","usr","pwd");
//select which database you want to edit
mysql_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')");
//confirm
echo "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 in
echo "You are not logged in.";
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26774-login-and-post-please-help/
Share on other sites

I would recommend rethinking the program flow.  Maybe somthing like this

if($_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
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.