【问题标题】:How do I get id for a certain data of mysql in php?如何在php中获取mysql的某些数据的id?
【发布时间】:2020-04-08 14:50:57
【问题描述】:

我想在 php 中获取数据的 id 以在我的数据库中更新它。我尝试了很多我在网上找到的东西,但无法解决问题。这是所有的编辑代码。

这是显示帖子的地方,但我不知道如何获取每个帖子的 id,以便人们可以在 mysql 上编辑/可以编辑

<?php

$link = mysqli_connect ('localhost', 'root', '', '');

mysqli_select_db ($link, 'CodersHome');

?>

<?php
session_start ();
if( !isset($_SESSION['username']))
header('location:../login.php');
 ?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>CodersHome Agenda</title>
    <link rel="shortcut icon" type="image/png" href="../tabimg.png">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="profile/profile.css">
    <link rel="stylesheet" type="text/css" href="../navbar/navbar.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendap.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendacard.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.5/lib/darkmode-js.min.js"></script>
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <script src="agendatabs.js" charset="utf-8"></script>
  </head>
<?php include '../navbar/navbar.html' ?>
  <body>


    <div id="page-container">
      <div id="content-wrap">

        <ul class="tab">
          <li><a href="#" class="tablinks active" onclick="openCity(event, 'agenda')"><strong>Agenda</strong></a></li>
          <li><a href="#" class="tablinks" onclick="openCity(event, 'createagenda')"><strong>Crea Agenda</strong></a></li>
        </ul>
<br>
    <div id="agenda" class="tabcontent">
      <div class="welcome">
        <h1 class="weltitle">Agenda 1er D</h1>
     </div>
     <br><hr><br>
      <?php
      $result = mysqli_query($link,"SELECT * FROM agenda");

      while($row = mysqli_fetch_array($result))  {

      //echo $row["title"];
      //echo $row["description"];
      //echo $row["datef"];


      ?>

      <?php $i=0; ?>
      <div class="row">
      <div class="column">
      <div class="card">
    <?php echo $row["description"]; ?>
    <?php echo $row["datef"]; ?>  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <?php echo $row["inserter"]; ?> <?php if($_SESSION['username'] == 'admin') { include('eddel.php');

    } ?> <br>
    </div>
    </div>
    </div>
      <br>
      <?php
      $i++;
      }
      ?>

    </div>

在这里你可以看到我尝试更新的地方。

<?php

session_start ();

$link = mysqli_connect ('localhost', 'root', '', '');

mysqli_select_db ($link, '');


if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}

$title = $_POST ['title'];
$desc = $_POST ['editor1'];
$date1 = $_POST ['date1'];
$inserter = $_SESSION['username'];

$result = mysqli_query($link,"SELECT * FROM agenda");

while($row = mysqli_fetch_array($result))  {

//echo $row["title"];
//echo $row["description"];
//echo $row["datef"];
$i=0;
}
$sql = "UPDATE agenda SET title = $title, description = $desc, datef = $date1, inserter = $inserter WHERE id = $row[id]; ";
  $i++;
if(mysqli_query($link, $sql)){
header("Refresh:0; url=agenda.php");
}

?>

<?php
if( !isset($_SESSION['username']))
header('location:../login.php');
?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
   <meta charset="utf-8">
   <title>CodersHome Agenda</title>
   <link rel="shortcut icon" type="image/png" href="../tabimg.png">
   <meta name="viewport" content="width=device-width,initial-scale=1">
   <link rel="stylesheet" href="profile/profile.css">
   <link rel="stylesheet" type="text/css" href="../navbar/navbar.css">
   <link rel="stylesheet" type="text/css" href="agendacss/agendap.css">
   <link rel="stylesheet" type="text/css" href="agendacss/agendacard.css">
   <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
   <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
   <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
   <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.5/lib/darkmode-js.min.js"></script>
   <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
   <script src="agendatabs.js" charset="utf-8"></script>
 </head>
