<?Php
require "config.php";// Database connection
$id=50;
$name = 'my_name';
$class='Three';
$mark=70;
$gender='male';
$query="INSERT INTO student (id,name,class,mark,gender) values($id,'$name','$class','$mark','$gender')";
if($connection->query($query)){
echo "<br>No of records inserted : ".$connection->affected_rows;
}else{
echo $connection->error;
}
?>
We will get error message if the query fails to execute.
MySQLI database connection file
With Parameters by using bind_param()<?Php
require "config.php";// Database connection
//////////////////////////////
$id=50;
$name = 'my_name';
$class='Three';
$mark=70;
$gender='male';
$query="INSERT INTO student (id,name,class,mark,gender) values(?, ?,?,?,?)";
$stmt=$connection->prepare($query);
if($stmt){
$stmt->bind_param("issis", $id, $name, $class,$mark,$gender);
if($stmt->execute()){
echo "<br>No of records inserted : ".$connection->affected_rows;
}else{
echo $connection->error;
}
}else{
echo $connection->error;
}
?>
We can change id field to auto increment type. Now we need not enter value for id. We will get the value for id after the record is added by using insert_id.
<?Php
require "config.php";// Database connection
//////////////////////////////
$name = 'my_name';
$class='Three';
$mark=70;
$gender='male';
$query="INSERT INTO student (name,class,mark,gender) values( ?,?,?,?)";
$stmt=$connection->prepare($query);
if($stmt){
$stmt->bind_param("ssis", $name, $class,$mark,$gender);
if($stmt->execute()){
echo "<br>No of records inserted : ".$connection->affected_rows;
echo "<br>Insert ID : ".$connection->insert_id;
}else{
echo $connection->error;
}
}else{
echo $connection->error;
}
?>
if($connection->query("insert into table_name (field_name) values('$field_value')")){
echo " Records added : ".$connection->affected_rows;
}else{
echo " Records not added ";
}
<?php
require "config.php"; // Database connection having $connection
try {
// Variables to be inserted
$name = "John Doe";
$class = "Three";
$marks = 85;
$gender = "Male";
// Prepare the SQL INSERT query
$query = "INSERT INTO student (name, class, mark, gender) VALUES (?, ?, ?, ?)";
// Prepare the statement
$stmt = $connection->prepare($query);
if($stmt){
// Bind parameters (s = string, i = integer)
$stmt->bind_param("ssis", $name, $class, $marks, $gender);
// Execute the statement
if ($stmt->execute()) {
echo "Record inserted successfully!";
// Output affected rows
echo "<br>No of records inserted: " . $connection->affected_rows;
// Output the ID of the inserted record
echo "<br>Insert ID: " . $connection->insert_id;
} else {
echo "Error: " . $stmt->error;
}
}
// Close the statement
$stmt->close();
} catch (mysqli_sql_exception $e) {
// Handle any errors
echo "Error: " . $e->getMessage();
}
// Close the connection
$connection->close();
?>
try {
// Variables to be inserted
$name = "John Doe";
$class = "Three";
$marks = 85;
$gender = "Male";
// Prepare the SQL INSERT query
$query = "INSERT INTO student (name, class, mark, gender) VALUES (?, ?, ?, ?)";
// Prepare the statement
$stmt = mysqli_prepare($connection, $query);
if ($stmt) {
// Bind parameters (s = string, i = integer)
mysqli_stmt_bind_param($stmt, "ssis", $name, $class, $marks, $gender);
// Execute the statement
if (mysqli_stmt_execute($stmt)) {
echo "Record inserted successfully!";
// Output affected rows
echo "<br>No of records inserted: " . mysqli_affected_rows($connection);
// Output the ID of the inserted record
echo "<br>Insert ID: " . mysqli_insert_id($connection);
} else {
echo "Error: " . mysqli_stmt_error($stmt);
}
// Close the statement
mysqli_stmt_close($stmt);
}
} catch (Exception $e) {
// Handle any errors
echo "Error: " . $e->getMessage();
}
$date = new DateTime($dt);
$dt=$date->format('Y-m-d');
Here $dt is the variable storing date field value.
$stmt->bind_param('s', $dt);
if(strlen($dt) >2){
$date = new DateTime($dt_approved);
$dt=$date->format('Y-m-d');
}else{
$dt=null;
}