donglong1465
2016-11-15 06:04
浏览 38
已采纳

将值插入mysql数据库并检索它们以插入另一个表

I'm trying to insert values to mysql database table called pointofcontact and then retrieve the primary key called pocid to insert to another table called students.

Somehow my code always return the pocid to be 0 and i have no idea why. Gladly to get some help. Any help would be greatly appreciated! Here is my code:

$query="insert into pointofcontact(Username,Password,FirstName,LastName,ContactNumber,EmailAddress,Address,Gender,Status,BackupContactNumber,ProfilePic) values ('$username','$password','$firstname','$lastname','$mobilenumber','$email','$address','$gender','Normal','$backup','$attch')";
    if($con->query($query) === TRUE)
    {
        $query2="select POCID from pointofcontact where username= '$username'";
        $result2=$con->query($query2);
        if($result2 ->num_rows > 0)
        {
            while($row2 = $result2->fetch_assoc())
            {
                $pocid = $row2['POCID'];
                $query3= "insert into student(StudentFirstName, StudentLastName, Allergies, NRIC, POCID) values ('$cfirstname','$clastname','$callergies','$cnric','$POCID')";
            }
            if($con->query($query3) === TRUE)
            {

            }
            else
            {

            }
        }
    }
    else
    {
        echo "error";
    }

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将值插入名为pointofcontact的mysql数据库表中,然后检索名为pocid的主键以插入到 另一张叫做学生的桌子

不知怎的,我的代码总是将pocid返回为0,我不明白为什么。 很高兴得到一些帮助。 任何帮助将不胜感激! 这是我的代码:

  $ query =“insert into pointofcontact(Username,Password,FirstName,LastName,ContactNumber,EmailAddress,Address,Gender,Status,BackupContactNumber,ProfilePic)值 ( '$用户名', '$密码', '$姓名', '$姓氏', '$移动电话号码', '$电子邮件', '$地址', '$性别', '普通', '$备份',  '$ attch')“; 
 if($ con> query($ query)=== TRUE)
 {
 $ query2 =”从pointofcontact中选择POCID,其中username ='$ username'“; 
  $ result2 = $ con>查询($ query2); 
 if($ result2  - > num_rows> 0)
 {
 while($ row2 = $ result2-> fetch_assoc())
 {  
 $ pocid = $ row2 ['POCID']; 
 $ query3 =“插入学生(StudentFirstName,StudentLastName,Allergies,NRIC,POCID)值('$ cfirstname','$ clastname','$ callergies',  '$ cnric','$ POCID')“; 
} 
 if($ con> query($ query3)=== TRUE)
 {
 
} 
其他
 
 
 
  
} 
} 
  } 
其他
 {
 echo“error”; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duan7007 2016-11-15 12:30
    已采纳

    I haven't tested this, but it should do what you need:

    // assuming proper validation and escaping is completed...
    
    if($con->query("INSERT INTO pointofcontact (Username,Password,FirstName,LastName,ContactNumber,EmailAddress,Address,Gender,Status,BackupContactNumber,ProfilePic) VALUES ('$username','$password','$firstname','$lastname','$mobilenumber','$email','$address','$gender','Normal','$backup','$attch');"){
        $POCID=$con->insert_id;
        if($con->query("INSERT INTO student (StudentFirstName, StudentLastName, Allergies, NRIC, POCID) VALUES ('$cfirstname','$clastname','$callergies','$cnric','$POCID');")){
            // Pass
        }else{
            // Fail
        }
    }else{
        // Fail
    }
    
    点赞 打赏 评论
  • duanlie4621 2016-11-15 06:11

    check your database table design for POCID column. When you created that table you must add it as primary key and auto_increment ...

    CREATE TABLE table_name (id int primary key auto_increment,....)
    

    If you already did this then, use mysqli_insert_id() to get the last inserted id.

    点赞 打赏 评论

相关推荐 更多相似问题