Jump to content

Database


robsstevejobs

Recommended Posts

  • 2 weeks later...

ok, look at my code in html and PHP with the image of my database, it skips the line and it leaves empty box on the table.

 

HTML CODE

Form 

        <form id="news" action="insert.php" method="POST">



            <label for="name">Nom</label>

                <input type="text" name="name" placeholder="votre nom">

                <Br>

                 <br>

            <label for="email">E-mail</label>

            <input type="email" name="email" placeholder="votre mail">

            <br>

            <br>

            <input type="submit" name="button" value="newsletters">

        </form>

 

PHP CODE

 

This is insert.php file 

 

<?php

$servername = "localhost";

$username = "root";

$password = "Robss123";

$dbname = "newsletter_database";



// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

    die("connection failed:" .$conn->connect_error);

}





$name = mysqli_real_escape_string($conn, $_POST['name']);

$email = mysqli_real_escape_string($conn, $_POST['email']);





$sql = "INSERT INTO data_user_insert (name, email)

VALUES ('$name','$email')";





if ($conn->query($sql) === TRUE) {

    echo "Votre enregistrement à reussi";

} else {

    echo "Error: " . $sql . "<br>" . $conn->error;

}



$conn->close();





if ($_SREVER["REQUEST_METHOD"] == "POST"){



  $name = test_input($_POST["name"]);

  $email = test_input($_POST["email"]);



}



  function test_input ($data){

      $data = trim($data);

      $data = stripcslashes($data);

      $data = htmlspecialchars($data);

      return $data;



  }



?>

 

7298D02B-2AC9-48DA-9456-584CBA57D62F.jpeg

Link to comment
Share on other sites

It's probably inserting blank rows because you don't do any validation, you don't check if anything was submitted before inserting, you just insert.  All of the code after you close the database connection isn't doing anything, you have a typo in it but the only thing you do is run a function on things that would be in post.  You don't do anything after doing that though.

You should check to see if the form was submitted before inserting data.

Link to comment
Share on other sites

You need to check if the form is submitted.  The last example on this page does that by checking the request method:

https://www.w3schools.com/php7/php7_form_validation.asp

That's not the only way to check if a form was  submitted, but that's one way.  Everything you want to do when the form is submitted needs to happen after you verify that it was actually submitted.

Link to comment
Share on other sites

you are sure after validating the form empty space would no longer be in the database

You're the programmer.  If you don't want to insert empty rows in the database, then check if the data is empty before inserting.  Doesn't that sound like it would prevent empty rows?

Seriously, a programming language is just the way that we tell computers what to do.  You're the one telling the computer what to do.  If you don't want it to do something, then make it not do it.  It really is that simple.

Link to comment
Share on other sites

Jquery is javascript which can be disabled by your browser. The best and safest option is to always validate using server language such as php before saving to database, then use jquery/javascript to validate client side before it gets submitted  to give a quicker  validation experience to the user without the need validate with php and having to reload and repopulating data entered to correct and continue when validation fails server side.

Link to comment
Share on other sites

  • 4 weeks later...

Look my code 

 

<form id="news" action="" method="POST">

 

            <label for="name">Nom</label>

                <input type="text" name="name" id="name" placeholder="votre nom" required>

                <Br>

                 <br>

            <label for="email">E-mail</label>

            <input type="email" name="email" id="email" placeholder="votre mail" required>

            <br>

            <br>

            <input type="submit" name="submit" id="submit" value="newsletters">

            <span id="error_message" class="text-danger"></span>

            <span id="success_message" class="text-success"></span>

 

        </form>

 

 

This is Ajax code

 

 

 

$(document).ready(function(){

        $('#submit').click(function(){

 

          var name = $('#name').val();

          var email = $('#email').val();

          if(name == '' || email == '')

          {

            $('#error_message').html("All field are required");

          }

          else

          {

            $('#error_message').html('');

            $.ajax({

                    url:"validate.php",

                    method:"POST",

                    data:{name:name, email:email},

                    success:function(data){

                       $("#news").trigger("reset"); 

 

                          $('#success_message').fadeIn().html(data);  

                          setTimeout(function(){  

                               $('#success_message').fadeOut("Slow");  

                          }, 2000);

                      

                    }

 

            });

          }

          

        });

      });

 

</script>

Link to comment
Share on other sites

You need to prevent the form from doing its default action of submitting by using preventDefault();

https://www.w3schools.com/jquery/event_preventdefault.asp

You should not use click event on the submit button but a onsubmit event on the form itself, as a form can be submitted by hitting enter key while within a input element.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...