douhang8991 2015-03-07 15:44
浏览 90

如何使用mysqli_insert_id()获取自动递增值的值?

I want to get the primary key values of movie and actor table. When i selected values in radio button primary key has been created automatically to the movie and actor table. Now I want to get that primary key value and insert to the movie_actor table. my code

connect.php
<?php

$dbc = mysqli_connect('localhost','root','black98765','db_name')
OR die("Cannot connect to MySQL:" . mysqli_connect_error());


//insert into actor table
$q = "INSERT INTO actor (name)
    VALUES ('$actor')";
//execute the query to get primary key value
$r = mysqli_query($dbc,$q);
//assign to variable below
$actor_no = mysqli_insert_id($dbc);

//insert into movie table   
$q2 = "INSERT INTO movie (movie_name, release_year)
        VALUES ('$movie','$year')";
//execute the query to get primary key value        
$r2 = mysqli_query($dbc,$q2);
//assign to variable below
$movie_no = mysqli_insert_id($dbc);

$q3 = "INSERT INTO movie_actor (movie_no, actor_no, rate)
        VALUES ($movie_no, $actor_no, '$rate')";        
//connect and insert $q 
$r3 = mysqli_query($dbc,$q3);
if($r && $r2 && $r3){
    echo "Inserted Successfully!";
}else{
    echo "Failed to Insert Data!";
    mysqli_error($dbc);
}
mysqli_close($dbc);

?>

form.php

<?php
if(isset($_POST['submit'])){
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $error = array();
        //choose actor
        if(!isset($_POST['actor'])){
            $error[] = "Please choose of the following actors!";
        }else{
            $actor = $_POST['actor'];           
        }
        //choose movie
        if(!isset($_POST['movie'])){
            $error[] = "Please choose of the following movies!";
        }else {
            $movie = $_POST['movie'];               
        }
        //choose release year
        if(!isset($_POST['year'])){
            $error[] = "Please choose of the following release year!!";
        }else{
            $year = $_POST['year'];
        }
        //choose rate
        if(!isset($_POST['rate'])){
            $error[] = "Please choose of the following rate!";
        }else{
            $rate = $_POST['rate'];
        }
        //if no errors
        if(empty($error)){
            require('connect.php');
        }else{
            echo "<p>System Error!</p>";
            foreach($error as $msg){
                echo $msg."<br/>
";
            }
        }       
    }
}

?>

<form action="form.php" method="POST">
<p>Select on the following Selections</p>

<p><label for="actor">Name of Actor:</label>
<input type="radio" name="actor1" value="Jet Li"/>Jet Li
<input type="radio" name="actor2" value="Sylvester Stallone"/>Sylvester Stallone
<input type="radio" name="actor3" value="Jason Statham"/>Jason Statham</p>

<p><label for="movie">Name of Movie:</label>
<input type="radio" name="movie1" value="Expendables 3"/>Expendables 3
<input type="radio" name="movie2" value="Rocky"/>Rocky
<input type="radio" name="movie3" value="Kiss of the Dragon"/>Kiss of the Dragon</p>

<p><label for="movie">Release Year:</label>
<input type="radio" name="year1" value="2014"/>2014
<input type="radio" name="year2" value="1976"/>1976
<input type="radio" name="year3" value="2001"/>2001</p>
<p><input type="submit" name="submit" value="Insert"/></p>
</form>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上