SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Update row ???
-
May 9, 2003, 11:24 #1
- Join Date
- Apr 2003
- Location
- San Juan, Puerto Rico
- Posts
- 215
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Update row ???
Im using this little code to update a row, but it doesnt seem to work?
PHP Code:<?php
####################################################################
## Updating Item ###################################################
####################################################################
if ($_POST['Submit'] == "Update")
{
$sql = 'UPDATE `Inventory_A` SET `Item_Name` = \'$name\' WHERE `ID` = \'$id\' LIMIT 1 ; ';
echo "Item $name has been updated";
}
?>"I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." - Albert Einstein
-
May 9, 2003, 11:39 #2
- Join Date
- Jul 2002
- Location
- Along the Wasatch Fault line.
- Posts
- 1,771
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this:
PHP Code:<?php
if ( $_POST['Submit'] == "Update" )
{
$name = $_POST['name'];
$id = $_GET['id'];
$sql = "UPDATE Inventory_A
SET
Item_Name = '$name'
WHERE
ID = '$id'
";
if( mysql_query( $sql ) )
echo "Item $name has been updated";
}
?>Last edited by PHP John; May 9, 2003 at 12:02. Reason: edited code
John
-
May 9, 2003, 11:51 #3
- Join Date
- Apr 2003
- Location
- San Juan, Puerto Rico
- Posts
- 215
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
nothing happen, but first let me give you more information about the little scrip ok...
here goes:
Code:The script works this way, first you select an item id from the page fix.php by clicling on a link. Then you are redirect to the page fix2.php, where using the $id you retrieve the information from the database and the info is displayed in each input(text areas,drop down lists, etc...) Now, you can make the changes to the item by re-typing the changes in the inputs... at the bottom of the page, there is a Update button <input name="Update" type="Submit" id="Submit10" value="Update"> The for is set as follow: <form action="fixed.php" method="post" name="fix_item" id="fix_item"> So now when you press update button you go to fixed.php, where the little scrip i show before is set. but notting happens :( Please have a look http://www.bsinternationalstock.com/fix.php
if need to see some code let me know ok"I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." - Albert Einstein
-
May 9, 2003, 12:03 #4
- Join Date
- Jul 2002
- Location
- Along the Wasatch Fault line.
- Posts
- 1,771
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You must get the $name and $id values from the posted and getted variables you are passing to the script.
Otherwise, $name and $id, are null.
See the edited code above.John
-
May 9, 2003, 13:05 #5
- Join Date
- Apr 2003
- Location
- San Juan, Puerto Rico
- Posts
- 215
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks I ended up with this code
PHP Code:<?php
####################################################################
## Getting posted $Variables #######################################
####################################################################
$name = $_POST['ItemName'];
$id = $_POST['ID'];
$submit = $_POST['Update'];
####################################################################
## Updating the Item ###############################################
####################################################################
if ( $submit == "Update" )
{
$sql = "UPDATE Inventory_A SET Item_Name = '$name' WHERE ID = '$id'";
if ( mysql_query( $sql ))
echo "Item $name has been succesfully updated.<br>";
}
echo "$name<br>";
echo "$id<br>";
echo "$submit";
?>"I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." - Albert Einstein
-
May 9, 2003, 13:09 #6
- Join Date
- Jul 2002
- Location
- Along the Wasatch Fault line.
- Posts
- 1,771
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just a note.
If your ID field is of type INT or strictly numeric, remove the single quotes around the $id in your query.John
-
May 9, 2003, 13:33 #7
- Join Date
- Apr 2003
- Location
- San Juan, Puerto Rico
- Posts
- 215
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
another question... how do i separete the differents variables ???
PHP Code:<?php
####################################################################
## Getting posted $Variables #######################################
####################################################################
$name = $_POST['ItemName'];
$id = $_POST['ID'];
$submit = $_POST['Update'];
####################################################################
## Updating the Item ###############################################
####################################################################
if ( $submit == "Update" )
{
$sql = "UPDATE Inventory_A SET
Item_Name = '$name ',
SKU = '$sku ',
Short_Description = '$short_description ',
Product_Group = '$product_group ',
Category = '$category ',
Sub_Category = '$sub_category ',
Product_Price = '$product_price ',
Dealer_Price = '$dealer_price ',
Quantity = '$quantity ',
Small_Picture = '$small_picture ',
Large_Picture = '$large_picture ',
Detailed_Description = '$detailed_description ',
Weight = '$weight ',
Special = '$special ',
Taxable = '$taxable ',
View = '$view ',
Add_To_Cart = '$add_to_cart ',
spic_original_file = '$spic_original_file ',
lpic_original_file = '$lpic_original_file ',
spic_path = '$spic_path ',
lpic_path = '$lpic_path '
WHERE ID = $id";
if ( mysql_query( $sql ))
echo "Item $name has been succesfully updated.<br>";
}
echo "$name<br>";
echo "$id<br>";
echo "$submit";
?>"I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." - Albert Einstein
-
May 9, 2003, 13:59 #8
- Join Date
- Jul 2002
- Location
- Along the Wasatch Fault line.
- Posts
- 1,771
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Alright, if you are fairly new to PHP things are going to look a little wierd:
PHP Code:<?php
####################################################################
## Getting posted $Variables #######################################
####################################################################
// I highly recommend that you change your form variables to the ones
// listed in the $fieldArray below, or change the variables names in the
// $fieldArray AND the query to the names that you have on your
// form. It helps keep things cleaner and there is less maintenance and
// headache trying to figure out where a value changes variable names.
$fieldArray = ( name,
id,
sku,
short_description,
product_group,
category,
sub_category,
product_price,
dealer_price,
quanitity,
small_picture,
large_picture,
detailed_description,
weight,
special,
taxable,
view,
add_to_cart,
spic_original_file,
lpic_original_file,
spic_path,
lpic_path,
Update );
// The foreach() loop tests each $_POST[] variable for a value and assigns
// it to its coresponding variable.
// if there is no value in the $_POST[] variable, then the coresponding
// variable is set to "" (no value)
foreach( $fieldArray as $fieldValue )
{
$$fieldValue = isset( $_POST[$$fieldValue] ) ? $_POST[$$fieldValue] : '';
}
####################################################################
## Updating the Item ###############################################
####################################################################
// Any field that accepts a NUMERIC value only needs to have the
// variable that it is being set to bare of single quotes.
// Just like you did with $id and the ID field
if ( $submit == "Update" )
{
$sql = "UPDATE Inventory_A
SET
Item_Name = '$name',
SKU = '$sku',
Short_Description = '$short_description',
Product_Group = '$product_group',
Category = '$category',
Sub_Category = '$sub_category',
Product_Price = '$product_price',
Dealer_Price = '$dealer_price',
Quantity = '$quantity',
Small_Picture = '$small_picture',
Large_Picture = '$large_picture',
Detailed_Description = '$detailed_description',
Weight = '$weight',
Special = '$special',
Taxable = '$taxable',
View = '$view',
Add_To_Cart = '$add_to_cart',
spic_original_file = '$spic_original_file',
lpic_original_file = '$lpic_original_file',
spic_path = '$spic_path',
lpic_path = '$lpic_path'
WHERE
ID = $id
";
if ( mysql_query( $sql ))
echo "Item $name has been succesfully updated.<br>";
}
echo "$name<br>";
echo "$id<br>";
echo "$submit";
?>John
-
May 9, 2003, 21:15 #9
- Join Date
- Apr 2003
- Location
- San Juan, Puerto Rico
- Posts
- 215
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
well i ended up doing this...
and it works, whats the different from yours and mine ???
PHP Code:<?php
####################################################################
## Getting Posted $Variables #######################################
####################################################################
$submit = $_POST['Update'];
$id = $_POST['ID'];
$name = $_POST['ItemName'];
$sku = $_POST['SKU'];
$short_description = $_POST['ShortDescription'];
$product_group = $_POST['ProductGroup'];
$category = $_POST['Category'];
$sub_category = $_POST['SubCategory'];
$product_price = $_POST['ProductPrice'];
$dealer_price = $_POST['DealerPrice'];
$quantity = $_POST['Quantity'];
$small_picture = $_POST['Small'];
$large_picture = $_POST['Large'];
$detailed_description = $_POST['DetailedDescription'];
$weight = $_POST['Weight'];
$special = $_POST['Special'];
$taxable = $_POST['Taxable'];
$view = $_POST['View'];
$add_to_cart = $_POST['Add'];
$spic_original_file = $_POST['spic_original'];
$lpic_original_file = $_POST['lpic_original'];
$spic_path = $_POST['spic_file'];
$lpic_path = $_POST['lpic_file'];
####################################################################
## Updating The Item ###############################################
####################################################################
if ( $submit == "Update" )
{
$sql = "UPDATE Inventory_A SET
Item_Name = '$name',
SKU = '$sku',
Short_Description = '$short_description',
Product_Group = '$product_group',
Category = '$category',
Sub_Category = '$sub_category',
Product_Price = '$product_price',
Dealer_Price = '$dealer_price',
Quantity = '$quantity',
Small_Picture = '$small_picture',
Large_Picture = '$large_picture',
Detailed_Description = '$detailed_description',
Weight = '$weight',
Special = '$special',
Taxable = '$taxable',
View = '$view',
Add_To_Cart = '$add_to_cart',
spic_original_file = '$spic_original_file',
lpic_original_file = '$lpic_original_file',
spic_path = '$spic_path',
lpic_path = '$lpic_path'
WHERE ID = '$id' ";
if ( mysql_query( $sql ))
$color ="GREEN";
echo "Item <FONT COLOR=$color><b>$name</b></FONT> has been successfully updated.<br><br>";
}"I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." - Albert Einstein
-
May 9, 2003, 21:34 #10
- Join Date
- Jul 2002
- Location
- Along the Wasatch Fault line.
- Posts
- 1,771
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yours will take more maintenance, if you want to add or subtract something.
You could also expand the code I posted to make it create your query.John
Bookmarks