my connection in db is correct but when inserting the line of
if ($result == true) {
echo "data inserted";
} else {
echo "data not inserted";
}
dosnt work and not showed its inserted or not actually when i check the table of DB its not inserted
this is code
<?php include('partials/footer.php'); ?>
<?php
// take value from server save it in DB
if (isset($_POST['submit'])) {
// get data from form
$fullname = $_POST['full_name'];
$username = $_POST['username'];
$password = md5($_POST['password']);
// sql statment for isert
$sql = "INSERT INTO `tbl_admin`;
full_name='$fullname',
username='$username',
password='$password'
";
// executin query and save the data in DB
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if ($result == true) {
echo "data inserted";
} else {
echo "data not inserted";
}
}
?>
this is the php file in include
<?php
// create a reuse data
define('LOCALHOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'food-ordder');
$conn = mysqli_connect(LOCALHOST, DB_USERNAME, DB_PASSWORD, DB_NAME, 3306) or die("error" . mysqli_error($conn));
?>
i tried to insert a data to the tbl_admin in my Db but it no works
CodePudding user response:
Actually, you are not inserting anything.
Your SQL is wrong, you end the transaction with ';'
It would be more like this :
$sql = "INSERT INTO `tbl_admin` VALUES('$fullname', '$username', '$password');";
CodePudding user response:
To insert data into a database using PHP, you can follow these steps:
Connect to the database: First, you will need to establish a connection to the database using the PHP function mysqli_connect(). This function takes four arguments: the server name, username, password, and the database name.
Prepare the SQL query: Next, you will need to prepare the SQL query that you want to use to insert data into the database. This query should include the name of the table that you want to insert the data into, as well as the names and values of the fields you want to insert.
Bind the values: If you are using a prepared statement, you will need to bind the values of the fields you are inserting to the corresponding placeholders in the SQL query using the mysqli_stmt_bind_param()
function.
Execute the query: Once the values have been bound, you can execute the query using the mysqli_stmt_execute()
function.
Close the connection: Finally, you will need to close the database connection using the mysqli_close()
function.
Here is an example of how you might insert data into a database using PHP:
<?php
// Connect to the database
$conn = mysqli_connect('server_name', 'username', 'password', 'database_name');
// Check the connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Prepare the SQL query
$stmt = mysqli_prepare($conn, "INSERT INTO table_name (field1, field2, field3) VALUES (?, ?, ?)");
// Bind the values
mysqli_stmt_bind_param($stmt, 'sss', $value1, $value2, $value3);
// Execute the query