jkewlo Posted July 8, 2008 Share Posted July 8, 2008 what do i use to stop a user from not entering data into a form field? so it is not blank in the database? im creating a custom Forum from the ground up and this would be great! thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/ Share on other sites More sharing options...
wildteen88 Posted July 8, 2008 Share Posted July 8, 2008 This falls down to to basic data validation/verification. Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584835 Share on other sites More sharing options...
jkewlo Posted July 8, 2008 Author Share Posted July 8, 2008 yeah but im thinking something on the lines of iset or w/e it is after they hit submit. so it dosnt enter blank data into the database. like you cannot leave the subject field blank. please go back Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584837 Share on other sites More sharing options...
wildteen88 Posted July 8, 2008 Share Posted July 8, 2008 That is exactly what data validation is. Example [code=php:0]<?php if(isset($_POST['submit')) { if(isset($_POST['title']) && trim($_POST['title']) != '') { $error = 'Please provide a title'; } if(isset($error)) { echo $error; } else { echo 'The title is: ' . $title; // this is where you run your SQL query } } ?> <form method="post"> Title <input type="text" name="title"><br /> <input type="submit" name="submit" value="Post Title"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584846 Share on other sites More sharing options...
jkewlo Posted July 8, 2008 Author Share Posted July 8, 2008 ok thanks and i would do this for all the fields?? iv never used data validation before. so i would have to do like <?php if(isset($_POST['submit'])) { if(isset($_POST['subject'], isset($_POST['body']) &&trim($_POST['subject'], $_POST['body']) != '') would that work? or how would i add more than one input field to it? Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584865 Share on other sites More sharing options...
wildteen88 Posted July 8, 2008 Share Posted July 8, 2008 No, you have to do each field separately. Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584870 Share on other sites More sharing options...
jkewlo Posted July 8, 2008 Author Share Posted July 8, 2008 ugh im confused googled it and still confuses me. <?php $host="localhost"; $dbname="root"; $password=""; $db_name="Forum"; $tbl_name="forum_question"; / mysql_connect("$host", "$dbname", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $topic=$_POST['topic']; $detail=$_POST['detail']; $myusername=$_SESSION['myusername']; $email=$_POST['email']; $datetime=date("d/m/y h:i:s"); $sql="INSERT INTO $tbl_name(topic, detail, username, email, datetime)VALUES('$topic', '$detail', '$myusername', '$email', '$datetime')"; $result=mysql_query($sql); if($result){ header("location: main_forum.php"); } else { echo "ERROR"; } mysql_close(); ?> how would i do it in here. just to get a concept of it. that way i will know next time Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584871 Share on other sites More sharing options...
jkewlo Posted July 9, 2008 Author Share Posted July 9, 2008 im still confused Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-584995 Share on other sites More sharing options...
Btown2 Posted July 9, 2008 Share Posted July 9, 2008 The simple answer is to use if statements to check that each variable you want to insert has been set. There are a few ways to do this... 1. regex (probably a little to advance at this point, but may be helpful in the future for keeping out malicious db entries) 2. simple isset if(!isset($var)){ $error = 'true';} for example if(!isset($username)) { $errormsg = 'You did not enter a username.'; die($errormsg); } if(!isset($password)) { $errormsg = 'You did not enter a password.'; die($errormsg); } if(!isset($anything)) { $errormsg = 'You did not enter a blah blah blah'; die($errormsg); } /*now if it makes it through all of the checks insert the data.*/ $query = 'insert your data here'; this is just psuedocode, so please make changes as nessecary ps: i know my spelling sux. Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-585019 Share on other sites More sharing options...
l0ve2hat3 Posted July 9, 2008 Share Posted July 9, 2008 ok do this <?php $host="localhost"; $dbname="root"; $password=""; $db_name="Forum"; $tbl_name="forum_question"; mysql_connect($host, $dbname, $password)or die(mysql_error()." : cannot connect"); mysql_select_db($db_name)or die(mysql_error()." : cannot select DB"); $topic=$_POST['topic']; $detail=$_POST['detail']; $myusername=$_SESSION['myusername']; $email=$_POST['email']; $datetime=date("d/m/y h:i:s"); if( empty($topic) || empty($detail) || empty($myusername) || empty($email) || empty($datetime) ){ echo "You are missing some data!"; }else{ $sql="INSERT INTO $tbl_name(topic, detail, username, email, datetime)VALUES('$topic', '$detail', '$myusername', '$email', '$datetime')"; $result=mysql_query($sql) or die (mysql_error()); header("location: main_forum.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/113809-warn-user-to-enter-data-and-not-leave-blank/#findComment-585046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.