<?php include '../navbar/navbar.html' ?>
 <body>


<div class="welcome2">
   <p class="weltitle">Edita Una Agenda</p>
</div>
<br><hr><br>
           <form class="agfrm" action="agendaedit.php" method="post">

             <div class="form-group">

               <label>Title</label> <br> <br>
               <input type="text" name="title" class="form-control" required><br> <br>

             </div>

             <div class="form-group">

               <label>Description</label> <br> <br>

               <textarea name="editor1" required></textarea>
                    <script>
                            CKEDITOR.replace( 'editor1' );
                    </script> <br> <br>
             </div>

           <label>Date</label> <br> <br>
           <input type="date" id="date1" name="date1"
                  value="2020-02-25"
                  min="2020-01-01" max="2021-12-31" required> <br>
                  <br>
       <button type="submit" class="homepbtn1">Sumbit</button>


       </form>

   </div>


     </div>


 </body>

</html>

<?php include '../Footer/index.html'?>


我想更新表中的数据。我也为每一行分配了 id,但我不知道如何获取 id 来编辑表格的确切行。

这里还有一个现场演示,展示了帖子的创建和显示位置。

function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}
body {
background-color: white;
  height: 96vh;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}


.welcome {
  border: 2px solid black;
  color: #7d3caf;
  margin-top: 40px;
  margin-left: 40px;
  margin-right: 40px;
  border-radius: 40px;
  transition: 0.5s ease;
  font-weight: 5em;
  font-family: 'Century Gothic', sans-serif;
  background-color: #f1f1f1;
}

.welcome:hover
{
  border: 1px solid #6747c7 ;
  background-color: #6747c7;
  color: white;
  font-weight: bold;
  font-family: 'Century Gothic', sans-serif;

   }


.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 30px;
  margin-bottom: 30px;
}
 .weldis {
   font-family: 'Century Gothic', sans-serif;
   text-align: center;
 }
.gtac {
  text-align: center;
  font-family: monospace;
}
.gtacl {
  text-align: center;
}

.gtac1:hover {
  color: black;
  background-color: black;
  border:  2px solid black;
}

.lbtn {
  font-size: 20px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  border: 1px solid #fff;
  transition: 0.6s ease
}

.lbtn:hover{
  background-color: #7a7979;
  color: white;
  font-size: 24px;
}


.span1 {
  height: 40px;
}

.homepbtn1 {
  top: 50%;
  left: 80%;
  background-color: #36344d;
  margin-right: 20px;
  font-size: 18px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  transition: 0.6s ease;
  border-radius: 10px;
  cursor: pointer;
}

.homepbtn1:hover {
  background-color: #7a7979;
  color: white;
  background-color: #FF5A36;
    border-radius: 50px;

}

.btnmargin1 {
  height: 60px;
}

.btnmargin2 {
  height: 60px;
}

.extraspace {
  height: 6px;
}



.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 36px;

}
 .weldis {
   font-family: 'Century Gothic', sans-serif;

 }

ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.tab li a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}
div[class*="tabcontent"]:first-of-type {
    display: block;
}
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

