0

Good Day, I am tasked to have an original and discounted price in our website, using PHP. Previously the original price is already present, now I am trying to add a second price by adding variables in the customer management system. The problem is that I get

Notice: Undefined index:

even when I add a discounted price value alongside the original price. I added a new column for the new discounted price option in my phpmyadmin database but I still get that index error.

Any help would be appreciated and ill explain the situation with codes and screenshots.

price error phpmyadmin columns

codes with original price and discounted price (the "d_price" is the one with unidentified index)

1.

  <div class="col-md-12"><hr></div>
                    <div class="col-md-3">ORIGINAL PRICE</div>
                    <div class="col-md-9">
                      <input type="number" class="form-control" name="price" placeholder="Original Price"
                        value="<?php
                          if(isset($_POST['price'])) {
                            echo($_POST['price']);
                          } else {
                            echo($rowProduct['price']);
                          } ?>">
                    </div>

                    <div class="col-md-12"><hr></div>
                    <div class="col-md-3">DISCOUNTED PRICE</div>
                    <div class="col-md-9">
                      <input type="number" class="form-control" name="d_price" placeholder="Discounted Price"
                        value="<?php
                          if(isset($_POST['d_price'])) {
                            echo($_POST['d_price']);
                          } else {
                            echo($rowProduct['d_price']);
                          } ?>">
                    </div>

2.

    <div class="col-md-12">
                <div class="col-md-6 text-align-center">
                  <h4>ORIGINAL PRICE</h4>
                    <strike><h4><?php  echo($rowProduct['price']); ?> PHP</h4></strike>
                </div>

                <div class="col-md-6 text-align-center">
                  <h4>DISCOUNTED PRICE</h4>
                    <h4><?php  echo($rowProduct['d_price']); ?> PHP</h4>
                </div>
              </div>

