PHP Noob Question - Trying to "GET" info from Database

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
sdaztec
Forum Newbie
Posts: 6
Joined: Thu Nov 25, 2010 11:54 am

PHP Noob Question - Trying to "GET" info from Database

Post by sdaztec »

Hi all PHP gods,

I've been messing around with the php on a friends site and on for a while. Things are probably done incorrectly, but it seems to be working. The one problem i'm having is when I pass data through the URL, I'm not able to import all the MYSQL information using one variable. I was able to get it to work using the "GET" function, but I have to pass all the variables through the URL, but that creates a massive url string.

can somebody help me understand how to properly fetch the rest of the row information using just 1 variable?

Thanks so much!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Noob Question - Trying to "GET" info from Database

Post by Celauran »

I'm not quite clear on what it is you're trying to do. Given your references to GET, I'll assume you're submitting a form using the get method and then trying to insert said data into your database. Is that about right? If so, you can specify each element in the $_GET array when you're building your SQL query. For instance, say you have someurl.com/?name=Bob&age=34&somevar=1. You would then go on to build your query like:

Code: Select all

$sql = "INSERT INTO table
        SET name    = '{$_GET['name']}',
            age     = $_GET['age'],
            somevar = '{$_GET['somevar']}'";
Or have I completely misunderstood what you're asking?
sdaztec
Forum Newbie
Posts: 6
Joined: Thu Nov 25, 2010 11:54 am

Re: PHP Noob Question - Trying to "GET" info from Database

Post by sdaztec »

Thank you for the reply Celauran, and I apologize that i am not explaining correctly, and I maybe getting in over my head here...


What I am trying to do is something like this:

I have a page that pulls product information from the database and displays it. From there if someone is interested in more information about the product they click the link. That link contains ALL the product information from the database and creates an extremely long URl. (here is a short example)

mydomain. com/product.php?description=You%20will%20love%20this%20amazing%20product.&price=211.87&id=100066024&size=xl&color=grey

What I am trying to do is just pass the product id and have the php script access the rest of the information from the product id to display on the product page.

mydomain. com/product.php?id=100066024

Does that make more sense?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: PHP Noob Question - Trying to "GET" info from Database

Post by s992 »

Code: Select all

$sql = "SELECT * FROM products WHERE id = " . $_GET['id']
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Noob Question - Trying to "GET" info from Database

Post by Celauran »

s992 wrote:

Code: Select all

$sql = "SELECT * FROM products WHERE id = " . $_GET['id']
Indeed.

Code: Select all

if (empty($_GET['id'])
{
    $sql = "SELECT id FROM table";
}
else
{
    $sql = "SELECT * FROM table WHERE id = $_GET['id']";
}
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Noob Question - Trying to "GET" info from Database

Post by McInfo »

It is wise to filter and sanitize user input.

PHP Manual:
sdaztec
Forum Newbie
Posts: 6
Joined: Thu Nov 25, 2010 11:54 am

Re: PHP Noob Question - Trying to "GET" info from Database

Post by sdaztec »

You are AWESOME

I has previously tried that, but I think I left the "$" out of the $_GET... PHP, so complicated yet so simple! :lol:

Okay last question, i can't seem to get it to import the info still, but I'm not getting any errors. On the previous we had used $style[$j] to call the information from that data base, but it doesn't seem to be working on this product page. Any ideas.

( The database select code has been changed to $sql="SELECT * FROM products where Product Number='$_GET[id]' "; )
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: PHP Noob Question - Trying to "GET" info from Database

Post by s992 »

Can we see your code?
sdaztec
Forum Newbie
Posts: 6
Joined: Thu Nov 25, 2010 11:54 am

Re: PHP Noob Question - Trying to "GET" info from Database

Post by sdaztec »

Code: Select all

$sql="SELECT * FROM product where Product Number='$_GET[id]' ";

$Type=array();
$Price=array();
$Color=array();
$Size=array();
$ID=array();
$PhotoLocation=array();

$result1=mysql_query($sql,$con);
if(!$result1)
{
echo "Bad";
}
else
{
while($row1=mysql_fetch_array($result1))
{
$counter=1;
$images=array();


$Type[]=$row1['type'];

$Price[]=$row1['price'];

$Color[]=$row1['color'];

$Size[]=$row1['size'];

$ID[]=$row1['id'];

$image=$row1['Photo Location'];

$images=explode(",",$image);

$PhotoLocation[]=$row1['Photo Location'];

$counter++;


unset($images);

}
mysql_close($con);
echo "</table>";


} ?>
After that When i want to cal the info into the page I put code like this - <?php echo "Type: $type[$j]"; ?> and i get a blank where the Info should be. I have a Database connect file required in the top of the page, so that isn't the issue...
Last edited by sdaztec on Sat Nov 27, 2010 2:57 pm, edited 2 times in total.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: PHP Noob Question - Trying to "GET" info from Database

Post by s992 »

You're missing an open quote here:

Code: Select all

$Type[]=$row1[type'];
There's nothing happening in this loop:

Code: Select all

for($i=0;$i<sizeof($images);$i++)
{


        }$PhotoLocation[]=$row1['Photo Location'];
And you don't have a variable named $Style.
Post Reply