ranshemer Posted September 9, 2011 Share Posted September 9, 2011 Hi i'm a newbie in this forum i like W3S is the best page ever for learning and be a web developer and i need help... how do i know if a filed is empty on php & mysql block code and make a redirection on the same page or other files for example... if we have a form called persons.php and have this code <html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html> and then we have this file call it insert.php <?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);$sql="INSERT INTO Persons (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con)?> and if a have fristname as a UNIQUE field in mysql... how do i know if a field is empty and make a redirection to a pages or file? (that includes same files on your root folder) i hope that you help me thx Link to comment Share on other sites More sharing options...
thescientist Posted September 9, 2011 Share Posted September 9, 2011 just check the value of the $_POST members you want to validate against. Since they come over as strings, if there was nothing submitted to them, you could use if($_POST['firstname'] === ''){ //redirect logic here}else{ //all other code here}; Link to comment Share on other sites More sharing options...
ranshemer Posted September 9, 2011 Author Share Posted September 9, 2011 ok thx i thing i have a idea how to do it ,i will show my real code: i have 2 files there name are in Spanish so sry (it is because is my native language) CrearUsario.php (Create User) proceso_usuario.php (user process) (with which make the insert to mysql) This is CrearUsuario.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="stylesheet" href="user_estilo.css" type="text/css" /><title>Crear Usuario</title></head><body><div id="forma"> <?php include("menu.php"); ?><h2 id="titulo_peque">cree su usario:</h2><form name="datos" action="proceso_usuario.php" method="post" > <p class="texto">username:<input type="text" name="username" id="username" /></p> <p class="texto">password:<input type="password" name="password" id="password" /></p> <p class="texto">Descripcion:</p> <input type="text" name="descripcion" id="descripcion" /> <br /> <input type="submit" name="botonphp" value="Registrar" id="boton"/> </form> </div></body></html> This is proceso_usuario.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>proceso</title><link rel="stylesheet" href="user_estilo.css" type="text/css" /></head><body><div id="forma"><?php include("menu.php"); ?><?php$con = mysql_connect("localhost","root","1234");if (!$con) { die('Error en la coneccion de SQL: ' . mysql_error()); }mysql_select_db("rani", $con);$sql="INSERT INTO usuario (username, password, descripcion)VALUES ('$_POST[username]','$_POST[password]','$_POST[descripcion]')";if (!mysql_query($sql,$con)) { die('Error con las tablas: ' . mysql_error()); }echo "Usuario Creado";mysql_close($con)?> so it will be ... note: i what to redirect to CrearUsuario.php <?php$sql="INSERT INTO usuario (username, password, descripcion)VALUES ('$_POST[username]','$_POST[password]','$_POST[descripcion]')";if($_POST['username'] && $_POST['password'] ==""){ //DONT KNOW THE REDIRECT CODE CANT YOU TELL}echo field can't be empty?> Link to comment Share on other sites More sharing options...
thescientist Posted September 9, 2011 Share Posted September 9, 2011 what you're looking for is the header function.http://www.w3schools.com/php/func_http_header.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.