0

How do I insert or replace a value on the database ?

Basically I need to replace one row on the database without alter the rest of the info, in this case is the form_email row based on the id.

On a previous instance I get the email with:

$query = "SELECT `form_email` FROM `$table_name_email` WHERE `id` = $form_id";
$result = mysql_query($query) or die(mysql_error());

while($row_email = mysql_fetch_array($result_email)){
    $email = $row_email[form_email];
    echo $email;
}

But I'm having problems to understand how to inser or replace that email

This is the table structure, just in case.

CREATE TABLE IF NOT EXISTS `contactforms` (
  `id` int(11) NOT NULL auto_increment,
  `form_slug` varchar(100) NOT NULL,
  `form_title` varchar(200) NOT NULL,
  `form_action` mediumtext NOT NULL,
  `form_method` varchar(4) NOT NULL,
  `form_fields` text NOT NULL,
  `submit_button_text` varchar(200) NOT NULL,
  `custom_code` mediumtext NOT NULL,
  `form_style` int(10) NOT NULL default '0',
  `form_email` text NOT NULL,
  `form_success_message` mediumtext NOT NULL,
  `form_thank_you_page` varchar(200) NOT NULL,
  `form_success_title` varchar(150) NOT NULL default 'Form Success!',
  `form_access` text NOT NULL,
  `form_email_subject` varchar(250) NOT NULL,
  `form_email_name` varchar(100) NOT NULL,
  `form_pages` varchar(400) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;

Thanks in advance

user983248
  • 2,588
  • 7
  • 24
  • 44
  • possible duplicate of ["INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"](http://stackoverflow.com/questions/548541/insert-ignore-vs-insert-on-duplicate-key-update) – Sebas Jun 25 '12 at 12:58
  • @Sebas The question you refer asks about “INSERT IGNORE” vs “INSERT ON DUPLICATE KEY UPDATE” (where and what is the best approach - one out of two possibles) Quite different to "How do I insert or replace a value on the database ?" Where the question don't asks about best approach, it just asks how to do it? Is just like saying Argentina's mate is not a mate just because it's smaller than a Uruguayan mate :) – user983248 Jun 25 '12 at 13:47
  • yeah, I picked up the closest one in the list proposed by the engine of stack overflow, I kind of know it was not exactly duplciate but it was to notice at least that this question was treated somewhere else already... – Sebas Jun 25 '12 at 14:36
  • Regarding to the mate I have my own preferences which I guess are not country related ^^ – Sebas Jun 25 '12 at 14:36

1 Answers1

1

You can use the sql UPDATE statement. It allows you to update certain fields for certain records, for example (pseudo code):

UPDATE contactforms
SET form_email = 'something@narly.com'
WHERE id = 'someId'

This will modify all form_email fields to something@narly.com for rows with id someId :)

Deruijter
  • 2,077
  • 16
  • 27