-2

I created a form to insert data into a table that created in the mysql(phpmyadmin) in localhost. when I filled the form and click the save button, the data were not inserted to the table. But, row ID incremented.It did not show any error message also. According to the coded, shows the message data inserted also successful.

use phpMyAdmin 4.8.4, MySql,PHP version 5.6.40. I tried in localhost.

Database Name : procurementtest; table Name: budgettypes; column Names : ID, budgetDescription, shortDescription, budgetCode

This is the code for database connection, form "insert.php" and "budgetTypes.php" respectively.

$con = mysqli_connect("localhost","root","","procurementtest");
if(!$con){
    echo 'Not connected to server';
}

if(!mysqli_select_db($con,'procurementtest')){
    echo 'Database Not Selected';
}

$BudgDes = isset($_POST['Budget Description']);
$ShorDes = isset($_POST['Short Description']);
$BudCode = isset($_POST['Budget Code']);

$sql = "INSERT INTO budgettypes (budgetDescription, shortDescription, budgetCode) VALUES ('$BudgDes', '$ShorDes' , '$BudCode'); ";

if (!mysqli_query ($con, $sql)){
    echo 'Not Inserted';
}else{
    echo 'Inserted';
}

    <div class="container">

    <h1>Budget Types</h1>

    <div class="pill-nav">
       <!-- <a class="active" href="#home">Home</a>-->
        <a href="#new">New</a>
        <a href="#Edit">Edit</a>
        <a href="#Delete">Delete</a>
    </div>


                <form action="insert.php"  method="POST">
                    <div class="form-group">
                    <label>Budget Description</label>
                    <input type="text" name="Budget Description" class="form-control" >
                    </div>

                    <div class="form-group">
                    <label>Short Description</label>
                    <input type="text" name="Short Description" class="form-control" >
                    </div>

                    <div class="form-group">
                    <label>Budget Code</label>
                    <input type="text" name="Budget Code" class="form-control" >
                    </div>

                    <div class="form-group">
                    <button type="submit" class="btn btn-default" id="Save" name="Save"> Save</button>
                    </div>
                </form>

                </a>
            </li>       
    </div>
</body>

This the message shows after insert data:

"Inserted"

This is the output in the table. I am a new learner. I referred youtube video to create this "insert.php" file. Thank You for any help.

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
munchi
  • 1
  • 1

1 Answers1

-2

Actually you're checking opposite condition please correct it by this

if (mysqli_query($conn, $sql)) {
     echo 'Inserted';
} else {
     echo 'Not Inserted';
}

and remember to close connection always afetr any db opreatio :)

mysqli_close($conn);
DaFois
  • 2,121
  • 8
  • 21
  • 35
Umair Latif
  • 388
  • 2
  • 6