简体   繁体   English

遍历MYSQL查询的结果并插入表中

[英]Looping through results from MYSQL query and inserting into a table

I'll try asking this question again. 我将尝试再次问这个问题。 Hopefully making more sense this time! 希望这次更有意义!

I have an user editable cell on my webpage. 我的网页上有一个用户可编辑的单元格。 I want to connect to MYSQL database and search a field for the text input into this cell, for each result found, I want to add a new row to my table. 我想连接到MYSQL数据库,并在一个字段中搜索输入到该单元格中的文本,对于找到的每个结果,我想在表中添加一个新行。 I know how to connect to the databases and I know how to create a query over html. 我知道如何连接到数据库,也知道如何通过html创建查询。 What I don't know and hoping for some guidance, is the mechanism for looping through each result and using a variable to complete the query! 我不知道,希望获得一些指导,是一种循环遍历每个结果并使用变量来完成查询的机制!

My code for adding a row 我添加行的代码

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

Query search code 查询搜索代码

<?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
?>

Any help would be greatly appreciated! 任何帮助将不胜感激!

you can do this: Here I am assuming that you have already successfully connected to database 您可以这样做:在这里,我假设您已经成功连接到数据库

//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

}

I hope this can be of some help. 我希望这可以有所帮助。

First I suggest to name your row cells in HTML as a same name cell[] . 首先,我建议将HTML中的行单元格命名为相同的名称cell[] They will be posted as an array which is much easier to loop it. 它们将以数组的形式发布,从而更容易循环。

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

Server side part: 服务器端部分:

<?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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM