skip to Main Content

I am new to PHP. My input forms doesn’t save any of it in my MySQL database but everytime I press my submit button it shows “register=success”,
even though its not inserted in my database and its not showing any errors in any line.

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>

        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    </head>

    <body>
        <div class="container">
            <form id="BG" class="col-lg-8" action="includes/register.php" method="POST">
                <div class="form-group">
                    <label for="formGroupExampleInput">Firstname</label>
                    <input type="text" class="form-control col-lg-6" id="formGroupExampleInput" placeholder="Firstname" name="fName">
                </div>
                <div class="form-group">
                    <label for="formGroupExampleInput2">Lastname</label>
                    <input type="text" class="form-control col-lg-6" id="formGroupExampleInput2" placeholder="Lastname" name="lName">
                </div>
                <div class="form-group">
                    <label for="formGroupExampleInput">Plate #</label>
                    <input type="text" class="form-control col-lg-6" id="formGroupExampleInput" placeholder="Plate #" name="plateNo">
                </div>
                <div class="form-group">
                    <label for="formGroupExampleInput2">Vehicle brand</label>
                    <input type="text" class="form-control col-lg-6" id="formGroupExampleInput2" placeholder="Vehicle brand" name="vBrand">
                </div>
                <div class="form-group">
                    <label for="formGroupExampleInput">color</label>
                    <input type="text" class="form-control col-lg-6" id="formGroupExampleInput" placeholder="color" name="color">
                </div>
                <button type="submit" name="submit" class="btn btn-primary">Register</button>

            </form>

        </div>
    </body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</html>

conn.php

<?php
    $dbSname='localhost';
    $dbUname ='root';
    $pwd='';
    $dbName = 'capstone';

    $conn = mysqli_connect($dbSname,$dbUname,$pwd,$dbName);

register.php

<?php
    include_once 'includes/conn.php';
    $fname=$_POST['fName'];
    $lName=$_POST['lName'];
    $plateNo=$_POST['plateNo'];
    $vBrand=$_POST['vBrand'];
    $color=$_POST['color'];

    $sql = "INSERT INTO register(name,lastname,Plateno,vehiclebrand,color)
            VALUES($fname,$lName,$plateNo,$vBrand,$color);";

    mysqli_query($conn,$sql);

    header("Location: ../index.php?register=success");

?>

I was expecting my input to save data to my MySQL database.

2

Answers


  1. First you need to know if an error is happening when you run your query. So, my recomendation is to change your .php script to handle exceptions, like this:

    <?php
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
        try {
            //start of your code
            $host = "localhost";
            $user = "root";
            $pass = "";
            $db = "capstone";        
            $conn = new mysqli($host,$user,$pass,$db);        
            $sql = "INSERT INTO register(name,lastname,Plateno,vehiclebrand,color)
                                VALUES($fname,$lName,$plateNo,$vBrand,$color);";
    
            $conn->query($sql);
            //end of your code
        }
        catch(mysqli_sql_exception | Exception $e){ 
            $error="Error #".$e->getCode()." ".$e->getMessage().PHP_EOL;
    
            if(isset($conn) && get_class($e) == "mysqli_sql_exception")
                $error.="SQLSTATE #".$conn->sqlstate." SQL: $sql".PHP_EOL;
    
            $error.=$e->getTraceAsString();
            echo(nl2br($error));    
        }
        finally {
            if(isset($result) && $result instanceof mysql_result)
                $result->free();
    
            if(isset($conn) && is_resource($conn) && get_resource_type($conn) === 'mysql link')
                $conn->close();
       }
    ?>
    

    Once you have an error, you can edit your question and share it.

    After you can solve your problem, I would recommend to take a look to this question: How can I prevent SQL injection in PHP?, because is important to learn early how to avoid SQL injection, a concerning security problem.

    Login or Signup to reply.
  2. if (isset($_POST[‘submit’])) {

    $fname=$_POST['fName'];
    $lName=$_POST['lName'];
    $plateNo=$_POST['plateNo'];
    $vBrand=$_POST['vBrand'];
    $color=$_POST['color'];
    

    }

    I think you should have a if checking if the button isset. If this does not answer your question, pls send the database info So I can check it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search