3. (this is the if statement snippet for the save button)

  if (isset($_POST['submit']) && $_POST['submit'] == 'SAVE')
  {
    $price = $_POST['price'];
    $d_price = $_POST['d_price'];

Again 'price' works normally while 'd_price' shows the error. I hope I have provided enough information to not get my question downvoted.

I think this is where $rowProduct originates:

  $producttagid = isset($_GET['t']) ? $_GET['t'] : 1;
  $merchantid = isset($_GET['m']) ? $_GET['m'] : NULL;

  $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
  $lstProductTag = $mcProductTag->SelectLst($db);
  $lstProductTagLink = $mcProductTagLink->SelectLst_ByProductId($db, $productid);
  $lstMerchant = $mcMerchant->SelectLst($db, 1, 10000);



$productid = $mcProduct->InsertObj($db, $merchantid, $title, $subtitle, $body, $recstatus, $price, $d_price, $qty, $createddate, $createdby, $featured, $packageid, $expirationdays);

heres a screenshot of my phpmyadmin database, I already added d_price in the Product row but I still get unidentified index. Im providing as much information as I can.

phpmyadmin

I think these lines of code interact with the SQL database via PDO

database link

$db

EDIT: here are all the locations of SelectObj_ByProductId (searched with notepad++)

 C:\xampp\htdocs\mwc_canuto\-api\ProductsBookmarkToggle.api.php (1 hit)
    Line 65:     $api['productData'] = $api['productModel']->SelectObj_ByProductId(
  C:\xampp\htdocs\mwc_canuto\cards\info-detailed.vc.php (2 hits)
    Line 45:   $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
    Line 54:     $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
  C:\xampp\htdocs\mwc_canuto\cards\info.vc.php (2 hits)
    Line 46:   $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
    Line 65:       $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
  C:\xampp\htdocs\mwc_canuto\clinic\info-detailed.vc.php (2 hits)
    Line 45:   $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
    Line 54:     $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
  C:\xampp\htdocs\mwc_canuto\cms\products_edit.vc.php (1 hit)
    Line 151:   $rowProduct = $mcProduct->SelectObj_ByProductId($db, $productid);
  C:\xampp\htdocs\mwc_canuto\_mc\Product.mc.php (1 hit)
    Line 205:   public function SelectObj_ByProductId($db, $productid) {

EDIT: I added d_price in what seems to be the insert object function, I still get unidentified index

  /********** Insert Object **********/

   public function InsertObj($db, $merchantid, $title, $subtitle, $body, $recstatus, $price, $d_price, $qty, $createddate, $createdby, $featured, $packageid, $expirationdays) {
    $stmt = $db->prepare(
      " INSERT INTO `product`
          (merchantid, title, subtitle, body, recstatus, price, d_price, qty, createddate, createdby, featured, packageid, expirationdays)
        VALUES
          (:merchantid, :title, :subtitle, :body, :recstatus, :price, :d_price, :qty, :createddate, :createdby, :featured, :packageid, :expirationdays) "
    );

    // cast bit type data to int else it wont save properly
    $featured = (int)$featured;

    $stmt->bindValue(':merchantid', $merchantid, PDO::PARAM_INT);
    $stmt->bindValue(':title', $title, PDO::PARAM_STR);
    $stmt->bindValue(':subtitle', $subtitle, PDO::PARAM_STR);
    $stmt->bindValue(':body', $body, PDO::PARAM_STR);
    $stmt->bindValue(':recstatus', $recstatus, PDO::PARAM_INT);
    $stmt->bindValue(':price', $price, PDO::PARAM_STR);
    $stmt->bindValue(':d_price', $d_price, PDO::PARAM_STR);
    $stmt->bindValue(':qty', $qty, PDO::PARAM_INT);
    $stmt->bindValue(':createddate', $createddate, PDO::PARAM_STR);
    $stmt->bindValue(':createdby', $createdby, PDO::PARAM_STR);
    $stmt->bindValue(':featured', $featured, PDO::PARAM_INT);
    $stmt->bindValue(':packageid', $packageid, PDO::PARAM_INT);
    $stmt->bindValue(':expirationdays', $expirationdays, PDO::PARAM_INT);
    $stmt->execute();
    $insertId = $db->lastInsertId();

    return $insertId;
  }

unidentified index

line 139

  • I'll take a guess and say that this `

    PHP

    ` is line 139? If so, use a conditional statement for it also or a ternary operator.
    – Funk Forty Niner Nov 21 '18 at 02:26
  • yes thats line 139 with the unidentified index. also i tried to explain the situation of the codes here in order to avoid that duplicate question mark. –  Nov 21 '18 at 02:28
  • may i see a smple of a conditional statement or a ternary operator using my code? –  Nov 21 '18 at 02:35
  • 1
    You already have one going now `if(isset($_POST['d_price'])) { echo($_POST['d_price']); }` - Base yourself on that. The ternary operator example can be found here http://php.net/manual/en/language.operators.comparison.php – Funk Forty Niner Nov 21 '18 at 02:41
  • i woud just like to admit and be honest after i read the ternary operator example and how my if else statement still outputs unidentified index: I Don't Get It –  Nov 21 '18 at 02:56
  • The manuals shows `$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];`. – Funk Forty Niner Nov 21 '18 at 02:57
  • i tried: "> but i still get unidentified index –  Nov 21 '18 at 03:02
  • Check your query then. If it's not happening in your form then it's in the query. – Funk Forty Niner Nov 21 '18 at 03:05
  • I could not find a query when browsing through the php files, if it is possible i would like an exmaple how PHP fetches data from SQL –  Nov 21 '18 at 03:14
  • I can't do anything else for you here. I reopened the question, so you can see if someone else can spot something. – Funk Forty Niner Nov 21 '18 at 03:15
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Nov 21 '18 at 03:33
  • can you post the code for `SelectObj_ByProductId($db, $productid)` function ? – Mojo Allmighty Nov 21 '18 at 03:35
  • added all locations of SelectObj_ByProductId –  Nov 21 '18 at 04:23
  • i added "d_price" in all lines of code with "price" in them like in the insert object function, i also added the "d_price" column in phpmyadmin, yet i still get unidentified index when trying to display it. –  Nov 21 '18 at 05:57
  • same error, i added d_price in $stmt, and im not sure what you mean by declare a variable, i already added d_price in insert object and have place it in the database –  Nov 21 '18 at 06:28
  • Why don't you print your query result object and see if it have the column? – Ussaid Iqbal Nov 21 '18 at 06:34
  • Use `echo "
    "; print_r($rowProduct); echo "
    ";`
    – Ussaid Iqbal Nov 21 '18 at 06:34
  • how do i print the query object? –  Nov 21 '18 at 06:35
  • I have updated the comment. – Ussaid Iqbal Nov 21 '18 at 06:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184007/discussion-between-ussaid-iqbal-and-hadrian-clayton). – Ussaid Iqbal Nov 21 '18 at 06:37

0 Answers0