-4

This would be a piece of my code.

$name = $_POST['UserNames'];
$pw = sha1($_POST['Passwords']);
$mail = $_POST['Emails'];
$pc = $_POST['Postcodes'];
$status = "0";
mysql_query("INSERT INTO userinfo 
               (Username,Password,Email,Postcode,status,valid) 
               VALUES 
               ('$name','$pw','$mail','$pc','$status','$validate')
");

How do i make sure every input will not be null and the Email will never repeat.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • [INSERT ... ON DUPLICATE KEY UPDATE](https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) ? – Peon Feb 18 '14 at 11:16
  • [array_filter](http://pk1.php.net/manual/en/function.array-filter.php) for `$_POST` and [array_unique](http://pk1.php.net/array_unique) for `$_POST['Emails']` (If array) – Rahil Wazir Feb 18 '14 at 11:19
  • Set a `UNIQUE` index on the column in the database and write code that validates the input before you insert it. – deceze Feb 18 '14 at 11:20

5 Answers5

0

Try this,

mysql_query("INSERT INTO userinfo (Username,Password,Email,Postcode,status,valid) VALUES ('$name','$pw','$mail','$pc','$status','$validate')  ON DUPLICATE KEY UPDATE status='0'");

For more details refer, https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

Vinod VT
  • 6,946
  • 11
  • 51
  • 75
0

use the empty function for the post fields to check if it contains something.

for the email, you must use a UNIQUE clause in your database for this field

Tommy
  • 391
  • 1
  • 2
  • 20
0

About the email: set up the column of Email in userinfo table to be unique, and on insert just do on duplicate ignore

About the fields, well in the way you are working just do isset($_POST['XXX']) && !empty($_POST['XXX']). A better way would be working with some input validation class, like this one.

Community
  • 1
  • 1
Michael Arenzon
  • 541
  • 8
  • 16
0

For empty input you can do the following :

$name = (isset($_POST['UserNames']) && !empty($_POST['UserNames'])) ? $_POST['UserNames'] : FALSE;

For duplicate email check you need to do a SELECT query before executing the INSERT statement.

-1

First check values before mysql query:

$valid = true;
foreach($_POST AS $key=>$value)
{
   if($value == null){
       echo "Please provide ".$key;
       $valid = false;
   }
}
if($valid == true)
{
    // check if email is in DB
    $result = mysql_query("SELECT email FROM user info WHERE Email = '".addslashes($_POST['email'])."'");
    if(mysql_num_rows($result) == 1)
    {
       echo "Email is already registered in our DB";
    }
    else
    {
       mysql_query("INSERT INTO userinfo (Username,Password,Email,Postcode,status,valid) VALUES ('$name','$pw','$mail','$pc','$status','$validate')");
    }
}
Oras
  • 1,036
  • 1
  • 12
  • 18