/* Dropdown Button */
.dbtn {
  color: black;
  font-size: 30px;
  border: none;
  background-color: transparent;
  cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.ddown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dcon {
  display: none;
  font-size: 16px;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dcon a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dcon a:hover {background-color: #ddd;}

/* Show the dropdown menu on hover */
.ddown:hover .dcon {display: block;}

/* Change the background color of the dropdown button when the dropdown content is shown */
.down:hover .dbtn {background-color: transparent;}

body {
background-color: white;
  height: 96vh;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}


.welcome {
  border: 2px solid black;
  color: #7d3caf;
  margin-top: 40px;
  margin-left: 40px;
  margin-right: 40px;
  border-radius: 40px;
  transition: 0.5s ease;
  font-weight: 5em;
  font-family: 'Century Gothic', sans-serif;
  background-color: #f1f1f1;
}

.welcome:hover
{
  border: 1px solid #6747c7 ;
  background-color: #6747c7;
  color: white;
  font-weight: bold;
  font-family: 'Century Gothic', sans-serif;

   }


.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 30px;
  margin-bottom: 30px;
}
 .weldis {
   font-family: 'Century Gothic', sans-serif;
   text-align: center;
 }
.gtac {
  text-align: center;
  font-family: monospace;
}
.gtacl {
  text-align: center;
}

.gtac1:hover {
  color: black;
  background-color: black;
  border:  2px solid black;
}

.lbtn {
  font-size: 20px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  border: 1px solid #fff;
  transition: 0.6s ease
}

.lbtn:hover{
  background-color: #7a7979;
  color: white;
  font-size: 24px;
}


.span1 {
  height: 40px;
}

.homepbtn1 {
  top: 50%;
  left: 80%;
  background-color: #36344d;
  margin-right: 20px;
  font-size: 18px;
  text-decoration: none;
  color: #fff;
  padding: 5px 20px;
  transition: 0.6s ease;
  border-radius: 10px;
  cursor: pointer;
}

.homepbtn1:hover {
  background-color: #7a7979;
  color: white;
  background-color: #FF5A36;
    border-radius: 50px;

}

.btnmargin1 {
  height: 60px;
}

.btnmargin2 {
  height: 60px;
}

.extraspace {
  height: 6px;
}



.weltitle {
  text-align: center;
  font-family: monospace;
  font-size: 36px;

}
 .weldis {
   font-family: 'Century Gothic', sans-serif;

 }

ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.tab li a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}
div[class*="tabcontent"]:first-of-type {
    display: block;
}
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}
<?php

$link = mysqli_connect ('localhost', 'root', 'mywebsite1245', 'CodersHome');

mysqli_select_db ($link, 'CodersHome');

//$con = mysqli_connect ('Localhost', 'id12806518_root', 'mywebsite1245', 'id12806518_codershome');

//mysqli_select_db ($con, 'id12806518_codershome');
?>

<?php
session_start ();
if( !isset($_SESSION['username']))
header('location:../login.php');
 ?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>CodersHome Agenda</title>
    <link rel="shortcut icon" type="image/png" href="../tabimg.png">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="profile/profile.css">
    <link rel="stylesheet" type="text/css" href="../navbar/navbar.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendap.css">
    <link rel="stylesheet" type="text/css" href="agendacss/agendacard.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.5/lib/darkmode-js.min.js"></script>
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <script src="agendatabs.js" charset="utf-8"></script>
  </head>
<?php include '../navbar/navbar.html' ?>
  <body>


    <div id="page-container">
      <div id="content-wrap">

        <ul class="tab">
          <li><a href="#" class="tablinks active" onclick="openCity(event, 'agenda')"><strong>Agenda</strong></a></li>
          <li><a href="#" class="tablinks" onclick="openCity(event, 'createagenda')"><strong>Crea Agenda</strong></a></li>
        </ul>
<br>
    <div id="agenda" class="tabcontent">
      <div class="welcome">
        <h1 class="weltitle">Agenda 1er D</h1>
     </div>
     <br><hr><br>
      <?php
      $result = mysqli_query($link,"SELECT * FROM agenda");

      while($row = mysqli_fetch_array($result))  {

      //echo $row["title"];
      //echo $row["description"];
      //echo $row["datef"];


      ?>

      <?php $i=0; ?>
      <div class="row">
      <div class="column">
      <div class="card">
    <?php echo $row["description"]; ?>
    <?php echo $row["datef"]; ?>  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <?php echo $row["inserter"]; ?> <?php if($_SESSION['username'] == 'admin') { include('eddel.php');

    } ?> <br>
    </div>
    </div>
    </div>
      <br>
      <?php
      $i++;
      }
      ?>

    </div>

    <div id="createagenda" class="tabcontent">


