/    Sign up×
Community /Pin to ProfileBookmark

$_GET and Undefined Index

Greetings;

As the title suggests I’m having a problem with my PHP code. I have two tables: seller and category.
Seller has: id, category_id, name and description.
Category has: id,name and description.

I wrote a code that would send the seller’s id to the seller’s page so I can write the seller’s information:

[code=php]<?php
$qry = ‘SELECT * FROM seller WHERE category_id=’.$_GET[‘categoryId’].’;’;
$result = mysql_query($qry);
while ($row = mysql_fetch_array($result)){
Print ‘<tr>’;

Print ‘<td style=”width:15%”><a href = “Seller_page.php?sellerId =’.$row[‘id’].'”>’;
Print ‘<img src = “Seller_Image_table.png” height = “50” width = “50”></a></td>’;
Print ‘<td style=”width:50%”><span class=”tableText”>’ .$row[‘name’].'</span><br/> ‘;
Print ‘<span class = “cellText”>’.$row[‘description’].'</span></td>’;
Print ‘</tr>’;
}
?>[/code]

In the seller’s page I wrote this code:

[code=php]<?php
$val = $_GET[‘sellerId’];
$qry1 = ‘SELECT * FROM seller WHERE id =”$val”‘;
$result1 = mysql_query ($qry1);
while ($row1 = mysql_fetch_array($result1)){
Print ‘<span style = “font-family:tahoma; font-size:22; font-weight:bold”>’.$row1[‘name’].'</div><br/>’;
}
?>[/code]

But I get this error:

Notice: Undefined index: sellerId in C:xampphtdocsSeller_page.php on line 39.

I’m not sure what the problem is.
Thanks in advance.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmDec 05.2015 — It would be nice if you pointed out where line 39 is. Obviously it means that the index you are using to reference some array element is incorrect, so seeing the actual usage would help us to help you.
Copy linkTweet thisAlerts:
@NogDogDec 06.2015 — Is this line 39?
[code=php]$val = $_GET['sellerId']; [/code]
If so, then what does the URL in the browser look like when you get there? (I.e., is there a "sellerId" key with a value in the query string?)

In any case, once you resolve that, you'll need to address the SQL that follows, as you'll want to invert your usage of double and single quotes (double quotes around the entire SQL string, single quotes around string literals within the SQL). Oh, and you should not be using the long-deprecated mysql_*() functions, instead using either the mysql[b]i[/b]_*() functions or the PDO extension (my preference).
Copy linkTweet thisAlerts:
@NogDogDec 06.2015 — PS: [i]And[/i] you should be escaping the value being used in the query so that you are not vulnerable to SQL injection.
Copy linkTweet thisAlerts:
@BoshokaiauthorDec 06.2015 — Is this line 39?
[code=php]$val = $_GET['sellerId']; [/code]
If so, then what does the URL in the browser look like when you get there? (I.e., is there a "sellerId" key with a value in the query string?)

In any case, once you resolve that, you'll need to address the SQL that follows, as you'll want to invert your usage of double and single quotes (double quotes around the entire SQL string, single quotes around string literals within the SQL). Oh, and you should not be using the long-deprecated mysql_*() functions, instead using either the mysql[b]i[/b]_*() functions or the PDO extension (my preference).[/QUOTE]


Actually, my browser does show that the value is sent which is the reason I'm confused.

I added this code to see if the variable actually receives the id, but apparently it's not.
[code=php]$id = isset($_GET['sellerId']) ? $_GET['sellerId'] : ''; [/code]

This is pretty much the source of my problem, $id is not receiving anything and so the other function are not working because of it.
Copy linkTweet thisAlerts:
@BoshokaiauthorDec 06.2015 — Yes thank you, I understood what the error meant, I just don't understand why the variable is not receiving the value I'm sending.
Copy linkTweet thisAlerts:
@BoshokaiauthorDec 06.2015 — Is this line 39?
[code=php]$val = $_GET['sellerId']; [/code]
If so, then what does the URL in the browser look like when you get there? (I.e., is there a "sellerId" key with a value in the query string?)

In any case, once you resolve that, you'll need to address the SQL that follows, as you'll want to invert your usage of double and single quotes (double quotes around the entire SQL string, single quotes around string literals within the SQL). Oh, and you should not be using the long-deprecated mysql_*() functions, instead using either the mysql[b]i[/b]_*() functions or the PDO extension (my preference).[/QUOTE]


Actually when I look at the URL the value is clearly written, and this is what is confusing me the most. I added a code to see if $id is receiving the value I'm sending but apparently its not.
Copy linkTweet thisAlerts:
@NogDogDec 06.2015 — You're going to kick yourself. You've got a space in your query string that you don't want.
[code=php]
Print '<td style="width:15%"><a href = "Seller_page.php?sellerId ='.$row['id'].'">';
// should be...
Print '<td style="width:15%"><a href = "Seller_page.php?sellerId='.$row['id'].'">';
[/code]

While I don't think it matters, I'd also get rid of the spaces around the equals sign for the href property:
[code=php]
Print '<td style="width:15%"><a href="Seller_page.php?sellerId='.$row['id'].'">';
[/code]
Copy linkTweet thisAlerts:
@BoshokaiauthorDec 06.2015 — You're going to kick yourself. You've got a space in your query string that you don't want.
[code=php]
Print '<td style="width:15%"><a href = "Seller_page.php?sellerId ='.$row['id'].'">';
// should be...
Print '<td style="width:15%"><a href = "Seller_page.php?sellerId='.$row['id'].'">';
[/code]

While I don't think it matters, I'd also get rid of the spaces around the equals sign for the href property:
[code=php]
Print '<td style="width:15%"><a href="Seller_page.php?sellerId='.$row['id'].'">';
[/code]
[/QUOTE]


Oh my god, it worked! Thank you so much I really appreciate it. Spaces are a bad habit of mine, better get ride of it.
×

Success!

Help @Boshokai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...