Jump to content

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

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.