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

时间:2014-09-02 03:33:36

标签: php mysql mysqli

美好的一天,这只是来自Referrence 1的关于stackoverflow的后续问题。虽然现在部分解决了,但我仍然遇到了开斋节的问题。通过mysqli进行多次插入有点工作(我只是说了一下,因为只有4个我们的6个表插入了值),但当我检查eid的外键是否也被{{ 1}}我很惊讶地看到外键mysqli_insert_id()得到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.*

如您所知,我在插入语句again please see the Referrence 1期间使用了mysqli_insert_id(),幸运的是它只能用于6个实际表中的4个。当我检查eid列时,它显示0值。

这是我的select语句(不是真正的语句):

eid

为了让事情变得非常奇怪,让我告诉你,当我尝试使用mysqli_fetched_assoc()显示值时,查询似乎不像我的索引页面那样显示其他内容。虽然db有它的记录。但是,当我使用mysql [虽然许多人声称它被弃用]它似乎工作正常,尽管我有mysqli用于插入语句。你们明白我的意思吗?我使用mysqli for insert语句和mysql进行显示,当然除了` <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>` 之外它工作正常。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

$sql=mysql_query

永远不会与

一起使用
mysqli_insert_id
     ^

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