<?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 "
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("issds", $id, $name, $class,$mark,$gender);
if($stmt->execute()){
echo "
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("ssds", $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 ";
}
$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;
}
MYSQLI Functionsmysqli_num_rows() Number of rows in result set
SELECT query
UPDATE query