iNko Posted November 2, 2012 Share Posted November 2, 2012 Got this code for inserting data into my database: <?php session_start(); require('config.php'); if(!isset($_SESSION['login'])) { header('location: login.php'); } if(isset($_POST['submitted'])){ $name= $_POST['name']; $sqlinsert = "INSERT INTO Users (name) VALUES ('$name')"; if(!mysql_query($sqlinsert)) { die('Couldnt create name'); } $newrecord = "Name created"; } ?> <form action="newuser.php" method="POST"> <input type="hidden" name="submitted" value="true" /> <label>Name</label> <input type="text" name="name"/> <input name="" type="submit" value="Create name"> </form> <?php echo $newrecord; ?> Im thinking i need to write something like if ( $name != ''){ //insert } else { //show that field is empty } But what if i have multiple fields? am i going in the right dirrection with this? Link to comment https://forums.phpfreaks.com/topic/270221-checking-if-textfield-isnt-empty-when-inserting-data-into-database/ Share on other sites More sharing options...
Jessica Posted November 2, 2012 Share Posted November 2, 2012 use trim and strlen. Link to comment https://forums.phpfreaks.com/topic/270221-checking-if-textfield-isnt-empty-when-inserting-data-into-database/#findComment-1389761 Share on other sites More sharing options...
floridaflatlander Posted November 2, 2012 Share Posted November 2, 2012 Here's a quick poor mans way to do what I think you're doing. if(isset($_POST['submitted'])){ $error = FALSE; // Check for a name: if (empty($_POST['name'])) { $error [] = 1; // Or $error [] = '<h5 style="color: red;">* You forgot to enter a name.</h5>'; and then print this array } else { $name= $_POST['name']; $name = mysqli_real_escape_string($dbc, trim($name)); } // Check for a something: if (empty($_POST['somethnig'])) { $error [] = 1; } else { $something= $_POST['something']; $something = mysqli_real_escape_string($dbc, trim($something)); } if (!$error) { $sqlinsert = "INSERT INTO Users (name) VALUES ('$name')"; $r = mysqli_query($dbc, $sqlinsert); // or die("Error: ".mysqli_error($dbc)); if ($r) {$newrecord = "Name created";} } } Link to comment https://forums.phpfreaks.com/topic/270221-checking-if-textfield-isnt-empty-when-inserting-data-into-database/#findComment-1389768 Share on other sites More sharing options...
iNko Posted November 2, 2012 Author Share Posted November 2, 2012 Re quoting my post (idk why but i cant find the edit button anymore) <?php session_start(); require('config.php'); if(!isset($_SESSION['login'])) { header('location: login.php'); } if(isset($_POST['submitted'])){ $name= $_POST['name']; $surname= $_POST['surname']; $sqlinsert = "INSERT INTO Users (name, surname) VALUES ('$name', '$surname')"; if(!mysql_query($sqlinsert)) { die('Couldnt create user'); } $newrecord = "User created"; } ?> <form action="newuser.php" method="POST"> <input type="hidden" name="submitted" value="true" /> <label>Name</label> <input type="text" name="name"/> <label>Surname</label> <input type="text" name="surname"/> <input name="" type="submit" value="Create user"> </form> <?php echo $newrecord; ?> i added a 2nd textfield and i also found this validation code: $username = trim($_POST['username']); // cut spaces around if(!preg_match('/ {2,}/', $username) // check for more than one space in the middle { // Show some error message... } else if(!preg_match('/^[a-z0-9_ ]+$/', $username)) // check for valid characters { // Show some error message... } else { // Username is good. } so i was thinking, can i do something like: $name= trim($_POST['name']); $surname= trim($_POST['surname']); if(!preg_match('/ {2,}/', $name || $surname) { // Show some error message... } else if(!preg_match('/^[a-z0-9_ ]+$/', $name || $surname)) { // Show some error message... } else { $sqlinsert = "INSERT INTO Users (name, surname) VALUES ('$name', '$surname')"; if(!mysql_query($sqlinsert)) { die('Couldnt create user'); } $newrecord = "User created"; } } EDIT: thx floridaflatlander, ill check ur code right now Link to comment https://forums.phpfreaks.com/topic/270221-checking-if-textfield-isnt-empty-when-inserting-data-into-database/#findComment-1389771 Share on other sites More sharing options...
iNko Posted November 2, 2012 Author Share Posted November 2, 2012 works now, ty for the help guys! Link to comment https://forums.phpfreaks.com/topic/270221-checking-if-textfield-isnt-empty-when-inserting-data-into-database/#findComment-1389777 Share on other sites More sharing options...
Christian F. Posted November 3, 2012 Share Posted November 3, 2012 Only one modification I'd do to your name/surname validator: Remove the underscore, and add a period and a dash instead. So that people with dashed double names, or initials, in their names can register too. Link to comment https://forums.phpfreaks.com/topic/270221-checking-if-textfield-isnt-empty-when-inserting-data-into-database/#findComment-1389942 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.