0
0
0ldn0mad2019-11-14 14:13:05
PHP
0ldn0mad, 2019-11-14 14:13:05

How to change the data from the form and return it back to the array?

There is a function that takes data from $_POST and puts it in the database

function addTask($addData){
    $db = new PDO("mysql:dbname=test; host=localhost", "root", "root");
    $sql = "INSERT INTO tasks (name, task) VALUES (:name, :task)";
    $statement = $db->prepare($sql);
    $statement->execute($addData);
  }

I would like to tie the millstones of verification here and, if necessary, change the data,
if(isset($_POST["name"])):
      $namePost = trim($_POST["name"]);
      $namePost = strip_tags($namePost);
      $namePost = htmlspecialchars($namePost,ENT_QUOTES);
      $namePost = stripslashes($namePost);
    endif;

and then return it again to an array of the same form as $_POST, in order not to change the original function. How to put it together correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2019-11-14
@0ldn0mad

WOULD NOT WANT.
Throw all these idiotic "millstones" in the trash.
Instead, it's better to make a normal function out of that hellish spaghetti you have now.

function addTask($db, $name, $task){
    $sql = "INSERT INTO tasks (name, task) VALUES (?,?)";
    $statement = $db->prepare($sql);
    $statement->execute([$name, $task]);
  }

S
Stanislav, 2019-11-14
@mzcoding

Well, with the original array and work

$_POST['name'] = trim($_POST['name']; 
....
$_POST['name'] = stripslashes($_POST['name']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question