Data Entry problem using php

Discussion in 'Databases' started by bghodsi, Aug 30, 2015.

  1. #1
    I use php script to enter data into a mysql table. But Every time I click submit it enters an empty record with new id followed by the data I entered. The following html php coding. Need your help. Thanks.
    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    .error {
    color:#ff0000;
    }
    form div {

    margin:0;
    padding:0;
    padding-top: 6px;
    }
    .form div input {
    font-family: arial, helvetica, sans-serif;
    font-weight:bold;
    font;size: 10px;
    }
    .form div label {
    float:left;
    width:120px;
    font: bold 0.9em arial, helvetica, sans-serif;
    }

    </style>
    </head>
    <body>

    <?php


    // define variables and set to empty values
    $usernameErr=$passwordErr=$firstnameErr=$surnameErr="";
    $username=$password=$firstname=$surname="";
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(empty($_POST['username'])){
    $usernameErr="User name required";
    }else{
    $username=test_input($_POST['username']);

    }
    if(empty($_POST['password'])){
    $passwordErr="Password required";
    }else{
    $password=test_input($_POST['password']);
    }

    if(empty($_POST['firstname'])){
    $firstnameErr="First name required";
    }else{
    $firstname=test_input($_POST['firstname']);
    }
    if(empty($_POST['surname'])){
    $surnameErr="Surname required";
    }else{
    $surname=test_input($_POST['surname']);
    }
    }

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    ?>

    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field.</span></p>
    <div class="form">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <div>
    <label for="text" >User Name:</label>
    <input type="text" name="username">
    <span class="error">*<?php echo $usernameErr;?></span>

    </div>
    <div>
    <label for="text">Password:</label>
    <input type="text" name="password">
    <span class="error">*<?php echo $passwordErr;?></span>

    </div>
    <div>
    <label for="text">First Name:</label>
    <input type="text" name="firstname">
    <span class="error">*<?php echo $firstnameErr;?></span>

    </div>
    <div>
    <label for="text">Last Name:</label>
    <input type="text" name="surname">
    <span class="error">*<?php echo $surnameErr;?></span>
    </div>
    <div>
    <input type="submit" name="submit" value="Submit">
    </div>
    </form>
    </div>
    <?php
    require ("scripts/connect_to_mysql.php");
    $query="INSERT INTO users (id,username,password,firstname,surname) VALUES('','$username','$password','$firstname','$surname')";
    mysqli_query($myConnection,$query);

    ?>

    </body>
    </html>
     
    bghodsi, Aug 30, 2015 IP
  2. CristianG.

    CristianG. Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    You write the code very bad !
    On insert please do not use `id`=' ' (Empty) do not set it, let the database set automatic if the id si increasement or if not please set the attribute!
    
    <!DOCTYPE HTML>
    <html>
       <head>
         <style>
           .error {
             color:#ff0000;
           }
           form div {
             margin:0;
             padding:0;
             padding-top: 6px;
           }
           .form div input {
             font-family: arial, helvetica, sans-serif;
             font-weight:bold;
             font;size: 10px;
           }
           .form div label {
             float:left;
             width:120px;
             font: bold 0.9em arial, helvetica, sans-serif;
           }
    
         </style>
       </head>
    <body>
    
    <?php
    
    
    // define variables and set to empty values
    $usernameErr = $passwordErr = $firstnameErr = $surnameErr="";
    $username = $password = $firstname = $surname="";
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
       if(empty($_POST['username'])){
         $usernameErr="User name required";
       } else {
         $username=test_input($_POST['username']);
       }
       if(empty($_POST['password'])){
         $passwordErr="Password required";
       } else {
         $password=test_input($_POST['password']);
       }
    
       if(empty($_POST['firstname'])){
         $firstnameErr="First name required";
       } else {
         $firstname=test_input($_POST['firstname']);
       }
       if(empty($_POST['surname'])){
         $surnameErr="Surname required";
       } else {
         $surname=test_input($_POST['surname']);
       }
    }
    
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    ?>
    
    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field.</span></p>
    <div class="form">
       <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
       <div>
         <label for="text" >User Name:</label>
         <input type="text" name="username">
         <span class="error">*<?php echo $usernameErr;?></span>
       </div>
       <div>
         <label for="text">Password:</label>
         <input type="text" name="password">
         <span class="error">*<?php echo $passwordErr;?></span>
       </div>
       <div>
         <label for="text">First Name:</label>
         <input type="text" name="firstname">
         <span class="error">*<?php echo $firstnameErr;?></span>
       </div>
       <div>
         <label for="text">Last Name:</label>
         <input type="text" name="surname">
         <span class="error">*<?php echo $surnameErr;?></span>
       </div>
         <div>
           <input type="submit" name="submit" value="Submit">
         </div>
       </form>
    </div>
    <?php
       require ("scripts/connect_to_mysql.php");
       $query = mysql_query("INSERT INTO `users` SET `username`='{$username}', `password`='{$password}', `firstname`='{$firstname}', `surname`='{$surname}';");
       if(mysql_affected_rows($query) > 0){
         echo "The username : {$username} was added in database!";
       } else {
         echo "Something occurred and we do not insert in database, please contact the administrator.";
       }
    
    ?>
    </body>
    </html>
    Code (markup):
     
    Last edited: Aug 30, 2015
    CristianG., Aug 30, 2015 IP