473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Prevent entering duplicate records to mysql table

26
Hi there!

Since this is my first post, I'll try to be gentle with you :-)

I have a script that goes like this:

Connect to db. select data from db, and list up (only one variable, text).

I also have a form so I can insert into the database (insert into table where $id = contentid).

How can I check in php, if it is already there?

My code is something like this:

Expand|Select|Wrap|Line Numbers
  1.             $id = intval( mysql_real_escape_string($_GET['id']));
  2.             $as_text = addslashes($_POST['text']);
  3.             $tr_text = trim($as_text);
  4.                 $query = "INSERT INTO jos_issues(ID,text,contentid)
  5.                     VALUES(NULL,'$tr_text','$id')";
  6.             $result = mysql_query($query);
  7.  
How can I easily check if it has already been inserted into the database?

the table consists of 3 columns; id, text and contentid.
contentid is used so I can list all text that is connected to one particular content.

So, now I've been gentle with you, so you can be gentle with me :-)


Phopman
Aug 28 '07 #1
9 2658
ak1dnar
1,584 Expert 1GB
Welcome to TheScripts.com

Your "ID" column is an auto increment field correct?
Then no Need to insert a Null value to it. remove it first.

And back to the question, As I feel you are trying to prevent entering duplicate records to the table.
But when you need it to check? in other words, duplicate entries could be store to the table when pressing refresh button after inserting record(when on the same page) or is it any other instance?
Aug 28 '07 #2
phopman
26
Welcome to TheScripts.com

Your "ID" column is an auto increment field correct?
Then no Need to insert a Null value to it. remove it first.

And back to the question, As I feel you are trying to prevent entering duplicate records to the table.
But when you need it to check? in other words, duplicate entries could be store to the table when pressing refresh button after inserting record(when on the same page) or is it any other instance?
I am trying to prevent duplicate records. one text (ex. 'Heidi',
belongs only to one contentID.

I need to check whenever someone press submit after filling out the form, or whenever someone press refresh button.

How do I do that? do you have any simple examples of this problem? I couldn't find any good examples on google.com, but I don't think I am the first one with a problem like this.


Phopman
Aug 28 '07 #3
ak1dnar
1,584 Expert 1GB
There were few threads on the site, I'm sure. I'll try to find out one for you. keep posting with your updates, then more experts comes to the thread. Please use the site search tool to find out.

Thread Title changed to match with the question:
Update delete and a select from db php. How?
Aug 28 '07 #4
pbmods
5,821 Expert 4TB
Heya, Phopman.

Try using REPLACE instead of INSERT.

REPLACE will automatically remove the conflicting row if it exists before INSERTing the new data.
Aug 28 '07 #5
phopman
26
Thanks for the tips. It works like a charm!
Aug 29 '07 #6
pbmods
5,821 Expert 4TB
Heya, Phopman.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 29 '07 #7
phopman
26
Will do!

I just found this forum, and I hope I can contribute more in time than I am doing now.

Real friendly forum with nice people it seems.

You are people and not computers? :-)
Aug 30 '07 #8
pbmods
5,821 Expert 4TB
I'm sorry, Dave.

I'm afraid I can't answer that.
Aug 30 '07 #9
phopman
26
What are you talking about, HAL?
Sep 3 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Catherine Jo Morgan | last post by:
Can I set it up so that a certain combination of fields can't contain the same entries, on another record? e.g. a combination of FirstName/LastName/address? Or FirstName/LastName/phone? Or...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
11
by: ariel81 | last post by:
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the...
0
by: jehrich | last post by:
Hi Everyone, I am a bit of a hobby programmer (read newbie), and I have been searching for a solution to a SQL problem for a recent pet project. I discovered that there are a number of brilliant...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.