doumen6605 2015-12-30 15:12
浏览 303
已采纳

从一个查询中选择id并在另一个查询中使用它

I have this code. In the first query I want it to select a pid. Then I want to somehow use the selected pid as WHERE in the second query. This do not work but I want it to work on this(the same) page. I have read about this on other forums but I still didn't fix it. Probably a small mistake somewhere.

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

if(mysqli_connect_errno())
{
  echo mysqli_connect_error();
}

$loggedInUserId = $_SESSION['user_id'];
$resu = mysql_query("SELECT pid FROM users WHERE id='$loggedInUserId';");
$ro = mysql_fetch_row($resu);

$sql= "SELECT pid, project_name, image, image_type FROM project WHERE pid ='". $row["pid"]. "';";

$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_array()) {
    
        //$type= "Content-type:".$row['image_type'];
    //header ($type);
    echo "<form action='respodents.php' method='post'><button name='submit' id='projectbutton'>
            <div> 
                <img src=pic.php?pid=".$row['pid']." width=100px height=100px/>"." <div  id='project_name'>".$row['project_name']."</div>"."
       
            <input type='hidden' name='pid' value='".$row['pid']."'>
            <input type='hidden' name='project_name' value='".$row['project_name']."'>
            
            </div>
       </button></form>";
    }}

mysqli_close($mysqli);

?>

</div>
  • 写回答

5条回答 默认 最新

  • dsff788655567 2015-12-30 15:22
    关注

    With respect to the sql, perhaps this might work

    SELECT `pid`, `project_name`, `image`, `image_type` 
        FROM `project` WHERE `pid` = ( 
            SELECT `pid` FROM `users` WHERE `id`='$loggedInUserId'
        );
    

    The original code had a mix of mysql and mysqli functions with a further mix of Object Orientated and Procedural method calls. Whilst this wouldn't cause an error necessarily it is bad practise to do so. Below is all in a procedural style - it's not tested but it incorporates the two queries into one which should work ( famous last words )

    Sidenote: That said - with mysqli you can take advantage of prepared statements which help mitigate against the threat of sql injection - it's quite straightforward to lean and use - so rather than embedding a variable in the sql you would use a placeholder and then bind a variable to that.

    <?php
        ini_set('display_errors',1);
        error_reporting(E_ALL);
    
        /* db connection? */
    
    
        if( mysqli_connect_errno() ) echo mysqli_connect_error();
        else {
    
            $loggedInUserId = $_SESSION['user_id'];
    
            $sql="select `pid`, `project_name`, `image`, `image_type` 
                from `project` 
                where `pid` = ( 
                    select `pid` from `users` where `id`='$loggedinuserid'
                );";
    
            $resu=mysqli_query( $mysqli, $sql );
            if( $resu ){
    
                $ro = mysqli_fetch_row( $resu );
    
                while( $row=mysqli_fetch_object( $resu ) ){
    
                    echo "<form action='respodents.php' method='post'>
                            <button name='submit' id='projectbutton'><!-- you cannot re-use IDs, they MUST be unique! -->
                                <div> 
                                    <img src'=pic.php?pid=".$row->pid."' width='100px' height='100px'/>
                                    <div id='project_name'>".$row->project_name."</div><!-- you cannot re-use IDs, they MUST be unique! -->
                                    <input type='hidden' name='pid' value='".$row->pid."'/>
                                    <input type='hidden' name='project_name' value='".$row->project_name."'/>
                                </div>
                            </button>
                       </form>";
    
                }
            }
            mysqli_close( $mysqli );
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)