473,698 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking if record with some field exists

Hello,

I am new to PHP so I have done a research on how to check if an entry
exists on the table. I came up with the following code:

include("dbinfo .inc.php");
$Name=$_POST['Name'];
$Code=$_POST['Code'];
mysql_connect($ host,$username, $password);
@mysql_select_d b($database) or die( "Unable to select database");
$result = mysql_query("SE LECT * FROM Contacts WHERE Code=$Code");
if($row = mysql_fetch_arr ay($result)) echo "exists";
else
{$query = "INSERT INTO Contacts VALUES ('','$Name','$C ode')";
echo "ok";}
mysql_query($qu ery);
mysql_close();

This works if the code is integer (1264), however if the code is
string (a4fg5h4) it shows - "Warning: mysql_fetch_arr ay(): supplied
argument is not a valid MySQL result resource in D:\xampp\htdocs \reg
\insert.php on line 10
ok"

I can't found out what is the problem here as all the examples on the
web shows similar codes to do checking.
Regards,
K. Vijayakumar

May 30 '07 #1
1 3564
Edwina Rothschild wrote:
Hello,

I am new to PHP so I have done a research on how to check if an entry
exists on the table. I came up with the following code:

include("dbinfo .inc.php");
$Name=$_POST['Name'];
$Code=$_POST['Code'];
mysql_connect($ host,$username, $password);
@mysql_select_d b($database) or die( "Unable to select database");
$result = mysql_query("SE LECT * FROM Contacts WHERE Code=$Code");
if($row = mysql_fetch_arr ay($result)) echo "exists";
else
{$query = "INSERT INTO Contacts VALUES ('','$Name','$C ode')";
echo "ok";}
mysql_query($qu ery);
mysql_close();

This works if the code is integer (1264), however if the code is
string (a4fg5h4) it shows - "Warning: mysql_fetch_arr ay(): supplied
argument is not a valid MySQL result resource in D:\xampp\htdocs \reg
\insert.php on line 10
ok"

I can't found out what is the problem here as all the examples on the
web shows similar codes to do checking.
Regards,
K. Vijayakumar
Your mysql_query failed.

You should always check the result of any MySQL call (except
mysql_close(), maybe).

$result = mysql_query("SE LECT * FROM Contacts WHERE Code=$Code");
if (!$result)
echo "Query failed!" . mysql_error();
else
if($row = mysql_fetch_arr ay($result)) echo "exists";
...

or,
if (mysql_num_rows ($result) 0) echo "exists";

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 31 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5301
by: Joe Bond | last post by:
Hi. I have a simple MS Access 2000 form in which I enter some customer data. When the address field is entered I need to see if a duplicate record exists. I need to know this *right away* before the remaining fields are filled out, so I'm trying to call a function on the Address_LostFocus() event which will do the lookup. If a match is found, it should prompt the user to either continue, or navigate to the existing record. This is where...
5
3063
by: Sue | last post by:
As soon as a character is entered in the first field of a new record, the number of records shown in the navigation buttons increases by 1 and the new record button becomes enabled. In the BeforeUpdate event of the first field, I check to see if that value has been previously entered. If it has, I do a Cancel = True and a Me.Undo. The number of records does not change and the new record button is still enabled although clicking it does...
2
2831
by: R Bolling | last post by:
I am using a routine to check to see if a phone number (PK) has alread been entered, and takes the user to that record if it is found -- as follows: Private Sub Contact_telephone___BeforeUpdate(Cancel As Integer) Dim rs As DAO.Recordset Dim iAns As Integer Set rs = Me.RecordsetClone rs.FindFirst " = '" & Me! & "'" If Not rs.NoMatch Then
6
1866
by: John | last post by:
Hi We have a staff database and need to make sure that the same staff is not entered twice. Is there a way for Access to flag if combination of forename and surname fields is already in the system when adding a new record? Preferably straight after entering forename & surname so user does not waste time entering the rest of the info. Thanks
5
3334
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example I'm adding a customer. The Customer fields are mostly foreign keys that refer to primary keys in other tables, left join instead of junction tables at this point. So, when I want to add a customer record, I also need to add records to the other...
5
23069
by: BerkshireGuy | last post by:
Hello everyone, I have a bond form that a user uses to enter data. One of my fields, is PolicyNumber. I added some code on the Before Update event of txtPolicyNumber that checks to see if that policy number is in the system when entering a new record. If it is, I want it to display a message and go back to the policy number field. I've tried this code on a policy number that is not in the table and it
20
2132
by: Bryan | last post by:
hello all... im trying to add a record to an sql db on ms sql server 2000, using vb.net. seems to be working.. except for one thing, one of the columns in the database is a bit datatype, and though i get no syntax errors when compiling, i get an error indicated that the data would be truncated. the field is login_status. ive tried in quotes and not, giving it an integer variable with the number 1
2
2115
by: mookid | last post by:
Hello, I am new to PHP so I have done a research on how to check if an entry exists on the table. I came up with the following code: include("dbinfo.inc.php"); $Name=$_POST; $Code=$_POST; mysql_connect($host,$username,$password); @mysql_select_db($database) or die( "Unable to select database");
1
1744
by: Orbie | last post by:
Hi All, I'm new to VB.NET and i'm looking for some help with my Windows Form. I need to check if a Commodity entered into (TextBox1.Text) already exists on my table before i insert it. I'm having issues checking if the number of rows returned from my Select is equal 0? Also i'm wondering should i be checking for an exception and open/closing my connections each time i make a call to my DB or is the way i have it coded below OK?? Any input...
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.