[Solved] PHP insert into not inserting any data

Introduction

If you are having trouble getting your PHP insert into statement to work, you are not alone. Many developers have encountered this issue and have had difficulty finding a solution. This article will provide an overview of the problem and offer some tips and tricks to help you get your PHP insert into statement working properly. We will also discuss some common mistakes that can lead to this issue and provide some best practices for avoiding them. Finally, we will provide some example code to help you get started.

Solution


$sql = "INSERT INTO tasks (taskName, requestedBy, details, dateAdded) VALUES ('$taskname' ,'$requestedby' ,'$details', '$datenow')";

// Removed quotes from columns, and added missing quote on datenow

Please note, this technique for adding values into the database is very insecure, and is prone to SQL injection attacks.

5

solved PHP insert into not inserting any data


<?php

// Connect to the database
$db = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Insert data into the database
$sql = "INSERT INTO table_name (column1, column2, column3)
VALUES ('value1', 'value2', 'value3')";

if (mysqli_query($db, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "
" . mysqli_error($db); } // Close connection mysqli_close($db); ?>

If you are trying to insert data into a database using PHP, but it is not working, there are a few things you can check to troubleshoot the issue. The first thing to do is to make sure that you have established a connection to the database. This can be done using the mysqli_connect() function. Once you have established a connection, you can use the mysqli_query() function to execute an INSERT query.

When writing an INSERT query, it is important to make sure that the column names and values match up correctly. If the column names and values do not match, the query will not work. Additionally, make sure that the data types of the values match the data types of the columns. For example, if a column is an integer, the value should be an integer.

If the query is still not working, you can use the mysqli_error() function to get more information about the error. This will help you to identify the issue and fix it.

Once you have fixed the issue, you can use the mysqli_close() function to close the connection to the database. This will ensure that the connection is not left open and that the data is properly inserted into the database.