Jump to content

[SOLVED] PHP calling a function and returning an error, failing


mike12255

Recommended Posts

here is my index.php file

 

 

<?php

include "universal.php";

if($_POST['submit']){
user_login($_POST['username'],$_POST['pwd']);
}


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
background-color: #999999;
}

#box{
border:2px #000000;
background-color:#CCCCCC;
width:400px;
height:300px;

}
-->
</style></head>

<body>
<div id="box">
<form method="post"  action="index.php">
                <p align="center"><span class="style1 style12 style20">Username: </span> 
                  <input type="text" name="username">
                  <br>
                   <p align="center"><span class="style1 style12 style20">Password: </span> 
                  <input type="password" name="pwd">
                  <br>
                  <input type="submit" name="login" value="Login">
                </p>
              </form><br />
<br />
<?php
if ($error){
echo $error;
}

?>
</div>
</body>
</html>

 

 

 

and here is my univeral.php file:

 

 

<?php
/*
*********************************************
********UNIVERAL CMS FUNCTIONS***************
********FOR STUDENT WEB PRO******************
********DEVELOPED BY: MICHAEL H**************
******* JULY 18 2009************************
*********************************************
*/


//These are required on all pages so make sure they get called as soon as file is included
session_start();
include ("connect.php");


function user_login ($user,$pass,$error){
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$sql = "SELECT * FROM tbl_users WHERE username = '".$user."'AND password = '".$pass."'";
$res = mysql_query($sql) or die (mysql_error());

if (mysql_num_rows($res) > 0){
$_SESSION['user'] = $user;
header ("Location: editpages.php");

}else{
$error = "Sorry bad username/password comination";
return $error;
}
}// end of login function

//MAKE SURE ADMIN IS LOGGED ON ON ADMIN PAGES
function check_status($session){
if ($_SESSION['user']){
return true;
}else{
return false;
}

}

//CREATE THE BOX
function create_box($page){

$sql = "SELECT * FROM tbl_pages WHERE page = '".$page."'";
	$res = mysql_query($sql) or die (mysql_error());
	while ($row = mysql_fetch_assoc($res)){
	$info=$row['info'];
	}




echo "<form method=\"post\" action=\"edit.php?page= . '$page' .\">";

echo "<div>";

	echo "<p>Design exactly how you want the Wine page to look in this: click submit when finished.</p>";

echo "<div>";
		echo "<textarea id=\"info\" name=\"info\" rows=\"15\" cols=\"70\" style=\"width: 80%\">";
	echo $info;
		echo "</textarea>";
	echo "</div>";
	echo "<input type=\"submit\" name=\"save\" value=\"Submit\" />";
echo "</div>";
echo "</form>";
}

//ENTER TEXT BOX INFO INTO DB
function insert_info($info,$page){
$sql = "UPDATE tbl_pages SET info = '".$info."' WHERE page = '".$page."'";
mysql_query($sql) or die (mysql_error());
header ("Location pageedits.php");

} 

// LOG THE USER OUT
function user_logout(){

unset($_SESSION['user']);
session_destroy();
header("Location: index.php");
}
?> 

 

 

for some reason when i enter nothing and click submit the error message dosnt appear, am i calling the function wrong?

Well, you're returning the errors that you get from the function. Using:

 

return $error;

 

Will simply return the string that $error contained. Do this instead:

 

<?php

include "universal.php";

if($_POST['submit']){
$logincheck = user_login($_POST['username'],$_POST['pwd']);
}


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
background-color: #999999;
}

#box{
border:2px #000000;
background-color:#CCCCCC;
width:400px;
height:300px;

}
-->
</style></head>

<body>
<div id="box">
<form method="post"  action="index.php">
                <p align="center"><span class="style1 style12 style20">Username: </span> 
                  <input type="text" name="username">
                  <br>
                   <p align="center"><span class="style1 style12 style20">Password: </span> 
                  <input type="password" name="pwd">
                  <br>
                  <input type="submit" name="login" value="Login">
                </p>
              </form><br />
<br />
<?php
if (!$logincheck) {
echo $logincheck;
}

?>
</div>
</body>
</html>

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.