How do I delete and edit database data?

Asked

Viewed 44 times

-2

Well, I made a registration system for a video classroom gringa (United States), so I want to be able to edit the data that this system sends to the bank from his code, that is. I want to make a system to edit and delete using the same registration code.

Here’s the code of the verification page, the one that checks everything and if it’s all right, it sends it to the bank.

 
    


$data = $_POST;

 session_start();

if (empty($data['nome']) ||
    empty($data['sobrenome']) ||
    empty($data['email']) ||
    empty($data['senha']) ||
    empty($data['repetirsenha'])) {
     $_SESSION['messages'] [] = 'Por Favor preencha todos os campos!';
   header('Location: /create_account'); 
  exit; 
}if ($data['senha'] !== $data['repetirsenha']) {
   $_SESSION['messages'] [] = 'As senhas não estão iguais!';  
   header('Location: /create_account'); 
   exit;
}

 require_once 'config.php';

 $statement = $connection->prepare('SELECT * FROM users WHERE nome = :nome OR email = :email');

 if($statement){

   $statement->execute([
 
    ':nome' => $data['nome'],
    ':email' => $data['email'],
 
   ]);

   $result = $statement->fetchAll(PDO::FETCH_ASSOC);

   if(!empty($result)){

    $_SESSION['messages'][] = 'Este email já está cadastrado!';
    header('Location: /create_account');
    exit;

   }

 }

 $statement = $connection->prepare('INSERT INTO users(nome, sobrenome, email, senha, repetirsenha) VALUES (:nome, :sobrenome, :email, :senha, :repetirsenha)');
 if($statement){
  $result = $statement->execute([
    ':nome' => $data['nome'],
    ':sobrenome' => $data['sobrenome'],
    ':email' => $data['email'],
    ':senha' => $data['senha'],
    ':repetirsenha' => $data['repetirsenha'],
  ]);

   if($result){
    $_SESSION['messages'] [] = 'Bem-Vindo Ao Aquinoflix, Agora você pode desfrutar de um vasto conteúdo de filmes e séries';
    header('Location: /browser');
    exit;
   }

 }


 

While the form, I already know how to do, the problem is even with this file, which is responsible for checking and storing the data in the database. In case he would have to turn one to edit and another to delete the database data. If anyone can take the code here and already show how it looks exactly I’d appreciate it.

I’ll leave here the connection file code, config.php, in case someone wants to use it to test something or whatever.

  // Site link geral



// connection with mysqli important!
 $dsn = 'mysql:dbname=install;host=localhost';
 $user = 'root';
 $password = '';

 try{

 $connection = new PDO($dsn, $user, $password);
  } catch(PDOException $exception){
     $_SESSION['messages'] [] = 'Connection Failed: ' . $exception->getMessage();
     header('Location: /index.php');
     exit;
  }
  • To delete use the SQL command DELETE and to change the command UPDATE.

  • So I just change? But where exactly should I put, in SELECT or in place of INSERT INTO?

1 answer

0


If you just want to delete and edit the user the commands are these:

Erase:

$statement = $connection->prepare('DELETE FROM users WHERE email = :email');

Edit:

$statement = $connection->prepare('UPDATE users SET nome = :novonome WHERE email = :email');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.