PHP: Testing input against a database

Status
Not open for further replies.

undark

Ars Scholae Palatinae
638
I'm getting input from a url like this: index.php?name=undark

I started off by inserting that value into a database, but now my problem is that I don't want to have duplicates in the database. I'm pretty much a newbie, and this is the only way I can think to do this. Either way, it's not working. Here's my code:
Code:
if ($name)
{
	while ($row = mysql_fetch_array($result))
	{
		$test = $row['name'];
	
		if ($name != $test)
		{
			$query = "INSERT INTO users VALUES ('$name','NULL')";
			mysql_query($query) or die("Could not execute query");
		}
	}
}

Any suggestions on how to get this to work?
 

_klamath_

Ars Scholae Palatinae
770
You didn't say what the problem was, so it's difficult to tell you how to fix it. Saying "it doesn't work" isn't generally sufficient.

But there are some simple solutions: just remember to watch out for race conditions (your code above, for example, doesn't take this into account).

Try creating a UNIQUE index on the column you want to be unique. Then just insert the value you got from the client unconditionally (after de-sanitizing it for security) -- if there is already a column with that value, the insert will return an error so you can ask the client for another input value.

P.S. Is there a reason you're using MySQL? It's a pretty bad database...
 

undark

Ars Scholae Palatinae
638
Well, the problem is that I just don't get any output from this. I would normally just do some tests to figure out where the problem lies, but I was hoping someone would be able to tell me a better way to implement this.

Define race condition. I am unfamiliar with this term.

As for MySQL, I was under the impression that it was a pretty good database. I'm probably just misinformed. What should I be using?
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
make SQL do the work... You'll have to make sure I didn't fuck up the code and such, but that's the idea.

Code:
 if ($name){
	$result = mysql_query("SELECT * FROM users WHERE name LIKE "$name'");
	if (mysql_rows_returned = 0)		{
		$query = "INSERT INTO users VALUES('$name','NULL')";
		mysql_query($query) or die("Could not execute query");
	}
}

EDIT:
My-ICBIN-SQL.jpg


You can do what he said, but what I didn't ought to fix what your code was doing. I do use MySQL, but because higherups force it upon me.
 

undark

Ars Scholae Palatinae
638
Turns out MySQL doesn't support mysql_rows_returned(). Anyway, I looked on php.net for an equivalent function and this is how my code turned out:
Code:
if ($name)
	{
	
		$query = "SELECT * FROM users WHERE name LIKE '$name'";
		$result = mysql_query($query);
		$num_rows = mysql_num_rows($result);

		if ($num_rows == 0)
			{
				$query = "INSERT INTO users VALUES('$name','NULL')";
				mysql_query($query) or die("Could not execute query");
			}
	}

Thanks.
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
Originally posted by clm100:
make SQL do the work... You'll have to make sure I didn't fuck up the code and such, but that's the idea.

I told ya I didn't try the code. But yeah, the nice (and bad sometimes) thing about php + MySQL is that there are 200 solutions to every problem. And about databases, there's lots of debate. I personally like MySQL except for it's lack of sub selects and a few minor things like that. PostgreSQL is considered a "superior" solution, but there's not real reason to change unless you constantly find the lack of some function bad.
 
Status
Not open for further replies.