-
[RESOLVED] PHP Validator/Error checking
Hi,
Does anyone know if and where I can get a php validator/Error checker? I have tried using the built-in validator in Dream Weaver. However, it only works for the html and not the php. I am following instructions that my lecturer has given us but I am missing something and I can not figure out what. This is an example of the code:
PHP Code:
<?php
//Start new session
session_start();
$email = $_POST['Email'];
$password = $_POST['Password'];
if (($email == "[email protected]")&&
($password == "password")){
//Save the values in session variables
$_SESSION['email'] = $email;
echo "<h2>Authorisation success!!!</h2>";
echo "<a href=member_page.php>Click here to go to the members page</a>";
}
else
{
header("Location: login.htm");
exit;
}
?>
With the above code when accessed via wampserver through htm the follow appears on the screen:
Quote:
Authorisation success!!!"; echo "Click here to go to the members page"; } else { header("Location: login.htm"); exit; } ?>
It outputs the code to the screen too and I can not see why?
Thanks,
Nightwalker
-
Re: PHP Validator/Error checking
the file extension needs to be .php not .htm
so myfile.php
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
dclamp
the file extension needs to be .php not .htm
so myfile.php
Yeah, the file is a php file that is being access via another html file! The user enters data into the html page and sends it then the php script responds.
Edit:
The problem with that page was I was using upper letters for the first letter of the field name when they were in-fact lower case in the html.
However, with this code:
PHP Code:
<?php
session_start();
if(isset($_SESSION['email']));
{
$email = $_SESSION['email'];
echo "<h1>Member Page</h1>";
echo "<p><h2>Welcome back $email</h2></p>";
echo "<br /><p><b>Your user email is: $email</b></p><br />";
echo "<p><h2><a href=logout.php>Logout</a></h2></p>";
}
else
{
//Redirected back to login form if not authorised.
header("Location: login.htm");
exit;
}
?>
There is a problem with the else statement! If I comment out the else statement like so:
PHP Code:
//else
//{
//Redirected back to login form if not authorised.
//header("Location: login.htm");
//exit;
//}
The code works normally!
-
Re: PHP Validator/Error checking
well, there's nothing wrong with your syntax. you need to be a little more specific than just "there is a problem."
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
kows
well, there's nothing wrong with your syntax. you need to be a little more specific than just "there is a problem."
I'm not sure what the problem is! I have commented out the else statement as stated above and it works. I just can't see why it's not working with the else in the code.
Edit:
The error points to the line "//else" as a parse error! I'm not sure what that means?
-
Re: PHP Validator/Error checking
Why do you have a semicolon after your if?
PHP Code:
if(isset($_SESSION['email']));
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
kfcSmitty
Why do you have a semicolon after your if?
PHP Code:
if(isset($_SESSION['email']));
It was a typo however, removing it has solved the problem. Thanks!
-
1 Attachment(s)
Re: PHP Validator/Error checking
I can't figure out why this is not working:
login.php
PHP Code:
<?php
//Start new session
session_start();
$email = $_POST['email'];
$password = $_POST['password'];
if (($email == "")&&
($password == "")){
header("Location: login.htm");
exit;
}
else
{
//Connect to the server and select database
require_once ('conn_db.php');
//Create and issue the query
$query = "select * from customers where Email = '$email' AND Password = '$password'";
$result = mysql_query ($query) or die ("Invaid Customer ID or Password");
//Get the number of rows in the results set; should be 1 if there is a match
if (mysql_num_rows($result) == 1){
//If authorized, get values of firstname, lastname, phone and email
$first_name=mysql_result($result, 0, "first_name");
$last_name=mysql_result($result, 0, "last_name");
$phone=mysql_result($result, 0, "phone");
$email=mysql_result($result, 0, "email");
mysql_close();
//Save the values in session variables
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['phone'] = $phone;
$_SESSION['email'] = $email;
echo "<h2>Authorisation success!!!</h2>";
echo "<a href=member_page.php>Click here to go to the members page</a>";
}
else
{
header ("Location: login.htm");
exit;
}
}
conn_db.php
PHP Code:
<?php
# FileName = "conn_db.php"
$hostname = "localhost";
$database = "custdb";
$user = "user";
$pass = "password";
//Connect
$mysql_link = mysql_connect($hostname, $user,$pass) or die ("Unable to connect to mysql server!");
mysql_select_db ($database,$mysql_link) or die ("Unable to select database!");
?>
The form is suppose to connect to the database and display the record matching the email and password. However, I receive this error:
Quote:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: YES) in C:\wamp\www\Lesson 5\conn_db.php on line 9
Unable to connect to mysql server!
That error is referring to the "conn_db.php" posted above. I tried putting user privileges on the database to match above but the page takes a while to send the data then I receive this error, see attachment!
-
Re: PHP Validator/Error checking
sounds like you have an incorrect username or password?
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
kows
sounds like you have an incorrect username or password?
I managed to copy the code from the pdf that we were given! Unfortunately, I can't test to see if it works because of the problem with apache (above).
-
Re: PHP Validator/Error checking
well, the error you posted is just a generic "program not responding/program crashed" error. you could try looking at the Apache error log to see what happened, it should be in /error/ or /logs/ or something similar in the Apache directory. my guess is it won't be incredibly helpful though.
changing MySQL user privileges wouldn't make Apache crash. MySQL and Apache are completely unrelated -- so you must have done something else (or your computer must have, or something).
I'd just reinstall Apache from scratch if you can't get it working.
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
kows
well, the error you posted is just a generic "program not responding/program crashed" error. you could try looking at the Apache error log to see what happened, it should be in /error/ or /logs/ or something similar in the Apache directory. my guess is it won't be incredibly helpful though.
changing MySQL user privileges wouldn't make Apache crash. MySQL and Apache are completely unrelated -- so you must have done something else (or your computer must have, or something).
Quote:
I'd just reinstall Apache from scratch if you can't get it working.
I have reinstalled WampServer twice and Apache still crashes. I am wondering if it was because I edited the wrong files when editing the apache httpd.conf and httpd-ssl.conf before I had setup the program properly. Although, I remember replacing the files with back ups of the originals afterward.
-
Re: PHP Validator/Error checking
Never dealt with packages that install Apache, MySQL and PHP together. I always just install them separately and have no problems. you might want to give it a try, but I'm not entirely sure it would fix anything.
-
Re: PHP Validator/Error checking
I'm trying to do that on Vista but I'm having a hard time setting it up:
http://www.vbforums.com/showthread.php?t=574444
-
Re: PHP Validator/Error checking
erm. am I missing something? the link you posted shows you asking about IIS but not Apache.
but, that brings up a question -- are you trying to run both Apache and IIS at the same time, on the same port? IIS could be using the port and Apache could be crashing because of it.
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
kows
erm. am I missing something? the link you posted shows you asking about IIS but not Apache.
but, that brings up a question -- are you trying to run both Apache and IIS at the same time, on the same port? IIS could be using the port and Apache could be crashing because of it.
No! I trying to setup IIS in that thread since Vista has a strange way with dealing with phpdev/wamp server. I have tried installing both phpdev and wampserver on Vista but the IIS is needed in order to use it.
At the moment I have Wampserver installed on my laptop (where apache is causing problems) and I am trying to setup IIS on my pc.
-
Re: PHP Validator/Error checking
I highly doubt that IIS is needed to install these MySQL/PHP/Apache packages on Vista. Like I said before, I've never used a packaged installer, but I've converted a Vista machine into a web server before with no problems. And I definitely didn't use IIS. IIS is just another web server (Apache is the web server in the case of both WAMP and PHPdev), it doesn't even need to be installed or turned on. In my case, I used it to create an FTP while Apache ran my web services. If you can't get it to work, install them separately.
And I don't really think I can help you with your Apache crashing problem!
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
kows
I highly doubt that IIS is needed to install these MySQL/PHP/Apache packages on Vista.
I do too! I'm just having difficulty locating the files in the "www" directory because whenever I start wampserver in Vista instead of localhost display the link to the files in the "www" directory it displays the IIS website.
-
Re: PHP Validator/Error checking
I suggested you ditch WAMP and install each component individually. Start with Apache, then PHP theh MySql.
-
Re: PHP Validator/Error checking
-
Re: PHP Validator/Error checking
Well, it looks like only one of my tutorials was causing problems with wampserver (apache). All the other tutorials I tested worked in Wampserver.
Edit:
Quote:
Originally Posted by
visualAd
I suggested you ditch WAMP and install each component individually. Start with Apache, then PHP theh MySql.
What difference would that make?
Quote:
Originally Posted by
visualAd
And disable IIS too ;)
Done!
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
Nightwalker83
What difference would that make?
Do you know how to configure the PHP Apache module in the httpd.conf file? If the answer is no, then that is the difference it will make.
One click installations of a complex envrionment like that will never be flawless and IMO encourage users to blindly install and use the tools without a sound understanding of how they interact and work together and without the experience to fix any configuration issues.
Packages such as WAMP also include their own bespoke configurations which can cause problems with some web applications and system set ups.
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
visualAd
Do you know how to configure the PHP Apache module in the httpd.conf file? If the answer is no, then that is the difference it will make.
Ah ok! However, I been given notes by my lecturer about what lines in that file I must change.
-
Re: PHP Validator/Error checking
The reason I was receiving the error in post #8 was because I was trying to close to database when it was still being accessed via an external script.
To fix the problem I replaced:
in login.php
with
PHP Code:
mysql_close($mysql_link);
-
Re: PHP Validator/Error checking
you never, ever need to call mysql_close(). PHP does this for you. you can just get rid of that call altogether.
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
Nightwalker83
The reason I was receiving the error in post #8 was because I was trying to close to database when it was still being accessed via an external script.
To fix the problem I replaced:
in login.php
with
PHP Code:
mysql_close($mysql_link);
Odd, I never saw those lines in the original code. Then again, it does lack calrity and readability so I may have missed it ;)
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
visualAd
Odd, I never saw those lines in the original code. Then again, it does lack calrity and readability so I may have missed it ;)
The line is in there after:
PHP Code:
$email=mysql_result($result, 0, "email");
I forgot to mention in my other post that the close statement should go just before the final else.
-
Re: PHP Validator/Error checking
My lecturer just informed me that WAMPServer 2.0 which I was using was not compatibile with the code he gave us (i.e the above code). He advised me to use xampplite instead.
-
Re: PHP Validator/Error checking
I reiterate, you should install it yourself. :)
-
Re: PHP Validator/Error checking
Quote:
Originally Posted by
visualAd
I reiterate, you should install it yourself. :)
I'm guessing my lecturer wanted us to use the wamp software because it was/is easy to install. Whereas this is better alternative for school because it means you don't have to stuff around installing multiple programs on a system just to have these programs removed until next time you have that class.
The computers at our school have software on them that refresh the system state after a certain period of time. Therefore any software that was not installed prior will be removed.
-
Re: [RESOLVED] PHP Validator/Error checking
Could some please figure out how to get this code working:
PHP Code:
conn_video.php
<?php
# FileName="conn_video.php"
$hostname= "localhost";
$database = "cartdb";
$user = "root";
$pass = "";
mysql_connect($hostname,$user,$pass) or die( "Unable to
connect to the server");
mysql_select_db($database) or die( "Unable to select the
database");
?>
products.php
PHP Code:
<?php
session_start();
$type = $_POST['type'];
$item_name = $_POST['item_name'];
//connect to server and select database
require_once('conn_video.php');
//Search products
if (($item_name=="") && ($type==""))
$query = "SELECT item_id, item_name FROM
products";
else if (($item_name=="") && ($type !=""))
$query = "SELECT item_id, item_name FROM products
WHERE (type = '$type')";
else if (($item_name!="") && ($type ==""))
$query = "SELECT item_id, item_name FROM products
WHERE (item_name='$item_name')";
else
$query = "SELECT item_id, item_name FROM products
WHERE (type = '$type' AND item_name='$item_name')";
$result = mysql_query ($query) or die ("query 2
failed");
mysql_close();
?>
<html>
<body>
<form method="post" action="products.php">
<table width="699" border="0">
<tr>
<td><strong>Title</strong></td>
<td><strong>Category</strong></td>
<td><strong>Search</strong></td>
<td> ;</td>
</tr>
<tr>
<td width="152"><input name="item_name"
type="text" id="item_name"></td>
<td width="196"><select name="type" size="1"
id="type">
<option selected></option>
<option value="comedy">Comedy</option>
<option value="romance">Romance</option>
<option value="action">Action</option>
<option value="other">Other</option>
</select></td>
<td width="121">
<div align="left">
<input type="submit" name="Submit"
value="Search">
</div></td>
<td width="212"><a
href="view_cart.php"><strong>View your Shopping Cart
</strong></a></td>
</tr>
</table>
</form>
<table width="100%" border="1">
<tr>
<td><b>Select Item</b> </td>
<td><b> Item Name </b></td>
<td><b> Quantity </b></td>
<td><b> Add to Cart </b></td>
</tr>
<?php
//Display products
while ($row = mysql_fetch_row
($result))
{
echo "<tr><form action=add_to_cart.php
method=POST>";
echo "<td> <input name= item_id
type=checkbox id= $row[0] value=$row[0]></td>";
echo "<td> $row[1]</td>";
echo"<td><input name=qty type=text
id=qty value=1 size=4 maxlength=2></td>";
echo"<td><INPUT name=add TYPE=submit
id=add value=Add><td>";
echo "</tr></form>";
}
?>
</form>
</table>
</td>
<td height="244" align="center" valign="middle"><p
align="left"> ;</p>
<p align="left"> ;</p></td>
</tr>
</table>
</body>
</html>
add_to_cart.php
PHP Code:
<?php
session_start();
include 'cart.php';
//Get the item id and the quantity
if(isset($_POST['item_id'], $_POST['qty'])){
$item_id = $POST['item_id'];
$qty = $POST['qty'];
}
// Store the number of items in the shopping cart
$counter = $_SESSION ['counter'];
$cart = new Cart ();
// Unserialized the cart if the cart is not empty
if ($counter>0)
$cart = Unserialize ($_SESSION ['cart']);
else
{
session_register ('cart');
session_register ('counter');
}
if (($item_id =="") or ($qty <1))
{
header ("Location: products.php");
exit;
}
else
{
//Connect to server and select database
require_once('conn_video.php');
$query = "SELECT item_name, price FROM products WHERE (item_id = 'item_id')";
$result = mysql_query ($query) or die ("Database Error!");
if (mysql_num_rows ($result)==1){
$item_name = mysql_result($result, 0, "item_name");
$price = mysql_result($result, 0, "price");
//Add to the cart
$new_item = new Item ($item_id, $item_name, $qty, $price);
$cart->additem($new_item);
//Update the counter
$_SESSION ['counter'] = $counter + 1;
$_SESSION ['cart'] = serialize ($cart);
header ("Location: view_cart.php");
mysql_close();
}
else
{
header ("Location: products.php");
exit;
}
}
?>
remove_from_cart.php
PHP Code:
<?php
session_start();
include 'cart.php';
$item_no=$_POST['item_no'];
//remove item from the cart if selected ¬ mark as
deleted
if ($item_no!=""){
$counter = $_SESSION['counter'];
$cart = new Cart();
$cart = unserialize($_SESSION['cart']);
//delete selected item from the cart
$cart¬>delete_item($item_no);
//update the counter
$_SESSION['counter'] = $counter¬1;
$_SESSION['cart'] = serialize($cart);
header("Location: view_cart.php");
}
?>
view_cart.php
PHP Code:
<?php
session_start();
include 'cart.php';
$cart = new Cart();
$counter= $_SESSION['counter'];
?>
<html>
</head>
<body>
<table width="100%" border="0">
<tr>
<td height="244" colspan="4" valign="top">
<?php
//check whether the cart is empty or not
if ($counter==0){
echo"<h1>Shopping Cart</h1>";
echo"<br><br><p><b> Your Shopping Cart is
empty !!! </b></p>";
echo"<p><b> <a href=products.php>Go back to
products </a> </b></p>";
}
else {
$cart = unserialize($_SESSION['cart']);
//$cart = $_SESSION['cart'];
$depth = $cart¬>get_depth();
echo"<h1>Shopping Cart</h1>";
echo "<table border=1>";
echo"<tr><td><b>Item
Name</b></td><td><b>Quantity</b></td><td><b>
Price</b></td></tr>";
for ($i=0; $i < $depth; $i++)
{
$item = $cart¬>get_item($i);
$deleted = $item¬>deleted;
//display if the item is not marked for
deletion
if (!$deleted){
$item_id = $item¬>get_item_id();
$item_name = $item¬
>get_item_name();
$qty = $item¬>get_qty();
$price = $item¬>get_price();
echo "<tr><form
action=remove_from_cart.php method=POST>";
echo"<td>$item_name</td><td>$qty
</td><td>$price</td>";
echo "<td> <input name= item_no
type=checkbox id= item_no value=$i></td>";
echo"<td><INPUT name=remove
TYPE=submit id=remove value=Remove><td>";
echo "</tr></form>";
}
}
echo "</table>";
echo"<p><b> <a href=checkout.php>Checkout
</a> </b></p>";
echo"<p><b> <a href=products.php>Go back to
products </a> </b></p>";
}
?>
</tr>
</table>
</body>
</html>
checkout.php
PHP Code:
<?php
session_start();
include 'cart.php';
$cart = new Cart();
$counter= $_SESSION['counter'];
if ($counter==0)
echo"<br><br><p><b> Your Shopping Cart is
empty !!! </b></p>";
else {
$cart = unserialize($_SESSION['cart']);
$depth = $cart¬>get_depth();
echo"<h1>Shopping Cart</h1>";
echo "<table border=1>";
echo"<tr><td><b>Item
Name</b></td><td><b>Quantity</b></td><td><b>
Price</b></td></tr>";
for ($i=0; $i < $depth; $i++)
{
$item = $cart¬>get_item($i);
$deleted = $item¬>deleted;
if (!$deleted){
$item_id = $item¬>get_item_id();
$item_name = $item¬
>get_item_name();
$qty = $item¬>get_qty();
$price = $item¬>get_price();
$total_price = $total_price +
($price*$qty);
echo"<tr><td>$item_name</td><td>$qty
</td><td>$price</td></tr>";
}
}
echo"<tr><td><b> Total
</b></td><td> ;</td><td><b>$total_price</b></td></tr>";
echo "</table>";
echo"<p><b> <a href=view_cart.php>Remove
Items from the Cart </a> </b></p>";
echo"<p><b> <a href=products.php>Go back to
products </a> </b></p>";
}
?>
The code is for a shopping cart! It was a example my lecturer gave us but I haven't been able to get it to work My lecturer gave me his working example but I have misplaced it.
This code is also required for the above example:
cart.php
PHP Code:
<?php
class Item {
var $item_id;
var $item_name;
var $qty;
var $price;
var $deleted = false;
function get_item_cost() {
return $this->qty * $this->price;
}
function delete_item() {
$this->deleted = true;
}
function Item ($item_id, $item_name, $qty, $price) {
$this->item_id = $item_id;
$this->item_name = $item_name;
$this->qty = $qty;
$this->price = $price;
}
function get_item_id() {
return $this->item_id;
}
function get_item_name() {
return $this->item_name;
}
function get_qty() {
return $this->qty;
}
function get_price() {
return $this->price;
}
}
class Cart {
var $items;
var $depth;
function Cart() {
$this->items = array();
$this->depth = 0;
}
function add_item($item) {
$this->items[$this->depth] = $item;
$this->depth++;
}
function delete_item($item_no) {
$this->items[$item_no]->delete_item();
}
function get_depth() {
return $this->depth;
}
function get_item($item_no) {
return $this->items[$item_no];
}
}
?>
And the sql for the database:
PHP Code:
CREATE TABLE `customers` (
`first_name` varchar(15) NOT NULL,
`last_name` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(8) NOT NULL,
PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `orders` (
`order_id` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`shipping_address` text NOT NULL,
`total_amount` decimal(6,2) NOT NULL,
`status` varchar(15) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `order_line` (
`order_id` varchar(20) NOT NULL,
`item_id` varchar(6) NOT NULL,
`qty` int(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `products` (
`item_id` varchar(6) NOT NULL,
`item_name` varchar(30) NOT NULL,
`price` decimal(6,2) NOT NULL,
`type` varchar(15) NOT NULL,
`item_date` date NOT NULL default '2004-11-11',
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `products` (`item_id`, `item_name`, `price`, `type`, `item_date`) VALUES
('1001', 'Life on Mass', '12.00', 'Action', '2004-11-11'),
('1002', 'Load of the Ring I', '16.00', 'Action', '2004-11-11'),
('1003', 'Heros S1', '36.00', 'Action', '2004-11-11'),
('1004', 'Friends S2', '19.00', 'Comedy', '2004-11-11');
Edit:
Sorry about the mess the above was copied from a pdf through gmail.
-
Re: [RESOLVED] PHP Validator/Error checking
I'm not sure why you insist on bumping old threads to ask completely unrelated questions? in a resolved thread nonetheless? lol :/
if you copied all of this code directly, and I'm assuming the forum hasn't messed anything up, you have an HTML entity instead of a terminator (";") at the end of each full line. if this is actually the code you're running, then do a find on ";" and replace it with ";", and then do a find on "¬" and replace it with "-".
other than that, maybe you should ... tell us what errors you're getting? if you haven't, turn on error reporting and tell us something -- if it isn't obvious what the errors mean.
-
Re: [RESOLVED] PHP Validator/Error checking
Well for products I still need to fix the following:
Quote:
Notice: Undefined index: type in on line 50
Notice: Undefined index: item_name in products.php on line 51
I looked at my code and the fields are named:
HTML Code:
<td><input name="item_name" type="text" id="item_name"/></td>
<td><select name="type" size ="1" id="type">
Just like the php is reffering to.
PHP Code:
$type = $_POST['type'];
$item_name = $_POST['item_name'];
-
Re: [RESOLVED] PHP Validator/Error checking
okay, except you define those variables before you even know whether or not "products.php" has been submitted to -- it obviously has not, which means those variables aren't set.
even still, you didn't answer anything I asked in my post. I don't know how to "fix" your code if it's a A) a complete mess and un-runnable as is (second line in my post), and B) you can't tell me what it's doing wrong.
if the only problem is that you're getting is undefined indexes, then just make sure those variables exist before assigning values.
PHP Code:
if(isset($_POST['var1'], $_POST['var2'])){
$myvar1 = $_POST['var1'];
$myvar2 = $_POST['var2'];
}
-
Re: [RESOLVED] PHP Validator/Error checking
Quote:
Originally Posted by
kows
okay, except you define those variables before you even know whether or not "products.php" has been submitted to -- it obviously has not, which means those variables aren't set.
even still, you didn't answer anything I asked in my post. I don't know how to "fix" your code if it's a A) a complete mess and un-runnable as is (second line in my post), and B) you can't tell me what it's doing wrong.
oops, you are correct! ; is replacing ";" and ¬ replacing "-" although I don't know why that happened. I can't copy directly from the pdf file because it is protected that's why I use the above method to copy the text.
Quote:
if the only problem is that you're getting is undefined indexes, then just make sure those variables exist before assigning values.
PHP Code:
if(isset($_POST['var1'], $_POST['var2'])){
$myvar1 = $_POST['var1'];
$myvar2 = $_POST['var2'];
}
Thanks! If that doesn't work I'll ask my lecturer tomorrow for his working code then compare the above with it and see what I missed out.
Edit:
Well, that solved the problem with the products page! However, Add to cart isn't working and I don't receive any errors. It looks like I have to ask my lecturer.
-
Re: [RESOLVED] PHP Validator/Error checking
Ok! this code works.
add products:
PHP Code:
<?php
session_start();
include 'cart.php';
//get the item_id and the quantity
$item_id=$_POST['item_id'];
$qty=$_POST['qty'];
//store number of items in the shopping cart
$counter = $_SESSION['counter'];
$cart = new Cart();
//unserialize the cart if the cart is not empty
if ($counter>0)
$cart = unserialize($_SESSION['cart']);
else {
session_register('cart');
session_register('counter');
}
if (($item_id == "")or ($qty < 1))
{
header("Location: products.php");
exit;
}
else
{
//connect to server and select database
require_once('conn_video.php');
$query = "SELECT item_name, price from products WHERE (item_id = '$item_id') ";
$result= mysql_query($query) or die( "Database Error");
if (mysql_num_rows($result) == 1) {
$item_name=mysql_result($result,0,"item_name");
$price=mysql_result($result,0,"price");
//add items to the cart
$new_item = new Item($item_id, $item_name, $qty, $price);
$cart->add_item($new_item);
//update the counter
$_SESSION['counter'] = $counter+1;
$_SESSION['cart'] = serialize($cart);
header("Location: view_cart.php");
}
else
{
header("Location: products.php");
exit;
}
}
?>
checkout:
PHP Code:
<?php
session_start();
include 'cart.php';
$cart = new Cart();
$counter= $_SESSION['counter'];
if ($counter==0)
echo"<br><br><p><b> Your Shopping Cart is empty !!! </b></p>";
else {
$cart = unserialize($_SESSION['cart']);
$depth = $cart->get_depth();
echo"<h1>Shopping Cart</h1>";
echo "<table border=1>";
echo"<tr><td><b>Item Name</b></td><td><b>Quantity</b></td><td><b> Price</b></td></tr>";
for ($i=0; $i < $depth; $i++)
{
$item = $cart->get_item($i);
$deleted = $item->deleted;
if (!$deleted){
$item_id = $item->get_item_id();
$item_name = $item->get_item_name();
$qty = $item->get_qty();
$price = $item->get_price();
$total_amount = $total_amount +($price*$qty);
echo"<tr><td>$item_name</td><td>$qty </td><td>$price</td></tr>";
}
}
echo"<tr><td><b> Total </b></td><td> </td><td><b>$total_amount</b></td></tr>";
echo "</table>";
echo"<p><b> <a href=view_cart.php>Remove Items from the Cart </a> </b></p>";
echo"<p><b> <a href=products.php>Go back to products </a> </b></p>";
}
?>
products:
PHP Code:
<?php
session_start();
//connect to server and select database
require_once('conn_video.php');
?>
<html>
<body>
<form method="post" action="products.php">
<table width="699" border="0">
<tr>
<td><strong>Title</strong></td>
<td><strong>Category</strong></td>
<td><strong>Search</strong></td>
<td> </td>
</tr>
<tr>
<td width="152"><input name="item_name" type="text" id="item_name"></td>
<td width="196"><select name="type" size="1" id="type">
<option selected></option>
<option value="comedy">Comedy</option>
<option value="romance">Romance</option>
<option value="action">Action</option>
<option value="other">Other</option>
</select></td>
<td width="121">
<div align="left">
<input type="submit" name="Submit" value="Search">
</div></td>
<td width="212"><a href="view_cart.php"><strong>View your Shopping Cart </strong></a></td>
</tr>
</table>
</form>
<table width="100%" border="1">
<tr>
<td><b>Select Item</b> </td>
<td><b> Item Name </b></td>
<td><b> Quantity </b></td>
<td><b> Add to Cart </b></td>
</tr>
<?php
//Search products
if(isset($_POST['type'], $_POST['item_name'])){
$type = $_POST['type'];
$item_name = $_POST['item_name'];
}
if (($item_name =="") && ($type ==""))
$query = "SELECT * FROM products";
else
$query = "SELECT item_id, item_name FROM products WHERE (type = '$type' AND item_name='$item_name')";
$result = mysql_query ($query) or die ("query 2 failed");
//Display products
while ($row = mysql_fetch_row ($result))
{
echo "<tr><form action=add_to_cart.php method=POST>";
echo "<td> <input name= item_id type=checkbox id= $row[0] value=$row[0]></td>";
echo "<td> $row[1]</td>";
echo"<td><input name=qty type=text id=qty value=1 size=4 maxlength=2></td>";
echo"<td><INPUT name=add TYPE=submit id=add value=Add><td>";
echo "</form></tr>";
}
?>
</table>
</body>
</html>
remove from cart:
PHP Code:
<?php
session_start();
include 'cart.php';
$item_no=$_POST['item_no'];
//remove item from the cart if selected - mark as deleted
if ($item_no!=""){
$counter = $_SESSION['counter'];
$cart = new Cart();
$cart = unserialize($_SESSION['cart']);
//delete selected item from the cart
$cart->delete_item($item_no);
//update the counter
$_SESSION['counter'] = $counter-1;
$_SESSION['cart'] = serialize($cart);
header("Location: view_cart.php");
}
?>
view cart:
PHP Code:
<?php
session_start();
include 'cart.php';
$cart = new Cart();
$counter= $_SESSION['counter'];
?>
<html>
</head>
<body>
<table width="100%" border="0">
<tr>
<td height="244" colspan="4" valign="top">
<?php
//check whether the cart is empty or not
if ($counter==0){
echo"<h1>Shopping Cart</h1>";
echo"<br><br><p><b> Your Shopping Cart is empty !!! </b></p>";
echo"<p><b> <a href=products.php>Go back to products </a> </b></p>";
}
else {
$cart = unserialize($_SESSION['cart']);
//$cart = $_SESSION['cart'];
$depth = $cart->get_depth();
echo"<h1>Shopping Cart</h1>";
echo "<table border=1>";
echo"<tr><td><b>Item Name</b></td><td><b>Quantity</b></td><td><b> Price</b></td></tr>";
for ($i=0; $i < $depth; $i++)
{
$item = $cart->get_item($i);
$deleted = $item->deleted;
//display if the item is not marked for deletion
if (!$deleted){
$item_id = $item->get_item_id();
$item_name = $item->get_item_name();
$qty = $item->get_qty();
$price = $item->get_price();
echo "<tr><form action=remove_from_cart.php method=POST>";
echo"<td>$item_name</td><td>$qty </td><td>$price</td>";
echo "<td> <input name= item_no type=checkbox id= item_no value=$i></td>";
echo"<td><INPUT name=remove TYPE=submit id=remove value=Remove><td>";
echo "</tr></form>";
}
}
echo "</table>";
echo"<p><b> <a href=checkout.php>Checkout </a> </b></p>";
echo"<p><b> <a href=products.php>Go back to products </a> </b></p>";
}
?>
</tr>
</table>
</body>
</html>
However, I keep receiving "Undefined variable" errors in the products code on the $type and $item_name although, this time it appears below the variable talked about above.
Also, in the checkout code I keep getting:
PHP Code:
Undefined variable: total_amount
on the line that calculates the total amount of the purchase.
-
Re: [RESOLVED] PHP Validator/Error checking
uhhh. and? do you not know what that warning means?
it's saying that you're referencing a variable that has yet to be initialised in your program. the line in question does: "$total_amount = $total_amount + something" -- this line is initialising the variable $total_amount with a value of $total_amount + something. if $total_amount doesn't exist before this point, then PHP will look at the $total_amount variable in the assignment and not be able to find it anywhere. hence the warning.
you could avoid this by simply declaring/initialising your variables at the beginning of your program's scope, or whenever you're about to use them (generally before loops). set it to a value of 0.
-
Re: [RESOLVED] PHP Validator/Error checking
Quote:
Originally Posted by
kows
uhhh. and? do you not know what that warning means?
it's saying that you're referencing a variable that has yet to be initialised in your program. the line in question does: "$total_amount = $total_amount + something" -- this line is initialising the variable $total_amount with a value of $total_amount + something. if $total_amount doesn't exist before this point, then PHP will look at the $total_amount variable in the assignment and not be able to find it anywhere. hence the warning.
you could avoid this by simply declaring/initialising your variables at the beginning of your program's scope, or whenever you're about to use them (generally before loops). set it to a value of 0.
Yes, I realize that but if I put something like:
PHP Code:
$total_amount = ($price*$qty);
The calculation does add up correctly.
or
put:
PHP Code:
var $total_amount;
at the top I get:
Quote:
Parse error: parse error in checkout.php on line 2
Edit:
Apart from the errors appearing on screen the whole thing is working as it is suppose to.
-
Re: [RESOLVED] PHP Validator/Error checking
All variables are declared implicitly. So simply write a value to the variable to initialize it:
PHP Code:
$totalAmount = 0;
-
Re: [RESOLVED] PHP Validator/Error checking
Quote:
Originally Posted by
visualAd
All variables are declared implicitly. So simply write a value to the variable to initialize it:
PHP Code:
$totalAmount = 0;
I have set it like so:
PHP Code:
$totalAmount = 0;
$total_amount = $total_amount +($price*$qty);
Although, I still get the above message on the second line, I think it has to do with the second "$total_amount" rather than the first.
Edit:
If I put:
PHP Code:
$total_amount = ($price*$qty);
instead of
PHP Code:
$total_amount = $total_amount +($price*$qty);
The message no longer appears. However the calculation doesn't calculation the values correctly if I use the first line of code.