This topic is locked

Making Field Unique

11/1/2005 10:57:32 AM
PHPRunner General questions
jimhnet author

I want to be able to make a field unique with phprunner, not by declaring it in the db. I only want to do this in the add mode. I need to add 2500 names to an existing table, that do not have email addresses.
I already have the add mode and the edit features running and I'm requiring new add's to have an email.
Obviously, I need to make the email field unique to avoid duplications.
Thanks for all your help!
Jim F

admin 11/2/2005

Jim,
you can add your code to check the entered email for uniqueness in ..._add.php file around this line:

db_exec($strSQL,$conn);


The code can look like this:

$rsunique = db_query("select * from mytable where email='".$avalues[`email`]."'",$conn);

if(!db_fetch_array($rsunique))

{

db_exec($strSQL,$conn);

// move files
foreach ($files_move as $file)

move_uploaded_file($file[0],$file[1]);

$message="<div class=message><<< "."Record inserted"." >>></div>";

}

else

$message="<div class=message><<< The email already exists >>></div>";


where mytableand emailare your actual table and field names.

jimhnet author 11/2/2005

Sergey:
Here is the way I implemented the code, however it always falls out to the else staement and no record ever gets added.
Should the return be outside the whole logic statment?
Jim

$rsunique = db_query("select * from memupdate where email='".$avalues[`email`]."'",$conn);

if(!db_fetch_array($rsunique))

{

db_exec($strSQL,$conn);

//   move files
 foreach ($files_move as $file)

    move_uploaded_file($file[0],$file[1]);

 $message="<div class=message><<< "."Record inserted"." >>></div>";
      ?>

<script>

window.location="http://www.dalrc.org/dalrcfiles/landingpage.html";;

</script>

<?php

return;

}

else

 $message="<div class=message><<< This email address already exists,<br> you must have updated already>>></div>";
}
admin 11/3/2005

Jim,
I suppose you should print out SQL string used to check an uniqueness of a email.

I.e. using this line:

echo "select * from memupdate where email='".$avalues[`email`]."'";



and execute it directly on your MySQL server to determine does it return any rows.
Also please chek that you spelled your field names correctly, they are case-sensitive.