Help me pls, php newbie with php problem..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Help me pls, php newbie with php problem..

Post by joandong2 »

HELP ME PLEASE, i keep getting this error, and with regards to header eerror, i tried replacing it with a script, it's ok, but after i login, it will not direct to the index.php, im a newbie, :(

Code: Select all

[u]index.php[/u]
<?php
session_start();

if (!isset($_SESSION['user_id'])){
	header('location: login.php');
}


?>
<html>

<title> WEEEE </title>

<h1><b>SALES AND INVENTORY</b></h1>
	<form action='sales.php' method='post'>
	<input type='submit' name='new_sale' value='new_sale'/>
	</form>
</html>	


[u]login.php[/u]

<?php
include("connection.php");
			
				
if (isset($_POST['login'])) {
		$username = trim(($_POST['user']));
		$password = trim(($_POST['pass']));
		//$hashed_password = sha1($password);
		$query = "SELECT * FROM users where username='$username' and password='$password'";
		$result = mysql_query($query) or die();
		
		if (mysql_num_rows($result) == 1) 
			{
				// username/password authenticated
				// and only 1 match
				session_start();
				$user = mysql_fetch_row($result);
				$_SESSION['user_id'] = $user['id'];
				$_SESSION['username'] = $user['username'];
				echo '<script>alert("User confirmed.");location.href="index.php";</script>';
			}
		else 
			{
				// username/password combo was not found in the database
				echo "invalid user!";
			}		
}
?>


<html>
<br /><br /><br />
<form action="login.php" method="post">
<table align="center" >
<tr>
<td>USERNAME:</td>
<td><input type="text" name="user"   maxlength="30"></td>
</tr><tr>
<td>PASSWORD</td>
<td><input type="password" name="pass"   maxlength="30"></td>
</tr><tr>

<td><input type="submit" name="login" value="login"></td>
</tr>

</tr>
</form>
</html>
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Help me pls, php newbie with php problem..

Post by mikecampbell »

session_start() must be called before you send anything to the browser. So move down the <span>...</span> bit at the top (you'd want it to come after your opening html tag anyways).
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

which page are you refering to master?? im sorry, im a newbie,, :( , i tried moving the position of the sessions,but still this error exist, i would like to eliminate the direct acces of the index.php without logging in..
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Help me pls, php newbie with php problem..

Post by mikecampbell »

Remove or move <span style="text-decoration: underline">index.php</span> from the top of login.php.
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

oh, its a different page, i have a problem with my sessions, ????
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help me pls, php newbie with php problem..

Post by Christopher »

You are showing the source for index.php, but the error message is for login.php. The problem is that there is some output before the call to session_start(). It could be a space or return or tab, but there is some output.

Also, you can just copy and paste the error message. You don't need huge screenshots for error messages. ;)
(#10850)
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

im sorry for that sir, sir can u help me identify those output? white spaces?/ huhuhu,, i read articles regarding thos spaces, but i can't find it, :( :( :(
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

help pls?? again?? i've been thinking what wrong with this,, when i hit record button, it will insert date, and total sales into a table, and that seems ok, but if there are no sales/ sold product, the total will be zero, but when i hit record button, it will not insert into the table. what's wrong?? huhu, everyting is fine if there is sold items, even if the total sales is 0, but if no sold items, and total is 0, it ain't inserting.. help meeee plsss...... :( :( :(


Code: Select all

[u]view_sales.php[/u]

<?php
//include("header.php");
include("connection.php");

if (isset($_POST['fr_sub'])){
$date= $_POST['st_date'];
$total=0;
	$query = "SELECT * FROM sales where date='$date'";
	$result = mysql_query($query) or die(mysql_error()); 
	
	if (mysql_num_rows($result) == 0)
	
	{
		echo "NO SALES-----";
		
	}
	else 
	{
	echo "<table cellpadding='2'>";
	echo "<h2>DATE: <b>".$date."</b></h2>";
	echo "<tr><th>SALES ID</th><th>TIME</th><th>TOTAL PAYMENTS</th><th>CASH</th><th>CHANGE</th></tr>";

		while($row = mysql_fetch_array($result))
			{
			$id=$row['sales_id'];
			
			//echo "<form action ='view_sales4.php' method='get'>";
			echo "<tr><td style='text-align:center'>";
			echo $id; 
			

			//echo "<input type='hidden' name='sel_id' value=".$row['sales_id'].">";
			echo "</td><td>";
			echo $row['time'];
			echo "</td><td style='text-align:center'>";
			echo "P".$row['t_pay'];
			echo "<input type='hidden' value='".$row['t_pay']."'>";
			echo "</td><td style='text-align:center'>";
			echo "P".$row['cash'];
			echo "</td><td style='text-align:center'>";
			echo "P".$row['change'];
			//echo "</td><td>";
			//echo "P".$row['price'];
			//echo "</td><td>";
			//echo $row['quantity'];
			echo "</td><td>";
			//echo '<a href="" rel="facebox">view sales</a>';
			//echo "<input type='submit' name='view' value='view' >";?>
			<a href="view_sales4.php?id=<?php echo $id; ?>" rel="facebox"><?php echo "view sale"; ?></a>
			<?php echo "</td></tr>";echo "</form>";
			$total += $row['t_pay'];
			
			}
			}
			echo "</table>";
			echo "<table>";
			echo "<form action ='daily_sales.php' method='post'>";
			//echo "<br />";
			echo "<hr><tr>";
			echo "<td>";
			echo "<input type='hidden' name='date' value='".$date."'>";
			echo "</td><td>";
			echo "TOTAL SALE: <b>P".$total."</b>";
			echo "</td><td>";
			echo "<input type='hidden' name='total' value='".$total."'>";
			echo "</td><td>";
			echo "<input type='submit' name='record' value='RECORD' class='button'>";
			echo "</td></tr>";
			echo "</form>";
			echo "</table>";

	
}
?>

[u]daily_sales.php[/u]

<?php

include("connection.php");

if(isset($_POST['record'])){

$total=	$_POST['total'];
$date= $_POST['date'];

$query="SELECT * FROM daily_sales2 where '$date'=date"; 
$result=mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
				if(mysql_num_rows($result)>0)
				{
					echo '<script>alert("GA CGE LANG TAG RECORD ANI!");location.href="index.php";</script>';
				}
		else
			{
				
				$query = "INSERT INTO daily_sales2 values (null,'$date','$total')";
				$result = mysql_query($query) or die('error');
				echo '<script>alert("MANIRA NATA!");location.href="index.php";</script>';
				
							
			}
}

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help me pls, php newbie with php problem..

Post by Christopher »

Based on the logic at the bottom of your script, it will only INSERT in the else for if(mysql_num_rows($result)>0).
(#10850)
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

sir help me pls?? if the date is already recorded in the table == exit, else insert values?? what's wrong sir?? i also try removing the if statement and put the insert function directly, still if there are no sold items and the total sale is 0, it aint recording.... huhuhuhuu helllpp????????????? plssss??????????????
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

help?
joandong2
Forum Newbie
Posts: 8
Joined: Wed Oct 06, 2010 10:30 am

Re: Help me pls, php newbie with php problem..

Post by joandong2 »

huhu?help?
Post Reply