Jump to content

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

}
?>







This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.