Jump to content

How to overwrite an existing table record


jllav03

Recommended Posts

I am new to PHP.

 

Goal: I want to create a php function that will override an existing table record

Condition: Only if the text in the id input box is identical to what is already in the database.

Currently, it just creates a new record.

 

The id in question is a Title field within an html Form. So if the title which has an id of dmname is already in the database, the new entry should override/replace what is there.

 

I'm using Joomla as my cms system which has an area for the PHP code and uses MySQL.

 

HTML snipet:

 

<div class="form_item">

  <div class="form_element cf_textbox">

    <label class="cf_label" style="width: 150px;">Title:</label>

    <input class="cf_inputbox required" maxlength="150" size="30" title=""id="dmname" name="dmname" type="text" />

  </div>

 

Please let me know if I need to clarify more. Thank you

Link to comment
Share on other sites

I'm getting closer. I was able to mark my field TITLE as Unique, (im using myPHPAdmin), and it works. But when I create a new record with the same TITLE name, it errors out, instead of overwriting the existing record. The article below perfectly addresses my problem, but in phpMyAdmin, how do I set the field to NOT NULL and change INSERT to INSERT IGNORE? I can't even find an option for that.

 

*********

What I found:

 

To prevent multiple records with the same first and last name values from being created in this table, add a PRIMARY KEY to its definition. When you do this, it's also necessary to declare the indexed columns to be NOT NULL, because a PRIMARY KEY does not allow NULL values:

 

 

CREATE TABLE person_tbl

(

  first_name CHAR(20) NOT NULL,

  last_name CHAR(20) NOT NULL,

  sex CHAR(10)

  PRIMARY KEY (last_name, first_name)

);

 

The presence of a unique index in a table normally causes an error to occur if you insert a record into the table that duplicates an existing record in the column or columns that define the index.

 

Use INSERT IGNORE rather than INSERT. If a record doesn't duplicate an existing record, MySQL inserts it as usual. If the record is a duplicate, the IGNORE keyword tells MySQL to discard it silently without generating an error.

 

*********

Link to comment
Share on other sites

Thanks Pikachu. Would this be the correct syntax for the SQL statement then? I'm only using one unique field.

 

UPDATE `jos_docman` SET `dmname`=[value-3], WHERE `dmname`=1

 

jose_docman being the table,

dmname being the field name,

[value-3] being the column's value,

and WHERE dmname=1, meaning only one entry is allowed?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.