<div class="welcome2">
    <p class="weltitle">Crea Una Agenda</p>
</div>
<br><hr><br>
            <form class="agfrm" action="insert.php" method="post">

              <div class="form-group">

                <label>Title</label> <br> <br>
                <input type="text" name="title" class="form-control" required><br> <br>

              </div>

              <div class="form-group">

                <label>Description</label> <br> <br>

                <textarea name="editor1" required></textarea>
                     <script>
                             CKEDITOR.replace( 'editor1' );
                     </script> <br> <br>
              </div>

            <label>Date</label> <br> <br>
            <input type="date" id="date1" name="date1"
                   value="2020-02-25"
                   min="2020-01-01" max="2021-12-31" required> <br>
                   <br>
        <button type="submit" class="homepbtn1">Sumbit</button>


        </form>

    </div>


      </div>


  </body>

</html>

  <?php include '../Footer/index.html'?>

【问题讨论】:

  • 字符串值需要加引号,否则会被当作列名,导致查询失败。但是,既然您使用的是 mysqli,请利用 prepared statementsbind_param这将解决可能出现的任何令人讨厌的引用问题。
  • 我不明白您要更新哪些行。
  • 警告:您的代码容易受到 SQL 注入攻击。您应该使用参数化查询和准备好的语句来帮助防止攻击者通过使用恶意输入值来破坏您的数据库。 bobby-tables.com 给出了风险解释,以及如何使用 PHP / mysqli 安全地编写查询的一些示例。 切勿将未经处理的数据直接插入您的 SQL 中。以您现在编写代码的方式,有人可以轻松窃取、错误更改甚至删除您的数据...
  • ...它也可能有助于解决您几乎可以肯定遇到的 SQL 语法错误(但尚未报告)。
  • 另外,永远不要让您的网络应用以 root 身份登录数据库。 Root 可以为所欲为,因此除了 SQL 注入漏洞之外,这只会让您的数据库成为黑客的开放书。而是专门为此应用程序创建一个单独的用户帐户,该帐户仅具有它实际需要才能正常工作的权限。甚至不要在开发或测试期间使用 root 帐户作为快捷方式,因为您还需要测试您的帐户权限 - 否则当您上线时,您可能会遇到与用户帐户设置相关的意外错误。

标签: php html mysql


【解决方案1】:

你犯的错误是你关闭了 while 循环。另外,为了更好的方法,把 i 放在外面。

$i=0;
while($row = mysqli_fetch_array($result))  {

//echo $row["title"];
//echo $row["description"];
//echo $row["datef"];


$sql = "UPDATE agenda SET title = $title, description = $desc, datef = $date1, inserter = $inserter WHERE id = $row[id]; ";
  $i++;
if(mysqli_query($link, $sql)){
header("Refresh:0; url=agenda.php");
}
}
?>

【讨论】:

  • 如果 OP 真的打算用完全相同的值更新表中的 all 行,那么 SELECTwhile 循环实际上是毫无意义的效率低下。他们可以从UPDATE 查询中删除WHERE 子句并执行一次,它会更新所有行。但从更广泛的背景来看,我认为这不是真正的意图——无论如何,这将是一件非常奇怪的事情。我认为,OP 想要更新一个特定的行,并询问如何获取该行的正确 ID。答案是从 HTML 表单中传递它。
  • 如何从 html 文件中传递?我也完成了上面的 html 文件
  • @AbisGamerYT 使用类似&lt;input type="hidden" name="foo" value="&lt;?php echo $var;?&gt;" /&gt;
  • 好的,所以我在输入中获取了 id 并将其隐藏对吗?我之前尝试过,但我不知道如何获取隐藏在我的 php 文件中的 ECHO 值。我必须使用$bla = $_POST['id'];
  • 可以使用 post 或 get 方法,但如果 post 方法具有 NULL 值,请确保给出条件,因此您必须设置默认值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2022-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多