Delete a column if you do not add another column in php with prepared statements

0

I have a question, I hope you can solve it for me. First of all, I have to clarify, that the code I did works correctly, but not at all. I explain first what I want to do.

I am practicing with PHP and MYSQLI, and I am creating a system of users and notifications as a way of practicing. Well, the registration form works correctly, the first notification is added correctly, but while I was assembling the code something happened to me. It happened that, when I registered with this system, the user was created correctly, but not the notification, and it is not the idea, because the idea is that if one of the two queries did not work, that directly does not do anything, that I generated the user or the notification.

Try with:

if(true && true){}

But it did not work, and I do not know what is happening. I leave all the code that obviously can be improved a lot, but as I said, I'm just starting and I need a guide, do not criticize. I know that my code can be totally improved, and I would appreciate it if you would not criticize my code, help me. Thanks in advance to those people who suggest you do X thing before criticizing.

Code:

include "../class/class.conexion.php";
include "define.urls.php";

$db = new Conexion();

if (isset($_POST["name"])) {
    $name = $_POST["name"]; // Nombre real del usuario
    $username = $_POST["username"]; // Nombre de usuario o nickname
    $email = $_POST["email"]; // Email del usuario
    $password = password_hash($_POST["password"], PASSWORD_DEFAULT); // Contraseña del usuario

    $imagen_de_perfil = website_images."/profile-image-lyrians.png"; // Imagen de perfil por defecto
    $imagen_de_portada = website_images."/profile-cover-lyrians.png";
    $rango = "novato"; // Rango inicial del usuario
    $descripcion = NULL;
    $confirmacion = "no confirmado";

    // Notification
    $id_notification = NULL;
    $notificacion = htmlentities("http://unapagina.com;Gracias por formar parte de esta comunidad;Te recomiendo leer este post");
    $time = time(); // Tiempo exacto de la notificacion
    $idAdmin = 1;

    // Verifico si en la tabla users no hay ningun usuario con el mismo EMAIL o USERNAME
    if ($verify = $db->prepare("SELECT username, email FROM users WHERE username = ? OR email = ?")) {
        $verify->bind_param("ss", $username, $email);
        $verify->execute();
        $verify->store_result();
        $verify->bind_result($username, $email);

        if ($verify->num_rows > 0) {
            echo "0";

            $verify->close();
        } else{
            $user = $db->prepare("INSERT INTO users (user_image, 
                                                     user_cover_page,
                                                     username,
                                                     user_description,
                                                     email, 
                                                     password, 
                                                     name, 
                                                     rango,
                                                     email_confirmation,
                                                     user_register_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

            $notification = $db->prepare("INSERT INTO notifications (notification_user, 
                                                                     notification_send_user, 
                                                                     notification_title, 
                                                                     notification_datetime) VALUES (?, ?, ?, ?)");

            $user->bind_param("sssssssssi", $imagen_de_perfil,
                                            $imagen_de_portada,
                                            $username, 
                                            $descripcion, 
                                            $email, 
                                            $password, 
                                            $name, 
                                            $rango,
                                            $confirmacion,
                                            $time);

            if ($user->execute()) {
                $userID = $user->insert_id;
                $notification->bind_param("iisi", $userID, $idAdmin, $notificacion, $time);

                if ($notification->execute()) {
                    echo "1";
                } else{
                    echo "notification";
                }

                $user->close();
                $notification->close();
            }
        }
    } else{
        echo $db->error;
    }
}
    
asked by Axel Benitez 19.12.2017 в 00:11
source

0 answers