PHP Tutorials
- What is MySqli
- mysql query
- mysql query example
- InnoDB
- mysql column Type
- CRUD Example
- Connection Using Function
- mysql keys
- SELECT
- WHERE
- UPDATE
- Count no of Rows
- ALIAS
- AND, AND & OR
- BETWEEN
- COMPARISON OPERATOR
- DELETE
- DELETE LIMIT
- DISTINCT
- EXISTS
- FROM
- GROUP BY
- HAVING
- IN
- INTERSECT
- IS NULL & IS NOT NULL
- LIKE
- NOT
- ORDER BY
- SELECT LIMIT
- SUBQUERY
- TRUNCATE
- UNION && UNION ALL
- Concat & Group_Concat
- mysql Function
- Mysql Insert Id
- MySql Aggregate Function
- Mysql Join
- JOIN in MySql
- Trigger
- Procedure
- Transaction
- views
- Index
- SQL Injection
- Normalization
- Query Bind
- Interview Questions
Important Link
Mysql Insert Id :-
mysql_insert_id() :-
mysqli_insert_id() function returns the id generated by a your query on a table with a column having the AUTO_INCREMENT attribute.
Note :Performing an INSERT or UPDATE statement using the LAST_INSERT_ID() function will also modify the value returned by the mysqli_insert_id function.
File Name:- index.php
<html><head>
<title></title>
</head>
<body>
<form action="pro_insert.php" name="" method="post">
<label>Product Name :</label><input type="text" name="name" /><br>
<label>Product size :</label><input type="text" name="size" /><br>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
File Name:- pro_insert.php
<?phpinclude 'db.php';
$pname = $_POST['name'];
$qry = "insert into products (pro_name) values('$pname')";
$result = mysqli_query($con, $qry);
$userid = mysqli_insert_id($con); // it auto generate the pro_id which you want to insert into product size table as a froegin key.
//echo "user id = ".$userid;
$prosize = $_POST['size'];
$qry1 = "insert into product_size (pro_size, user_id) values('$prosize','$userid')";
echo $qry1;
$result1 = mysqli_query($con, $qry1) or die(mysqli_error());
if($result1)
{
echo "successful";
}
else {
echo "error";
}
?>
File Name:- db.php
<?php$host = "localhost";
$user = "root";
$pass = "";
$db_name = "foreign_key_table";
$con = mysqli_connect($host, $user, $pass, $db_name);
mysqli_select_db($con, $db_name);
?>