Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Undefined index: prod_id...

    hello guys, i have 2 tables - tblproduct and tblretprod. prod_id is common in both. i am retrieving records from the tables to display with edit and delete options beside each record displayed.
    i created an update.php and updprod.php (for edit).

    update.php

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Update/Delete Product</title>
     
    <?php
     
    include_once("db_connect.php");
     
    $result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id");
     
    ?>
     
    </head>
     
    <body>
     
    <?php
     
    echo "<table width='80%' border=0>";
     
    echo 
    "<tr bgcolor='#CCCCCC'>";
    echo 
    "<td>Product</td>";
    echo 
    "<td>Brand</td>";
    echo 
    "<td>Price</td>";
    echo 
    "<td>Action</td>";
    echo 
    "</tr>";
     
    while(
    $res=mysql_fetch_array($result)){
     
    echo 
    "<tr>";
    echo 
    "<td>".$res['prod_name']."</td>";
    echo 
    "<td>".$res['prod_brand']."</td>";
    echo 
    "<td>".$res['prod_price']."</td>"

    echo 
    "<td><a href=\"edit.php?prod_id=$res[prod_id]\">Edit</a> | <a href=\"delete.php?prod_id=$res[prod_id]\">Delete</a></td>";
    }
     
    echo 
    "</table>";
    ?>
     
    </body>
    </html>
     
     
    </body>
    </html>
    error message being displayed for code above is - notice: Notice: Undefined index: prod_id in C:\wamp\www\buysmart_site\update.php on line 36

    upd_prod.php

    PHP Code:
    include_once("db_connect.php");
     
    if(isset($_POST['update']))
    {
    $prod_id = $_POST['prod_id'];
     
    $prod_name=$_POST['prod_name'];
    $prod_brand=$_POST['prod_brand'];
    $prod_price=$_POST['prod_price']; 

     
    // checking empty field
    if(empty($prod_price))
    {
    //if name field is empty
    if(empty($prod_price))
    {
    echo "<font color='red'>Price field is empty.</font><br/>";
    }
     


    else


    //updating the table
    $result=mysql_query("UPDATE tblretprod SET proc_price='$prod_price' WHERE prod_id=$prod_id");
     
    //redirectig to the display page. In our case, it is index.php
    //header("Location: index.php");
    }
    }
    ?>
    <?php
    //for displaying data of this particular data
     
    //getting id from url
    $prod_id $_GET['prod_id'];
     
    //selecting data associated with this particular id
    $result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id");
     
    while(
    $res=mysql_fetch_array($result))
    {
    $prod_name $res['prod_name'];
    $prod_brand $res['prod_brand'];
    $prod_price $res['prod_price'];
    }
    ?>
    <html>
    <title>Edit Product</title>
    <body>
    <a href="#">Home</a>
    <br/><br/>
    <form name="edit" method="post" action="edit.php">
    <table border="0">
       <tr> 
        <td>Product Name</td>
        <td>
            <input disabled="disabled" type="text" name="prod_name" value=<?php echo $prod_name;?>>     </td>
      </tr>
      <tr> 
        <td>Brand</td>
        <td>
            <input disabled="disabled" type="text" name="prod_brand" value=<?php echo $prod_brand;?>>     </td>
      </tr>
    <tr> 
        <td>Product Price</td>
        <td>
            <input type="text" name="prod_price" value=<?php echo $prod_price;?>>     </td>
      </tr>
    <tr>
        <td><input type="hidden" name="prod_id" value=<?php echo $_GET['prod_id'];?>>    </td>
        <td><input type="submit" name="update" value="Update"></td>
      </tr>
    </table>
    </form>
     
    </body>
    </html>
    same error message as above

    can anyone please help me in defining the prod_id?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    That column is not returned in your mysql result set. Why?

    Because you didn't select it:
    SELECT a.prod_name, a.prod_brand, b.prod_price FROM

    Put that column in there and your problem will be solved.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,038
    Thanks
    15
    Thanked 240 Times in 240 Posts
    You seem to be sending the prod_id by $_GET, but trying to receive it by $_POST.
    Surely that won't work.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #4
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    thnx..that works fine when i add b.prod_id to the select statement.
    but now on the second part of the code, the product name, brand and price is not being displayed in the text boxes for editing. the edit button is beside each product being displayed. help in solving that issue is needed please

  • #5
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Try this line to print out an error, if there is one, on the query.

    $result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id") or die(mysql_error());

    You are loading this page with a product ID in the URL, correct?

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #6
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    the prod_id is the primary key in tblproduct and a foreign key in tblretprod.
    am not getting it from url, am getting it from table tblretprod. when i change the price and click on update, the tblretprod is updated with the new price

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    upd_prod.php
    $prod_id = $_GET['prod_id'];

    Looks to me like it's trying to get it from the URL.. anyway if you put the earlier code I gave you in, you will possibly get an error message which will help us troubleshoot.

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    i have no error message. in fact what is happening is that the name of the last product in the list along with the brand and price is being displayed. the update is working. i just tested it. i dnt know why the last product's name, brand and price are being displayed.

  • #9
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    i managed to solve the issue but now all products are being displayed instead of the one beside which i clicked the edit link. find below the new codes

    PHP Code:
    include_once("db_connect.php");

    if(isset($_POST['update']))
    {
    $prod_id = $_POST['prod_id'];

    $prod_name=$_POST['prod_name'];
    $prod_brand=$_POST['prod_brand'];
    $prod_price=$_POST['prod_price']; 

    // checking empty field
    if(empty($prod_price))
    {
    //if name field is empty
    if(empty($prod_price))
    {
    echo "<font color='red'>Price field is empty.</font><br/>";
    }


    else

    //updating the table
    //$result=mysql_query("UPDATE tblretprod SET prod_price='$prod_price' WHERE prod_id=$prod_id");
    $result=mysql_query("UPDATE tblretprod SET prod_price='".$prod_price."' WHERE prod_id='".$prod_id."';");

    //redirectig to the display page. In our case, it is index.php
    header("Location: update.php");
    }
    }
    ?>
    <?php

    $prod_id 
    $_GET['prod_id'];

    $result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id") or die(mysql_error());

    ?>
    <html>
    <title>Edit Product</title>
    <body>
    <a href="#">Home</a>
    <br/><br/>
    <form name="edit" method="post" action="updprod.php">
    <table border="0">
    <?php
    while($res=mysql_fetch_array($result))
    {
    $prod_name $res['prod_name'];
    $prod_brand $res['prod_brand'];
    $prod_price $res['prod_price'];

    ?>
       <tr> 
        <td>Product Name</td>
        <td>
            <input type="text" disabled="disabled" name="prod_name[]" value="<?php echo $prod_name;?>"> </td>
      </tr>
      <tr> 
        <td>Brand</td>
        <td>
            <input type="text" disabled="disabled" name="prod_brand[]" value="<?php echo $prod_brand;?>">     </td>
      </tr>
    <tr> 
        <td>Product Price</td>
        <td>
            <input type="text" name="prod_price[]" value="<?php echo $prod_price;?>">
            <input type="hidden" name="prod_id[]" value="<?php echo $_GET['prod_id'];?>">
                 </td>
      </tr>
       <?php ?> 
       <tr>
        <td><input type="submit" name="update" value="Update"></td>
      </tr>
    </table>
    </form>

    </body>
    </html>

  • #10
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    i managed to sort it out

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •