Go Back   HTMLCenter Web Development Forums > Web Design and Development > Programming and Scripting
Connect with Facebook

Help with PHP Update and Insert script

Closed Thread
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 06-25-2009, 11:21 AM
Registered User
 
Join Date: Jun 2009
Posts: 1
Exclamation Help with PHP Update and Insert script

I'm trying to use a web form to either update a database record if it exists (using the email address as the verification), or insert a new record if it does not... and I'm clueless!

Right now I have the insert script working, I just don't know how to include the Update.

Here's the working code I have for the insert and page redirect when complete:

PHP Code:
$query="insert into tblMyTableName (firstname,lastname,company,title,email,phone,v1datesubmitted,v1,v2,campaign) values ('".$firstname."','".$lastname."','".$company."','".$title."','".$email."','".$phone."','".$v1datesubmitted."','".$v1."','".$v2."','".$campaign."')";

mysql_query($query);

header("Refresh: 0;url=../?page_id=25"); 
I appreciate ANY help on this!! Thanks!!

Last edited by curtiss; 06-27-2009 at 07:55 AM.. Reason: Added code tags

  #2 (permalink)  
Old 06-27-2009, 08:02 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,533
Try something like this (assuming you're using the e-mail address as the unique identifier for each record):
PHP Code:
$exists false;
$query "SELECT COUNT(*) FROM tblMyTableName WHERE email='$email";
if(
$query mysql_query($query)) {
if(
$rs mysql_fetch_array($query)) {
if(
$rs[0] > 0) {
$exists true;
}
}
}

if(
$exists) {
$query "UPDATE tblMyTableName SET firstname='$firstname',lastname='$lastname',company='$company',title='$title',phone='$phone',v1datesubmitted='$v1datesubmitted',v1='$v1',v2='$v2',campaign='$campaign' WHERE email='$email'";
}
else {
$query="insert into tblMyTableName (firstname,lastname,company,title,email,phone,v1datesubmitted,v1,v2,campaign) values ('".$firstname."','".$lastname."','".$company."','".$title."','".$email."','".$phone."','".$v1datesubmitted."','".$v1."','".$v2."','".$campaign."')";
}

mysql_query($query);

header("Refresh: 0;url=../?page_id=25"); 
__________________
I hate Internet Explorer! Anyone with me?
  #3 (permalink)  
Old 06-30-2009, 07:42 AM
Registered User
 
Join Date: Jan 2009
Location: Localhost
Posts: 50
Try

INSERT ON DUPLICATE KEY UPDATE

option of mysql.
__________________
Dedicated Servers - sales@hivelocity.net - 1-888-869-HOST(4678)
Award winning Managed Hosting - Dedicated Server Hosting
Managed Dedicated Servers. Reseller Discounts. 24/7 Impressive Tech Support.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
no update made and no table created gilgalbiblewhee Databases 0 07-15-2008 11:26 AM
Phormer Gallery and PHP editing.. yuppicide Programming and Scripting 3 10-11-2007 06:03 PM


All times are GMT -5. The time now is 05:21 AM.

 
Clicky Web Analytics
CloudContacts
Loop11
Page.ly


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.5.1 PL1
© 1997-2009 HTMLCenter