在mysqli_commit之后进行查询($ con)

时间:2016-01-26 09:24:59

标签: php mysql mysqli

我是PHPMySQL的新手,我遇到了以下问题。

我试图将新用户插入数据库。在用户插入后,我必须获取此用户的ID并使用此ID将一些数据插入到我的数据库中的另一个表中。

我试图获取提到的ID但是当我执行mysqli_fetch_row(result_parameter)时,我得到一个错误"期望参数1是资源,布尔给定" 。知道我该如何解决这个问题?

插入函数mysqli_commit($ con)后,我可以执行另一个查询吗?还是有更好的方法来制作它?

if(isset($_POST['regjistrohu']))
{
    $emri = $_POST['emri'];
    $mbiemri = $_POST['mbiemri'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $gjinia = $_POST['gjinia'];
    $mosha = $_POST['mosha'];
    $adresa = $_POST['adresa'];
    $numer = $_POST['numer'];
    $data = date("Y-m-d");
    $sql = "INSERT INTO user (U_Email,U_Password,Type) VALUES ('$email','$password',3) ; INSERT INTO pacient (P_Emer,P_Mbiemer,P_Email,P_Password,P_Gjinia,P_Mosha,P_Adresa,P_Cel) VALUES ('$emri','$mbiemri','$email','$password','$gjinia','$mosha','$adresa','$numer')";
    $res = mysqli_multi_query($conn,$sql);
    if($res == true)
    {
        echo "pacienti u shtua";
    }
    else
    {
        echo "pacienti nuk u shtua" .mysql_error();
    }   
}
$sel = "SELECT P_Id FROM pacient WHERE P_Email = '$email'";
$zgjidh = mysqli_query($conn,$sel);
while ($row = mysqli_fetch_row($zgjidh))
{
    echo $row[0];
}

2 个答案:

答案 0 :(得分:0)

使用此代码:

$sel = 'SELECT P_Id FROM pacient WHERE P_Email = "'.$email.'" ';
$zgjidh = mysqli_query($conn,$sel);
while ($row = mysqli_fetch_assoc($zgjidh)){
    $id= $row['P_Id']
}
echo $id;

这将返回id属于$ email的id(插入的电子邮件地址)!

答案 1 :(得分:0)

此代码容易受到sql注入,所以请考虑在mysqli中使用预处理语句!它看起来应该可以工作,稍微重新安排并且使用select稍微不同的语法(需要$email作为参数,所以应该在IF/END IF控件

<?php
    if( isset( $_POST['regjistrohu'] ) ){

        $emri = $_POST['emri'];
        $mbiemri = $_POST['mbiemri'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $gjinia = $_POST['gjinia'];
        $mosha = $_POST['mosha'];
        $adresa = $_POST['adresa'];
        $numer = $_POST['numer'];
        $data = date("Y-m-d");


        $sql = "INSERT INTO `user` ( `U_Email`, `U_Password`, `Type` ) VALUES ( '$email', '$password', 3 );

                INSERT INTO `pacient` ( `P_Emer`, `P_Mbiemer`, `P_Email`, `P_Password`, `P_Gjinia`, `P_Mosha`, `P_Adresa`, `P_Cel` ) 
                    VALUES 
                ( '$emri', '$mbiemri', '$email', '$password', '$gjinia', '$mosha', '$adresa', '$numer' );";

        $res = mysqli_multi_query( $conn, $sql );

        if( $res == true ) {
            echo "pacienti u shtua";


            $sql = "SELECT `P_Id` FROM `pacient` WHERE `P_Email` = '$email';";
            $res = mysqli_query( $conn, $sql );

            while ( $row = mysqli_fetch_object( $res ) ) {
                echo $row->P_Id;
            }

        } else {
            echo "pacienti nuk u shtua" .mysql_error();
        }

        mysqli_close( $conn );
    }
?>