Jump to content

Please help with field validation


lingo5

Recommended Posts

Hi,

I have this form to create a new user:

 

<form action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data" name="form1" id="form1" on>
  <table width="850" border="0" align="center" cellpadding="8" cellspacing="0">
    <tr>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="10">
        <tr>
          <td><table width="100%" border="0" cellspacing="0" cellpadding="6">
            <tr>
              <td><span class="CP_blueTXT">Nombre de la persona autorizada</span></td>
            </tr>
            <tr>
              <td>
                <input name="usuario_nombre" type="text" class="CP_loginFormFields" id="usuario_nombre" size="32" /></td>
            </tr>
          </table></td>
        </tr>
      </table>
        <table width="100%" border="0" cellpadding="10" cellspacing="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="6">
              <tr>
                <td><span class="CP_blueTXT">Nombre de usuario</span></td>
              </tr>
              <tr>
                <td>
                  <input name="usuario" type="text" class="CP_loginFormFields" id="usuario" size="32" /> <span class="CP_SiNoText"><?php echo isset($errorMsg) ? $errorMsg : '';?></span></td>
              </tr>
            </table></td>
          </tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="10">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="6">
              <tr>
                <td><span class="CP_blueTXT">Contraseña</span></td>
              </tr>
              <tr>
                <td>
                  <input name="password" type="text" class="CP_loginFormFields" id="password" size="32" /></td>
              </tr>
            </table></td>
          </tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="6">
              <tr>
                <td width="2%"> </td>
                <td width="98%"><input name="upload" type="submit" class="box" id="upload" value=" crear usuario" /></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
    </tr>
  </table>
</form>

 

...and I use this query to cretae a new user record:

 

$colname_checkdup_RS = "-1";  // this checks that if the user entered is already in database
if (isset($_POST['usuario'])) {
  $colname_checkdup_RS = $_POST['usuario'];
}
mysql_select_db($database_MySQLconnect, $MySQLconnect);
$query_checkdup_RS = sprintf("SELECT * FROM t_usuario WHERE usuario = %s", GetSQLValueString($colname_checkdup_RS, "text"));
$checkdup_RS = mysql_query($query_checkdup_RS, $MySQLconnect) or die(mysql_error());
$row_checkdup_RS = mysql_fetch_assoc($checkdup_RS);
$totalRows_checkdup_RS = mysql_num_rows($checkdup_RS);

if($totalRows_checkdup_RS==1)
{
$errorMsg =  "el usuario introducido ya existe en la base de datos"; // duplicate entry found message
}

else{

$query = "INSERT INTO eu45antinew.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 


//echo "<br>Files uploaded<br>";

header("Location: PC_users_display.php"); 
}
}
?>

 

The query includes a function to check if the value entered in "user" already exists. This works fine, but I want the field "user" to be an email address...so I would like to validate that field...how can I do that?

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/251962-please-help-with-field-validation/
Share on other sites

What you can do is set the email field in your table to be unique. Doing that the query will return an error if the email already exists. By doing it like this, you also don't need to select anything first to validate. You just run the INSERT query, and check what the response from the database is

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.