Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

problems with updating database

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

problems with updating database

Postby beginner123 » Sat Nov 03, 2012 10:19 am

So I'm creating a website with a load of products and I need to make a page to allow you to update the information in the database. So i have a list a products and next to each product is a link that brings to a new page with a form with text boxes populated with the info from the database. The user can then edit any text box and click the button and the database should be updated.
But i keep getting the same error: Notice: Undefined index: id in C:\wamp\www\musicwebsite\update.php on line 63

Here is my code for the form which displays all the product information:
Code: Select all
 <?php

$id = $_GET['propID'];
@$db = new mysqli( 'localhost', 'root', "", 'k00127082');
if (mysqli_connect_errno())
{
   echo 'error connecting to db';
   exit;
}

$query = "SELECT * from products where id = '" . $id . "'" ;

$result = $db->query($query);

$row = $result->fetch_object();

//get details from products table
$product_name= $row->product_name;
$product_description = $row->product_description;
$quantity_on_hand = $row->quantity_on_hand;
$price = $row->price;
$image = $row->image;
$image_big = $row->image_big;
$formattedPrice = number_format($price, 2, '.', ',');

$result->free();
$db->close();

?>
</p>
<p>&nbsp;
</p>
<form id ="form1" method="post" name="form1" action="update.php" >

   <table width="650" border="1">

         <tr>
         <td width="150"><label class="contact_form">Product Name </label></td>
         <td colspan="2"><input type="text" name="product_name" id="product_name" class="contact_input" value="<?php echo $product_name ?>" /></td>
      </tr>

      <tr>
         <td width="150"><label class="contact_form">Product Description </label></td>
         <td colspan="2"><input type="text" name="product_description" id="product_description" class="contact_input" value="<?php echo $product_description ?>" /></td>
      </tr>
                       
        <tr>
         <td width="150"><label class="contact_form">Quantity on Hand </label></td>
         <td colspan="2"><input type="text" name="quantity_on_hand" id="quantity_on_hand" class="contact_input" value="<?php echo $quantity_on_hand ?>" /> </td>
      </tr>
       
        <tr>
         <td width="150"><label class="contact_form">Price</label></td>
         <td colspan="2"><input type="text" name="price" id="price" class="contact_input" value="<?php echo $price ?>" /></td>
      </tr>
       
        <tr>
         <td width="150"><label class="contact_form">Image</label></td>
         <td colspan="2"><input type="text" name="image" id="image" class="contact_input" value="<?php echo $image ?>" /></td>
      </tr>
       
         <tr>
         <td width="150"><label class="contact_form">Big Image</label></td>
         <td colspan="2"><input type="text" name="image_big" id="image_big" class="contact_input" value="<?php echo $image_big ?>" /></td>
      </tr>
       
   
</table>
</br>

<input type="submit" name="submit" id="submit" value="Update Product" />

<INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)">
</form>


and heres the code to update the database:
Code: Select all
 <?php
            
               @$db = new mysqli( 'localhost', 'root', "", 'k00127082');
               
               if (mysqli_connect_errno()) {
               echo 'error connecting to db';
               exit;
               }
               $product_name=$_POST["product_name"];
               $id = $_POST['id']; //this is line that gets the error
               $product_description=$_POST["product_description"];
               $quantity_on_hand=$_POST["quantity_on_hand"];
               $price=$_POST["price"];
               $image=$_POST["image"];
               $image_big=$_POST["image_big"];
               
               $query = "UPDATE products SET product_name='$product_name', product_description='$product_description', quantity_on_hand=$quantity_on_hand',                                     price='$price', image='$image', image_big='$image_big' where id = '" . $id . "'"  or die(mysql_error());
               
   
               
               $result = $db->query($query);
               
               if($result)
               echo $db->affected_rows . ' products updated in the database';
               else
               echo 'There was a problem updating the information in the database';
               
               $db->close();
               
            ?>

so the error i'm getting has something to with id but i'm just not sure how to fix it
beginner123
New php-forum User
New php-forum User
 
Posts: 33
Joined: Fri Jan 20, 2012 10:39 am

Re: problems with updating database

Postby simplypixie » Sun Nov 04, 2012 12:31 am

You have nothing in your form that posts through the product id. Just add a hidden field with the value of the products id and you should be sorted.

Code: Select all
<input type="hidden" name="id" value="<?php echo $id; ?>" />
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: problems with updating database

Postby beginner123 » Sun Nov 04, 2012 9:03 am

that got rid of the error but now it just displays my error message - There was a problem updating the information in the database.

So there is still a problem somewhere
beginner123
New php-forum User
New php-forum User
 
Posts: 33
Joined: Fri Jan 20, 2012 10:39 am

Re: problems with updating database

Postby simplypixie » Sun Nov 04, 2012 9:13 am

Why not echo out $query and then check all the data is there and formatted as it should be. If it is then try running it directly in phpmyadmin and see what happens.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: problems with updating database

Postby beginner123 » Sun Nov 04, 2012 10:31 am

ok so i echo the query and put it into phpmyadmin and heres the error i get:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', price='100' where id ='1'' at line 1
beginner123
New php-forum User
New php-forum User
 
Posts: 33
Joined: Fri Jan 20, 2012 10:39 am

Re: problems with updating database

Postby MeroD » Sun Nov 04, 2012 12:17 pm

quantity_on_hand=$quantity_on_hand',

Missing '

quantity_on_hand='$quantity_on_hand',
MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: problems with updating database

Postby beginner123 » Sun Nov 04, 2012 12:47 pm

it works thanks :)
beginner123
New php-forum User
New php-forum User
 
Posts: 33
Joined: Fri Jan 20, 2012 10:39 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron