Jump to content

Check if something exists in the database already


lAZLf

Recommended Posts

There are several ways, the most common method is probably to use "SELECT id FROM table WHERE username='$username'" and to then use mysql_num_rows to check how many rows are returned. If it's 0 then you know that the username passed as $username is not in the database already.

I did what u suggested, but it won't work for some reason.

 

I keep getting this error: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home4/annarbo1/public_html/registerin.php on line 12

Your passwords or email adresss don't match up"

 

the code:

<?php
session_start();
$con = mysql_connect("localhost","xxxxxxx","xxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("annarbo1_Archive", $con);
$date = date("Y/m/d");
$sql="INSERT INTO people (fname, lname, username, password, joined, email, level) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[user]',sha1('$_POST[pass]'),'$date','$_POST[email]','norm')";
$sqlcheck="SELECT * FROM people WHERE username = '$_POST[user]' OR email = '$_POST[email]'";
$numrows = mysql_num_rows($sqlcheck);

if (!empty($_POST['pass']) || !empty($_POST['user']) || !empty($_POST['confemail']) || !empty($_POST['confpass']) || !empty($_POST['email']) || !empty($_POST['fname']) || !empty($_POST['lname'])) {
if($_POST['confemail'] == $_POST['email'] && $_POST['pass'] == $_POST['confpass']) {

	if($numrows == 0) {
		if (!mysql_query($sql,$con))
  			{
  				die('Error: ' . mysql_error());
  			} else{
			header ("Location: userinfo.php");
		}
	} else {
		echo "username or email taken already";
	}

} else {
echo"Your passwords or email adresss don't match up";
}
} else {
echo "empty fields";
}
mysql_close($con);
?>

You have syntax issues...try:

$sql="INSERT INTO people (fname, lname, username, password, joined, email, level) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[user]',sha1('$_POST[pass]'),'$date','$_POST[email]','norm')";
$result = mysql_query($sql);
$sqlcheck="SELECT * FROM people WHERE username = '$_POST[user]' OR email = '$_POST[email]'";
$result = mysql_query($sqlcheck);
$numrows = mysql_num_rows($result);

 

check these links for the correct usage of INSERT and SELECT statements

 

http://us.php.net/mysql_num_rows for using mysql_num_rows

 

and

http://us.php.net/manual/en/function.mysql-query.php for SELECT and INSERT statements

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.