PHP CRUD data not posting even after db connection successful

43 Views Asked by At

I developed the create function for my PHP CRUD, the database connection is successful, but when I post the data, it doesn't do anything. It should either say that the data is posted successfully or that there is an error.

I have two pages:

connect.php:

<?php 
$con = new mysqli('localhost', 'root', '', 'crudoperation');

if(!$con){
    die(mysqli_error($con));
}else{
    echo 'database connection successfully established';
}
?>

and user.php

<?php 
include 'connect.php';

if (isset($_POST['submit'])){ // si on appuye sur submit, poster tous les champs
    $name=$_POST['name'];
    $email=$_POST['email'];
    $mobile=$_POST['mobile'];
    $password=$_POST['password'];

    $sql="insert into `crud` (name, email, mobile, password)
    values('$name','$email','$mobile','$password')";
    $result=mysqli_query($con, $sql);
    if($result){
        echo "Data inserted successfully";
    }else{
        die(mysqli_error($con));
    }
}
?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CRUD PHP</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
  </head>
  <body>
    <div class="container my-5">
        <form method="post">
            <div class="mb-3">
                <label>Name</label>
                <input type="text" class="form-control" placeholder="Enter your name" name="name" autocomplete="off">
            </div>
            <div class="mb-3">
                <label>Email</label>
                <input type="email" class="form-control" placeholder="Enter your email" name="email" autocomplete="off">
            </div>
            <div class="mb-3">
                <label>Mobile</label>
                <input type="text" class="form-control" placeholder="Enter your mobile number" name="mobile" autocomplete="off">
            </div>
            <div class="mb-3">
                <label>Password</label>
                <input type="password" class="form-control" placeholder="Enter your password" name="password" autocomplete="off">
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy" crossorigin="anonymous"></script>
  </body>
</html>

What can be the problem ?

I removed the name="submit" I previously had and instead of showing me a white screen, it showed me the same forms as before submitting.

1

There are 1 best solutions below

0
Amaury On BEST ANSWER

The answer to my mistake was that I didn't activate the error reporting. To activate it:

You have to create a file you open in the code editor and call it info.php inside, add:

<?php

phpinfo();

you'll see a page with the php version and a table with information. do a cmd f on mac or ctrl f on windows and search Loaded Configuration File. You'll find as the value of it, a path with the php.ini file. Find it in your directory and open the file with your favorite code editor. once inside, we want to edit the file at two places. First search with cmd f on mac/ctrl f on windows error_reporting and make sure error_reporting = E_ALL. If not, replace it with the correct value. Then search with cmd f on mac/ctrl f on windows for display_errors and make sure display_errors = On. If not, replace it with the correct value. Then restart MAMP/XAMP/WAMP.

Now you need to make a mistake so in the info.php file, replace the code by this one:

<?php

phpinfo(;

and once you visualise, you'll see the error appear.

In my case the error was that the database password was empty whereas there needed to be a password. I found the old password and entered it, which solved the issue.