doujian1954 2016-07-19 09:59
浏览 81

使用PHP在SQL数据库中显示一行

I have a db called 'members' and a table called 'users'. I want an id to be taken when a user signs up and then that id used to display just that one users details from the table rather than all rows in the table. This is my sign up code:

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("members", $con);

    $sql="INSERT INTO users (name, email, address, dob, password)
    VALUES('$_POST[name]','$_POST[email]','$_POST[address]','$_POST[dob]','$_POST[mypassword]')";

    if (!mysql_query($sql,$con))
    {
      die('Error: ' . mysql_error());
    }

    {
      header ("Location: sign_in.php");
      exit;
    }

    mysql_close($con)
    ?>

And this is my display table code:

    <?php

    // Create connection
    $conn = new mysqli("localhost", "root", "", "members");
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT name, email, address, dob FROM users";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table><tr><th>Name</th><th>Email</th><th>Address</th>       <th>DOB</th></tr> id-";
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>".$row["name"]."</td><td>".$row["email"]."</td><td>".$row["address"]."</td><td>".$row["dob"]."</td></tr>";
        }
        echo "</table>";
    } else {
            echo "<p>No Details</p><style>p{color: #336699; font-size: 20pt; font-family: Gulim; font-weight: bold; align: center; padding: 10px; position: absolute; top: 80; left: 560; z-index: 2; border-spacing: 0px;}</style> ";
    }
    $conn->close();
    ?>
  • 写回答

2条回答 默认 最新

  • dongwang6837 2016-07-19 10:06
    关注

    is there any column name like ID in 'users' table? if not then create a column with name 'ID' at first of all columns and set to auto increment.

    then run loop in your query and get id

    评论

报告相同问题?

悬赏问题

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