Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check to see if i already have the data

Status
Not open for further replies.

Cristii00

Programmer
Nov 18, 2002
8
0
0
RO
i need to check the database and see wether the information i'm about to enter doesn't already exist. what's the best way to do that?

here's the code i'm using to insert the new data. but before that i should see wether either one (the cod "cod" or the name) is already there.

Code:
<?php
include('dbcon.php');

if (!empty($_GET[cod]) && !empty($_GET[name])) {

		$query = &quot;INSERT INTO mytable VALUES ('$_GET[cod]', '$_GET[name]')&quot;;
		$result = mysql_db_query(&quot;mydbase&quot;, $query);
		if ($result) {
			echo &quot;<p>Name $_GET[name] was add</p>&quot;;
			echo &quot;<p>with cod : $_GET[cod] .</p>&quot;;
		}
	
}
else{ echo &quot; there are empty fields &quot;;
}
?>
 
I think you should do a SELECT query on your database to see if one (or more) records exists. Something like this :

include ('dbcon.php');
$query = &quot;SELECT * DROM mytable WHERE field = '&quot;.$_GET[cod].&quot;'&quot;;
$result = mysql_query($result, $connection);
$mytable = mysql_fetch_array($result);

if ($mytable[field] == $_GET[cod])
$fieldexists := true;

But maybe there are other ways.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top