php - 通过 MySQLi 插入的值无法用 mysqli_insert_id 完全插入

标签 php mysql mysqli

美好的一天,这只是 Referrence 1 的后续问题在 stackoverflow 上。虽然它现在正在部分解决,但我仍然对开斋节有疑问。通过 mysqli 的多次插入有点工作(我只是说了一点,因为我们的 6 个表中只有 4 个插入了值),但是当我检查 eid 的外键是否也被复制时通过 mysqli_insert_id() 我很惊讶地看到外键 eid 有 0 个值。

是这样的(注意:ff: 数据不是实际数据):

Table: Employee [eid is it's primary key]

eid    employee_name     address     etc
1002   employee A        Beside U     ♥
1003   employee B        Pluto        ♥

Table: Contact [eid is it's foreign key]

eid    telno         email
0      911       kingCorbra@hey.com
0      ***       universe@hey.com


Table: Work Experience [eid is it's foreign key]

eid    Company Name            start date   end date
0      Windows Macintosh       2012-12-01   2012-12-02 
0      Micro Entertainment LG  2012-12-31   2013-01-01

*Other Tables are not included in the samples but part of the query.*

As you know, I have used mysqli_insert_id() during the insert statement again please see the Referrence 1, and luckily it work but only for 4 out of 6 actual tables. And when I checked the eid column it shows 0 values.

Here is my select statement (not the real one):

` <table cellspacing="0" width="100%">
<thead>
<tr>
<th><strong>Name:</strong></th>
<th><strong>Date of Birth:</strong></th>
<th><strong>Birthplace:</strong></th>
<th><strong>Gender:</strong></th>
<th><strong>Email Add:</strong></th>
<th><strong>Contact#:</strong></th>
<th><strong>Address:</strong></th>
</tr>
</thead>
    <?php
include('db.php');
    $sql=mysqli_query("SELECT emp.eid,emp.fname,
emp.mname,emp.lname,
emp.age,emp.gender,
emp.birthday,emp.birthplace,
emp.citizenship,emp.status,
emp.sss,emp.philhealth,
emp.tin,emp.height,
emp.weight,
con.address,
con.province,con.postcode,
con.telno,con.mobile,
con.email,con.alternate,
educ.elem,
educ.egrad,educ.high,
educ.hgrad,educ.college,
educ.cgrad,
ems.position,ems.hireDate,ems.job_desc,ems.basic,ems.salary,
w.company_name,w.position,w.desc,w.startDate,w.endDate,
fam.fatherName,fam.motherName,fam.sibling,fam.spouse,fam.children

 FROM employee AS emp INNER JOIN contact AS con ON con.eid='emp.eid' 

 INNER JOIN educ AS  educ ON educ.eid='emp.eid' 
 INNER JOIN employment AS  ems ON ems.eid='emp.eid'
 INNER JOIN work AS w ON w.eid='emp.eid'
 INNER JOIN family AS fam ON fam.eid='emp.eid' WHERE emp.eid='$id'");
    $counter=0;
    while($row=mysqli_fetch_assoc($sql))
        {
        $cname  =   $row['name'];
        $cbday  =   $row['birthday'];
        $pob    =   $row['pob'];
        $cgen   =   $row['gender'];
        $email  =   $row['email'];
        $contact=   $row['contact'];
        $add    =   $row['address'];

            if($counter%2)
            {
            ?>
            <tbody>
            <?php } else { ?>
            <tr>
            <?php } ?>
            <td><?php echo $cname; ?></td>
            <td><?php echo $cbday; ?></td>
            <td><?php echo $pob; ?></td>
            <td><?php echo $cgen; ?></td>
            <td><?php echo $email; ?></td>
            <td><?php echo $contract; ?></td>
            <td><?php echo $add; ?></td>
            </tr>
            </tbody>
        <?php
        $counter++;
        }//while-end
        ?>

</table>`

让事情变得更奇怪,让我告诉你,当我尝试使用 mysqli_fetched_assoc() 显示值时,查询似乎不起作用,因为在我的索引页面上没有显示任何其他内容。虽然数据库有它的记录。但是当我使用 mysql 时 [尽管许多人声称它已被弃用] 它似乎工作正常,尽管我将 mysqli 用于插入语句。你们明白我的意思吗?我将 mysqli 用于插入语句,将 mysql 用于显示,当然除了 eid 之外它工作正常。

有什么想法吗?

最佳答案

$sql=mysql_query

永远不会与

mysqli_insert_id
     ^

您需要为这两个调用使用相同的 API。始终使用 MySQLi

关于php - 通过 MySQLi 插入的值无法用 mysqli_insert_id 完全插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615428/

相关文章:

php - 如何将数组值添加到数组的中间?

php - knpMenuBundle问题

php - 在php中插入计算代码后出错

mysql:两个表但三个选择条件

mysql - rails : how do I test a helper method that executes a mysql query

PHP 和 MySQL 使用相同的查询显示不同的结果

php - 无法在 <item> 标记的 rss feed 中添加链接或图像

mysql - 尝试通过链接服务器更新时出错(mssql -> mysql) - 选择有效

php - 升级到 MySQLi - 就像将 mysql 换成 mysqli 一样简单?

php - MySQLi 的更新语句错误