0

PHP noob here. I've written out the 8 step to try to get the data from the database but my first line just return me with

"Notice: Undefined index: itemid in C:\xampp\htdocs\1605267B\KUSO\cart.php on line 130"

I can't find anything wrong with using the GET function on the first line. Please do help me.

<?php

$itemid = $_GET['itemid'];

//TODO 1: Connect to forumdb database
$mysqli = new mysqli("localhost","root",null,"kuso");

//TODO 2: Prepare the statement to select message id and subject info belonging to $userid from forummessage table 
$stmt = $mysqli->prepare("select itemid, itemname, itempicture, description, price from items where itemid=?");

//TODO 3: Bind the values   
$stmt->bind_param("i",$itemid);

//TODO 4: Execute the statement
$stmt->execute();

//TODO 5: bind results into $messageid, $subject
$stmt->bind_result($itemid, $itemname, $itempicture, $description, $price);

echo "<table border='1'>";
echo "<tr><td><b>Subject</b></td><td><b>Actions</b></td></tr>";

// Use while loop to fetch messages and put in a <table>
while ($stmt->fetch()) {
    echo "<tr>";
    //TODO 6: In 1st <td>, display subject
    echo "Sliim-fit.jpg";
    echo "<a href='menschoice.php?productid==$itemid'>";

    //TODO 7: In 2nd <td>, display View hyperlink with messageid in query string.
    //The View hyperlink links to messagedetails.php
    echo "<td> <a href='messagedetails.php?messageid=$messageid'> View </a></td>";

    echo "</tr>";
}

echo "</table>";


//TODO 8: close the statement
$stmt->close();

//TODO 9: close $mysqli
$mysqli->close();

?>

I don't know why it keeps throwing me back the undefined index. Help me out senpais thanks.

Martin Ille
  • 6,747
  • 9
  • 44
  • 63
Max Yeo
  • 1
  • 1
  • So are you sending itemid using query string? ex: example.com?itemid=1; – B. Desai Jan 29 '17 at 04:41
  • @GurV its still not working properly, still returns back the same thing. :( – Max Yeo Jan 29 '17 at 04:50
  • here is nothing wrong. so you confirm that your query like this: www.example.com?itemid=12 – Omar Jan 29 '17 at 04:50
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Sean Jan 29 '17 at 05:39
  • (http://localhost/1605267B/KUSO/cart.php) this is it. im trying to link to the phpMyAdmin server to retrive the data i put in there – Max Yeo Jan 29 '17 at 06:12
  • Just to let you know, phpmyadmin is not a server, it is just a database GUI manager. You are running a Mysql service. – Leandro Papasidero Jan 29 '17 at 06:47
  • 1
    Try http://localhost/1605267B/KUSO/cart.php?itemid=1 like @OmarFaruque said. Change id 1 to any other id that you wnat – Leandro Papasidero Jan 29 '17 at 06:49

1 Answers1

0

Change the following line

$itemid = $_GET['itemid'];

With

$itemid = !empty($_GET['itemid'])  ? $_GET['itemid'] : 0;

Zero should be any default id or 0 if you dont want to display any match.

Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33