Jump to content


Photo

PHP


  • Please log in to reply
24 replies to this topic

#1 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 04 August 2003 - 06:48 AM

I am making my dads website and one of the pages, the user selects which category they want to go to. I have a page that generates a list of all the products in that category. How do I get it so when you click on a normal <a href></a> link to tell the products page generator to generate a page based on that category, if you see what I mean. At the moment all I can think of is using a submit button that carries a variable that stores which category they selected to the next page and then the products page generator gets the information from that variable and generates a page based on that.

#2 Richard H

Richard H

    Pyro Forum Veteran

  • Admin
  • 2,706 posts

Posted 04 August 2003 - 12:09 PM

Thats easy. You simply pass the record reference along with the URL as a get string.

e.g. http://www.domain.co...ecord.php?ref=1

when you loop through the database to pull out all the records for each one you echo the link and append the variable to the get string.

Then on the show record page you extract the value of ref e.g. $_GET['ref'] and then use it in your SQL query.

#3 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 04 August 2003 - 05:45 PM

Thanks G'vnur

#4 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 04 August 2003 - 10:12 PM

When you say ref do you mean the variable name? And I am having trouble with moving variable values from forms to the next part of the progie. Not sure if it would work but could you say if the source here would give the desired effect. The first bit checks if the variable edited has the value yes in it, which was added in the form bit. If it was, then it goes to the function 'add' which adds the information to the database. Notice how it restarts the program when the submit button is pressed. I am learning PHP at the moment but know the principles of programing languages e.g. arrays, function, loops etc.
////////////////////////////////////////////////////
<html>
<head>
<title>Add a Product</title>
</head>
<body>
<?php
if ($edited == "yes")
{
add();
}
?>
<form method="post" action="addcat.php">
<p>Enter Product Code - <input type="text" name="code">
<p>Enter Product Name - <input type="text" name="name">
<p>Enter Product Description - <input type="textarea" name="description">
<p>Enter Product Sizes - <input type="text" name="sizes">
<p>Enter Product Colours - <input type="text" name="colours">
<p>Enter Product Category - <input type="text" name="category">
<p>Enter Product Price - <input type="text" name="price">
<p>Enter Product Thumb Nail Picture Name - <input type="text" name="thumb">
<p>Enter Product Picture Name - <input type="text" name="picture">
/*IMPORTANT BIT*/
<p><input type="hidden" name="edited" value="yes">
<p><input type="submit">
</form>
<?php
function add($code,$name,$description,$sizes,$colours,$category,$price,$thumb,$picture)
{
$host="localhost";
$user="root";
$password="";
$database="brenigcat";
$connection=mysql_connect($host,$user,$password)
or die ("Could Not Connect To MySQL Server");
$db=mysql_select_db($database,$connection)
or die ("Could Not Connect To Database");

$query="INSERT INTO products (code,name,description,category,colours,sizes,price,thumb,picture)
values
('$code','$name','$description','$category','$colours','$sizes','$price','$thumb','$picture')";
$result=mysql_query($query)
or die ("Could Not Add To Database");
}
$edited="no";
?>
<p><a href="addcat.php">Add Another Product</a>
</body>
</html>
////////////////////////////////////////////////////

[Edited on 4-8-2003 by Stuart]

#5 Richard H

Richard H

    Pyro Forum Veteran

  • Admin
  • 2,706 posts

Posted 04 August 2003 - 11:08 PM

If there is a problem I would suspect the calling of the function. When you call it you need to specify the parameters from your form in the brackets, otherwise your data from the form can't get into the function.

so where you call it with:
add();

replace it with

add($code,$name,$description,$sizes,$colours,$category,$price,$thumb,$picture);

Also, I have noticed potential security holes and possible probs when inserting a record.

Try and get the script to add a record by following my advice, and then I will help you to make the script more secure

:)

#6 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 05 August 2003 - 06:10 AM

Thanks. I have done what you said but when you click the submit button it appears to add the values because there is no error but when I go to the catalogue update program, it doesnt appear to be in the database. What I am going to do is try some of the example scripts that came with the book I have and then see if they work when I want to add something to the database.

Thanks


Stuart

#7 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 12 August 2003 - 12:19 PM

