【问题标题】:Looping through results from MYSQL query and inserting into a table循环遍历 MYSQL 查询的结果并插入到表中
【发布时间】:2013-07-14 09:52:47
【问题描述】:

我会再次尝试问这个问题。希望这次更有意义!

我的网页上有一个用户可编辑的单元格。我想连接到 MYSQL 数据库并在一个字段中搜索输入到此单元格的文本,对于找到的每个结果,我想在我的表中添加一个新行。我知道如何连接到数据库,也知道如何通过 html 创建查询。我不知道并希望得到一些指导的是循环每个结果并使用变量完成查询的机制!

我的添加行代码

<script>
function getsearchresults()
{
var table=document.getElementById("myTable");
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
cell1.innerHTML= Variable1heremaybe?;
cell2.innerHTML="Variable2heremaybe?;
cell3.innerHTML="Variable3heremaybe?;
cell4.innerHTML="Variable4heremaybe?;
}
</script>
</head>
<body>

<table style="margin-top:350px; margin-left:25px;" id="myTable" border="1">
  <tr>
    <td>column1 <div style="width: 100px"> </div></td>
    <td>column2 <div style="width: 100px" > </div></td>
    <td>column3 <div style="width: 100px" > </div></td>
    <td>column4 <div style="width: 100px" > </div></td>

</table>

查询搜索码

<?php
// Create connection
$con=mysqli_connect("bla","blabla","blablabla","blablabla");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

SELECT `Table1`.`Column1`
FROM Table1
WHERE (`Table1`.`Column1` Variableheremayb?)
ORDER BY `Table1`.`Column1` ASC
?>

任何帮助将不胜感激!

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    你可以这样做: 这里我假设你已经成功连接到数据库了

    //first execute the query 
    
    $result = mysqli_query("Your query");
    
    //then fetch the result of each row using a loop
    
    while($row = mysqli_fetch_assoc($result))
    {
      //here you can display the data or store it in a variable or anything else you want
      // for example you want to display it inside div tag.
       echo "<div>".$row["column1"]."</div>";
       echo "<div>".$row["column2"]."</div>";
       //and so on
    
    }
    

    我希望这能有所帮助。

    【讨论】:

    • 谢谢!但是,在 while 语句中,我似乎无法将新行添加到表中,因此显然无法添加结果。我不熟悉 .$row["column1"],这是假设用我们活动结果的 column1 中的数据填充 'mytable' 中的新行吗? link 这是我正在寻找的影响,但我们查询的每个结果!
    • 你熟悉ajax吗
    • 不 :( 哈哈,有点菜鸟我害怕!。我对编程很陌生
    • 如果我没记错的话,您希望在点击搜索按钮时将行添加到现有的 html 表中而不刷新页面,对吗?如果是这样,那么它需要使用 ajax 来完成,所以你需要先学习它才能这样做,如果之后卡住了,你可以随时回到 stackflow 寻求帮助。
    • 感谢您的帮助,在我离开并正确学习 ajax 之前的最后一个问题!这可能看起来很幼稚,但我发送的示例似乎并没有刷新页面,只是使用了 var cell1=row.insertCell(0);然后使用 cell1.innerHTML="New"; 直接填充每个单元格。有没有办法我可以传递一个变量?也许使用一个计数器来增加结果?
    【解决方案2】:

    首先,我建议将 HTML 中的行单元格命名为同名 cell[]。它们将被发布为一个更容易循环的数组。

    <table>
      <tr>
        <td><input type="text" name="cell[]" /></td>
        <td><input type="text" name="cell[]" /></td>
        <td><input type="text" name="cell[]" /></td>
      </tr>
    </table>
    

    服务器端部分:

    <?php
    
    // Connect to MySQL
    ...
    
    // Validate your form inputs
    ...
    $cells = yourInputFilter($_POST['cell']);
    ...
    
    // Build query
    $cellSql = implode(',', $cells);
    
    $sql = "SELECT *
            FROM table1
            WHERE column1 IN ($cellSql)
            ORDER BY column1";
    

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2015-10-05
      相关资源
      最近更新 更多