Jump to content

Recommended Posts

I have two variables

One in a field in mysql which I pull out and compare with another that is sent via $_POST.

 

The problem, if the field data variable is say fred bloggs and the $_POST form data variable is FREDBLOGGS or Fred_bloggs or something that looks the same and for all intensions is the same, how can I do a comparison that 'kicks' or 'echoes out' to the user that these entries could be or are the same and they should the entry before commiting it to the database.

 

Link to comment
https://forums.phpfreaks.com/topic/133170-solved-comparing-variables/
Share on other sites

Nearly there....here's the code so far

so just to review...I want to compare a$_POST input variable with a mysql field variable and if they match they cannot enter that data otherwise its ok....if you see where its coming from.

 

<?php
if ((isset($_POST["Submit"])) && (isset($_POST["RootName"]))){
$root_name = $_POST['RootName'];
$root_ID = $_POST['RootID'];
$root_type = $_POST['RootType'];
$root_desc = $_POST['RootDesc'];
$gallery_name = $_POST['GalleryName'];
$gallery_link = $_POST['GalleryLink'];
//check for copy ID
$result = mysql_query("SELECT category.ID, category.NAME from category WHERE ID='$root_ID'") 
or die(mysql_error());
$row=(mysql_fetch_array($result));

if($row == 0){
//need to check for similar data entry
$result = mysql_query("SELECT category.NAME from category")
or die(mysql_error());
while($row=(mysql_fetch_array($result))){;
$temp_name = $row['NAME'];
$temp_name = str_replace(' ', '', $temp_name);
$temp_name = strtolower($temp_name);
$temp_name = ereg_replace("[^A-Za-z0-9]", "", $temp_name);
//echo $temp_name.'<br/>';

//now strip and check the input
$temp_input = $root_name;
$temp_input = str_replace(' ', '', $temp_input);
$temp_input = strtolower($temp_input);
$temp_input = ereg_replace("[^A-Za-z0-9]", "", $temp_input);
//echo $temp_input;

//now compare apart from spelling 
if($temp_input == $temp_name){
echo 'same name';
break;
}else{
echo $root_name.' Gallery was created';
break;
}}
}else{
//check for repeat ID
$temp_ID = $row['ID'];
echo 'gallery ID'.$temp_ID.' is already taken try another ID ';

}


}


?>

the prob I have now is that it does select name from category and in the while statement gets all of the names in the name field, does a set of string functions to it etc AND gets the input and applies the same to that.

 

ITS only when I compare to find if they match that there seems to be an error in what and when it catches a match.

 

Do I need a do while instead or inside the while( $rows = mysql_fetch_array( $result2 ) ) so as to catch the matching strings and then breaks out and does something based on a match result and/or a non-match

 

HELLLLLP!

 

<?php
if ((isset($_POST["Submit"])) && (isset($_POST["RootName"]))){
$root_name = $_POST['RootName'];
$root_ID = $_POST['RootID'];
$root_type = $_POST['RootType'];
$root_desc = $_POST['RootDesc'];
$gallery_name = $_POST['GalleryName'];
$gallery_link = $_POST['GalleryLink'];
//check for copy ID
$result = mysql_query("SELECT category.ID, category.NAME from category WHERE ID='$root_ID'") or die(mysql_error());
$row=(mysql_fetch_array($result));

if($row == 0){
//need to check for similar NAME data entry
$result2 = mysql_query("SELECT NAME from category order by ID")or die(mysql_error());
while( $rows = mysql_fetch_array( $result2 ) ){
//grab data and apply string functions
$temp_name = $rows['NAME'];
$temp_name = str_replace(' ', '', $temp_name);
$temp_name = strtolower($temp_name);
$temp_name = ereg_replace("[^A-Za-z0-9]", "", $temp_name);
echo 'tempname: '.$temp_name.'<br/>';
//get input data and apply same
$temp_input = $root_name;
$temp_input = str_replace(' ', '', $temp_input);
$temp_input = strtolower($temp_input);
$temp_input = ereg_replace("[^A-Za-z0-9]", "", $temp_input);
echo 'tempinput: '.$temp_input.'<br/>';
if($temp_name == $temp_input){
echo 'match name';
break;
}else{
echo 'not matched';

}

}//end else
}else{
//check for repeat ID
$temp_ID = $row['ID'];
echo 'gallery ID '.$temp_ID.' is already taken try another ID ';

}//end if row


}//end input submit


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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