Have got the add to catalogue part working but am having trounble witht he delete from catalogue bit. It looks like it should work because it doesnt come up with errors but it doesnt work ie it doesnt delete it from the database. If I leave out the WHERE clause then it deletes all information in the table so that works OK. Could you have a look please :unsure: BTW I have register_globals turned on.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<html>
<head>
<title>Delete A Product</title>
</head>
<body>
<?php
/*Connect to Database*/
include("misc.inc");
/*Get the names of all of the products from the database*/
$query="SELECT name FROM products";
$result=mysql_query($query)
or die("Could Not Select Names");
/*Create form and select tags*/
echo "<form action='deletecatconfirm.php' method='post'>\n";
echo "<select name='product'>\n";
/*Start of While Loop*/
while ($row=mysql_fetch_array($result))
{
/*Extract all of the information from $row*/
extract($row);
/*Display the information in a drop down list*/
echo "<option value='name'>$name\n";
}
?>
</select>
<input type="submit">
</form>
</body>
</html>

And the confirmation page.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<html>
<head>
<title>Deleted Product Confirmation</title>
</head>
<body>
<?php
/*Connect to Database*/
include("misc.inc");
/*Delete the row store in $product*/
$query="DELETE FROM products WHERE $product";
$result=mysql_query($query)
or die("Couldnt Delete Product");
?>
</body>
</html>

Edited by Stuart, 12 August 2003 - 12:40 PM.


#8 Richard H

Richard H

    Pyro Forum Veteran

  • Admin
  • 2,706 posts

Posted 12 August 2003 - 01:56 PM

It's your SQL query

'$query="DELETE FROM products WHERE $product";'

The above is saying delete from the products table where product.

This is wrong. You need to tell it which product to delete. e.g.

$query="DELETE FROM products WHERE ref=$productref";

you need to specify the unique primary key of the record you want to delete.

So after the WHERE command,specify the field name of the primary key e.g. ref and then put equals, and then the variable that contains the value of the key to be deleted.

e.g. DELETE FROM products WHERE ref=1 is what you would see if you echo the query to the browser, and this is what SQL see's.

Hope this helps.

#9 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 12 August 2003 - 03:19 PM

Thanks Rich, I noticed that I needed to put that after I had put the post but it still didnt work. I know what is wrong though. I am not specifing the primary key, instead I am specifing the name of the product and not its code which is the primary key. I will change the primary key to the product name now, thanks


Stuart

#10 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 12 August 2003 - 03:41 PM

Little Update-
I dont know if that was the problem but it still didnt work after I changed the primary key. But then after fiddling, I managed to get it to work :D If you lookat where the drop down list is created in the first program, it sets the value of each item to product. Im not sure how it works, but I got rid of it to clean the code up a little to make it easier to understand and I noticed that it wasnt needed. I got rid and it worked. :D :D :D :D :D. I can now get the updatecat program to work now and then I should have the back bone done and can start fiddling and stop it working again as I try to make it look better and smaller :D .


Thanks for the help, probebly want a bit more again some time :)


Stuart :D :D :D

Edited by Stuart, 12 August 2003 - 03:42 PM.


#11 Richard H

Richard H

    Pyro Forum Veteran

  • Admin
  • 2,706 posts

Posted 12 August 2003 - 03:44 PM

Glad you got it sorted, only too happy to help.

#12 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 13 August 2003 - 05:28 PM

Oh holy one, I kneel before and bring a gift, another PHP and MySQL question.
One of the things the database has to store is a description of the product. Well, it stores the spaces in between words, but how would I get it to store 'enters' so that the text will appear on the next line when the database is query.
Heres an easier way of putting it.
Here is a new line.
And another.

How would I get it to store the text on those new lines instead of like this-
Here is a new line.And another.


Thanks


Stuart

#13 Richard H

Richard H

    Pyro Forum Veteran

  • Admin
  • 2,706 posts

Posted 13 August 2003 - 08:58 PM

You don't need to worry about that when you put the record into the database, but to make text appear correctly (paragraphs etc) you run the output through a function before echoing it to the html page.

You do this by:

$text=nl2br($row['description']);
echo("$text");

The nl2br() function takes new lines in your text (everytime you punch return or start a new line) and converts them to a </br> tag. This preserves the structure of the text.

The result of this is that the text will look the same as when you put it in.

Hope that helps.

#14 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 13 August 2003 - 09:16 PM

OK, I will try tomorow, too tired and late to fiddle with PHP now

Thanks


Stuart

#15 Stuart

Stuart

    BPS Member

  • General Public Members
  • PipPipPip
  • 664 posts

Posted 13 August 2003 - 09:28 PM

Just tried it now actually-worked :D

Thanks

Stuart




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users