dongxiaoguang9108 2016-06-15 19:06
浏览 35

PHP到SQL请求

So I am new to this and I wanted to use the ID of my li elements to make a request to SQL but realized too late that was not possible. Now I am stuck and I don't know how to proceed other than by doing this the long way and coding each li individually.

Here is my code:

         <li style="float: right;">Themes
            <ul>

             <?php
                $id_count = 0;
                $db=mysql_connect('localhost','root','');
                if(!$db) {
                    die('Could not connect: '.mysql_error());
                }
                $connection_string=mysql_select_db('Mydb',$db);
                $selectSQL='SELECT * FROM color_patterns';       
                $queryset=mysql_query($selectSQL);
                $num=mysql_num_rows($queryset);
                if(0==$num) {
                    echo "No record";
                    exit;
                } else {
                    while($row=mysql_fetch_assoc($queryset)) {?>
                    <li id="<?php echo($id_count) ?>" onclick="
                              $sql='SELECT * FROM color_elements WHERE pat_ID = "the index of the li"';       
                              $query=mysql_query($sql);
                              $hex1 = "hex code in row 1"
                              $hex2 = "hex code in row 2"
                              $hex3 = "hex code in row 3"">
                        <?php echo($row['name']); 
                        $id_count++;
                        ?></li><?php
                    }

                }
            ?>


            </ul>
        </li>

The themes list element has a dropdown list which is populated by the while loop (it creates a new li for each row in the table color_patterns and puts the name of the row as the title of the li, this part works perfectly). Now when the user clicks on one of the li in the dropdown I wanted to send a SQL request to the color_elements table where the id = "the clicked li index" and it will retrieve a table with 3 rows and and I would assign the value in the "hex_code" column from each row to one of 3 php variables. I can't think of anyway to do this (mainly send the clicked li index to SQL) is it possible or will I have to do it another way (I know another way but it's ugly and long).

For example: My color_patterns table has 2 rows with names A and B,

the while loop creates 2 li labeled one labeled A and the other B, When the user clicks A a sql command is run:

   $sql='SELECT * FROM color_elements WHERE pat_ID = 1'; //here 1 is the index of A

this returns a table with 3 rows and I use the "hex_code" column to assign $hex1,$hex2,$hex3 values based on the 1st, 2nd and 3rd row.

Any help at all is appreciated. Thank you.

  • 写回答

1条回答 默认 最新

  • dongsou0083 2016-06-15 22:49
    关注
    while($row=mysql_fetch_assoc($queryset)) {?>
                        <li id="<?php echo($id_count) ?>" onclick='
                                  $sql="SELECT * FROM color_elements WHERE pat_ID = $id_count"';       
                                  $query=mysql_query($sql);
                                  $hex1 = "hex code in row 1"
                                  $hex2 = "hex code in row 2"
                                  $hex3 = "hex code in row 3"">
                            <?php echo($row['name']); 
                            $id_count++;
                            ?></li><?php
                        }
    

    Why do you want to get the value from MySQl if it's available in PHP?

    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写