Jump to content

Comparing a db query with user input.


ibuprofen

Recommended Posts

The code below is a function that checks to see if an email address exists in a database, if so it alerts the user. The db has one table and one field. It works fine when there is ONE record! However, if there are > 1 it doesn't work. How can I step through each record and compare it to what the user entered? Of course,  $_POST is the user's value and the db record is the $myAddy value.

 

<?php

function emailLookup()

{

include ('file:///Library/WebServer/Documents/re_connect_scripts/emailLookup.php');

while ($row = mysqli_fetch_array($result))

{

extract ($row);

$myAddy = $addy;

}

if ($_POST["add_email"] == $myAddy)

{

global $lookupError;

$lookupError = 'This email address is already on the list.';

global $counter;

$counter++;

}

else

{

return;

}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/210377-comparing-a-db-query-with-user-input/
Share on other sites

your searching for the number of times there is a mail address in  the DB?

<?php
function fetchNum($sql){


	$result = mysql_query($sql);
	$num_rows = mysql_num_rows($result);	

	if($num_rows < 1){
		$result = 0;
	} else {
		$result = $num_rows;
	}

	return $result; 
}

$sql = "select * from tablename where fieldname ='".$_POST['REQUEST VAR']."' ";

if(fetchNum($sql) >= 1))
{
        echo "exists";
}

else 
{
        echo "doest exist";
}

?>

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.