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条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本