Udating MySQL Record - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Databases - MySQL, SQL, Oracle, Access and others

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-23-2006, 11:36 PM   #1 (permalink)
Junior Member
 
Join Date: Aug 2006
Posts: 4
Default Udating MySQL Record

I tried this script to update a records in MySQL table. It is not working. can anyone tells me what i'm doing wrong? I have the id in the form as a hidden field and i'm only updating some of the fields not all of them. Do i have to update all the table fields for it to work?


foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
require_once("mydatabaseconnection");
$db1=mysql_connect($Host, $User, $Pass, $DBName);
mysql_select_db("mydatabase");
echo ";
echo ";
$query="UPDATE table1 set ".
"contact_email= \"".$formVars["contact_email"]."\",".
"contact_phone= \"".$formVars["contact_phone"]."\",".
"city= \"".$formVars["city"]."\",".
"title= \"".$formVars["title"]."\",".
"brief_des= \"".$formVars["brief_des"]."\",".
"comments= \"".$formVars["comments"]."\",".
"price= \"".$formVars["price"]."\",".
"\" WHERE id = \"".$formVars["id"]."\"";
mysql_query($query);
mysql_close($db1);
mathieu67 is offline   Reply With Quote
Sponsored Links
Old 08-30-2006, 03:55 PM   #2 (permalink)
Regular User
 
Join Date: Jul 2005
Posts: 47
Default Re: Udating MySQL Record

Quote:
can anyone tells me what i'm doing wrong?
What error message do you receive?
Quote:
Do i have to update all the table fields for it to work?
Uuhh - NO
sandman is offline   Reply With Quote
Old 09-03-2006, 04:45 AM   #3 (permalink)
Junior Member
 
Join Date: Aug 2006
Posts: 29
Default Re: Udating MySQL Record

PHP Code:
<?php
$query
="UPDATE table1 SET `contact_email` = '".$formVars["contact_email"]."', `contact_phone` = '".$formVars["contact_phone"]."', `city` = '".$formVars["city"]."', `title`= '".$formVars["title"]."', `brief_des` = '".$formVars["brief_des"]."', `comments` = '".$formVars["comments"]."', `price` = '".$formVars["price"]."' WHERE `id` = '".$formVars["id"]."' LIMIT 1";

?>
LIMIT 1 used because id is unique, isn't it? If not, delete it... Don't use " for all cases, those backslashes look really ugly - better use " and '.

Also, you should put the code after foreach in { and }, like that (it's increasing code readibility):
PHP Code:
<?
foreach($HTTP_POST_VARS as $varname => $value)
{
$formVars[$varname]=$value;
}
?>
mysql_connect doesn't really take $DBName parameter, you're selecting DB in your next line, with mysql_select_db...

PHP Code:
<?
foreach($HTTP_POST_VARS as $varname => $value)
{
$formVars[$varname]=$value;
}
// require_once("mydatabaseconnection"); <-- And that line is for what?
$db1=mysql_connect($Host$User$Pass);
mysql_select_db("mydatabase");
$query="UPDATE table1 SET `contact_email` = '".$formVars["contact_email"]."', `contact_phone` = '".$formVars["contact_phone"]."', `city` = '".$formVars["city"]."', `title`= '".$formVars["title"]."', `brief_des` = '".$formVars["brief_des"]."', `comments` = '".$formVars["comments"]."', `price` = '".$formVars["price"]."' WHERE `id` = '".$formVars["id"]."' LIMIT 1";
mysql_query($query);
mysql_close($db1);
?>
If you're debuging it, I'd first comment out all the DB connection and print $formVars array - if it's set properly, then it's your DB connection that is the problem. Check the query (echo it) after passing values with foreach...
At last, use DB layer to make the connection and working with MySQL easier.
moriakaice is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 08:12 AM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting