Jump to content

Warn user to enter data and not leave blank


jkewlo

Recommended Posts

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>

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?

 

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

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.

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");

}
?>







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.