Jump to content

Recommended Posts

hi everyone, is there anyone to help me, I created a database with PHPmyadmin with a registration form. to my surprise when I save a member the database assigned random incrementation and then it leaves empty box in database. what can I do?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

If you validate the form checking for empty required submitted fields IN PHP, that returns back to the form if it fails and only will be inserted into the database if it passes validation. Then Yes! no empty fields will appear in the database.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

If you want help here we need a clear description of the problem and the relevant code.

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

in its present state the form will submit as usual and reload the form page, before it gets a chance to process for ajax call.

Share this post


Link to post
Share on other sites

You need to prevent the form from submitting normally.  See the link above.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...