检查每个MySQL查询结果行并执行操作

时间:2014-12-20 10:45:31

标签: php mysql

我有2个框架,还有一个充满元素的MySQL表。

第一帧有一个下拉菜单和一个提交按钮。下拉列表的值(总共15个)来自MySQL数据库,查询为SELECT DISTINCT (table_column) table_name

<!DOCTYPE html>
<html>
<body>
<form method="post">
<?php
    include_once "CONNECT_TO_DB_SCRIPT.php";
    $query_ak = 'SELECT DISTINCT (column_name) FROM table_name';
    $result = mysql_query($query_ak) or die(mysql_error());
?>
<select name="exa">
<?php
    while ($row = mysql_fetch_assoc($result)) {
        echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
    }
?>
</select>
<input type="submit" value="GO!" name="go"/>
</form>
</body>
</html>

所以现在我想在第二帧中检查我们是否选择下拉列表的值来显示MySQL表的等效部分,其中某个column_name的值与下拉菜单中提交的值相同。 / p>

示例:SELECT * FROM table_name where column_name= value_of_dropdown_menu.

我试过这个,但它不起作用:

<?php
include_once "CONNECT_TO_DB_script.php";
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result_ak = mysql_query ($query_ak) or die (mysql_error());

if (isset($_POST['go'])) { //checks if submit button has a value or it is NULL

    while ($row = mysqli_fetch_assoc($result_ak)) {
    if ($_POST["exa"] == "$row['ak_ex']") {
        $query = "SELECT * FROM table_name where column_name='.$row['ak_ex'].'";
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());

…

close connection
?>

2 个答案:

答案 0 :(得分:0)

  

试试这个

<form method="post">
<?php
    include_once "CONNECT_TO_DB_SCRIPT.php";
    $query_ak = 'SELECT DISTINCT (column_name) FROM table_name';
    $result = mysql_query($query_ak) or die(mysql_error());
?>
<select name="exa">
<?php
    while ($row = mysql_fetch_array($result)) {
        echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
    }
?>
</select>
<input type="submit" value="GO!" name="go"/>
</form>

<?php
if(isset($_REQUEST['go'])) {
$a = $row['exa'];
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result_ak = mysql_query ($query_ak) or die (mysql_error());

    while ($row = mysql_fetch_array($result_ak)) {
        if ($row['col_name'] == $a) {
            $query = "SELECT * FROM table_name where column_name='".$row['ak_ex']."' ";
            $result = mysql_query($query) or die('Query failed: ' . mysql_error());
        }
    }
}
?>

答案 1 :(得分:-1)

请注意,您的SQL结果来自mysql_query()函数,但您正在使用mysqli_fetch_assoc()从中获取数据。使用mysql_fetch_array() 此外,您正在经历一个非常艰难的过程,以获取发布的数据和一些语法错误。在第二帧中是这样的:

<?php

    include_once "CONNECT_TO_DB_script.php";
    if (isset($_POST['go']))    {   //checks if submit button has a value or it is NULL
            $query= 'SELECT * FROM table_name where column_name=\''.$_POST["exa"].'\'';
            $result= mysql_query($query) or die ('Query failed: ' . mysql_error());
.
.
.
close connection
?>