Jump to content

Help with Upload Form and "additional validation"


Chrisj

Recommended Posts

I was provided this code for an Upload Form, but I need some help with

"do some additional validation here to make sure the submitted data is correct"

And "probably do more filtering here".

Can you enlighten me on what would be required there?

Thanks

 

<?php
try {    
         if (false === isset($_POST) || false === isset($_POST['form_submit']) {       
throw new Exception('The form has not been submitted');    
}   
// collect thge submitted data from $_POST array    
foreach ($_POST as $k => $v) {        
$$k = trim($v);
// probably do more filtering here       
// maybe check that data isn't too short or too long etc    
}
// do some additional validation here to make sure    
// the submitted data is correct
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';    
$user = 'dbuser';    
$password = 'dbpass';

try  {       
$dbh = new PDO($dsn, $user, $password);   
}  catch (PDOException $e) {        
echo 'Connection failed: ' . $e->getMessage();    
}

// insert new row to the database
$statement = $dbh->prepare('INSERT INTO table_name (number, word) VALUES (?, ?)');
$statement->bindParam(1, $number, PDO::PARAM_INT);
$statement->bindParam(2, $word, PDO::PARAM_STR, strlen($word));
if (false === $statement->execute(array($number, $word))) {
        throw new Exception('Failed to insert row to database')   
}

echo 'Everything went successfully ';

} catch (Exception $e) {
    echo $e->getMessage();
}  

Frankly I think you've got things back to front. You would need to tell us what data you have and what validation you need based on requirements for length, character type, file type etc. Then we would help you achieve that validation. I don't see how we can help filter/validate when you didn't even stipulate what any of the fields are...

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.