简体   繁体   English

是否可以将列表项名称存储到可以在SQL查询中使用的变量中?

[英]Is it possible to store a list item name into a variable that can be used in an SQL query?

I am trying to grab the text in a list item that a user clicks on and then store it in a variable. 我试图获取用户单击的列表项中的文本,然后将其存储在变量中。 I would then use that variable as a search term in an sql query. 然后,我将使用该变量作为sql查询中的搜索词。 I would like to know if it is possible to do this. 我想知道是否可以这样做。

I think I have most of the code written, I'm just confused on how to make all of the files work together. 我想我已经写了大部分代码,只是对如何使所有文件协同工作感到困惑。

Jquery script to change class names to whatever text the user clicks on: jQuery脚本将类名更改为用户单击的任何文本:

$('li').click(function () {
    var name = $(this).text(); 
    var sqlname = name;
    $('#prf').attr("class",name);
    $('#pic').attr("class",name);
    $('#info').attr("class",name);

 });

SQL Code within a PHP document to search a MyPHPAdmin database for whatever value 'stagename' is equal to. PHP文档中的SQL代码,用于在MyPHPAdmin数据库中搜索“ stagename”等于的任何值。

<?php

$link = mysqli_connect("host", "username", "pass","db_name") or die ("Error ". mysqli_error($link));

$query = "SELECT realname
           FROM artistmaster
           WHERE stagename = '**jQuery variable here**'";

        $result = mysqli_query($link, $query)  or die(mysqli_error($link));         

        $row = mysqli_fetch_assoc($result);

        foreach($row as $key => $value) {
                        $$key = $value;
                        } ;
        echo $realname;
?>

EDIT: I already have the MyPHPAdmin setup, and I have already created a database with the necessary data. 编辑:我已经有了MyPHPAdmin设置,并且我已经用必要的数据创建了一个数据库。 (all stagenames and realnames of each artist) (每个艺术家的所有艺名和真实姓名)

So basically the user clicks on a list item that has the stage-name of musical artist, the stage-name then gets ran through an sql query that returns the real name of the musician. 因此,基本上,用户单击具有音乐艺术家的艺名的列表项,然后通过SQL查询运行该艺名,该查询返回音乐家的真实姓名。

I'm fairly new to programming in general, and have just started learning HTML, PHP, and JQuery within the past week. 我一般对编程还不陌生,并且在过去一周内才刚刚开始学习HTML,PHP和JQuery。 That being said, If i'm over-complicating this somehow or if anyone has a suggestion on how to simply if this, that would be great. 话虽这么说,如果我以某种方式过度繁琐,或者如果有人对如何简单地提出建议,那将很棒。

What you're attempting to do here has been done many, many times before, so don't think you're trying to reinvent the wheel! 您在这里尝试做的事情已经做过很多次了,所以不要以为您要重新发明轮子! The general premise is something like this: 一般前提是这样的:

  • User clicks a button/link/whatever 用户点击按钮/链接/任何
  • The jQuery sends an AJAX request to the PHP server using GET or POST methods. jQuery使用GET或POST方法将AJAX请求发送到PHP服务器。 For GET, the parameters are in the query string (eg ?val1=1&val2=2 ). 对于GET,参数位于查询字符串中(例如?val1=1&val2=2 )。 For POST, the values will be stored in the body of the message, represented in jQuery $.ajax by the data field. 对于POST,值将存储在消息正文中,在jQuery $.ajaxdata字段表示。
  • The PHP page will make the mysqli call to the database based off of what was in the $_GET[] or $_POST[] variable (depending on if you used get or post). PHP页面将根据$_GET[]$_POST[]变量中的内容(取决于使用的是get还是post)对数据库进行mysqli调用。 This is where things like SQL Injection prevention (ie parametariazation) are extremely important. 这就是SQL注入预防(即参数化)之类极为重要的地方。
  • The PHP page returns results, normally as JSON (javascript object notation), but technically can be anything that you will eventually interpret with JSON PHP页面通常以JSON(JavaScript对象表示法)的形式返回结果,但从技术上讲,它可以是您最终将使用JSON解释的任何结果
  • The JSON call takes the results returned and does what you need it to do with them JSON调用接受返回的结果,并根据需要进行处理

There's a few technologies involved there, but the general idea is that the client (jQuery) cannot talk to the database directly, it has to go through the server. 这里涉及到一些技术,但是一般的想法是客户端(jQuery)无法直接与数据库对话,它必须通过服务器。 The use of AJAX makes it possible to do so 'on-the-fly' without having to reload the page. 使用AJAX可以“即时”执行此操作,而不必重新加载页面。

If nothing I mentioned in this post makes any sense, I'd encourage you to spend some time reading up not only on documentation, but tutorials and examples. 如果我在本文中提到的内容没有任何意义,我鼓励您花一些时间阅读不仅是文档,还有教程和示例。 This type of setup is, some would say, the core to web development in today's world. 有人会说,这种类型的设置是当今世界上Web开发的核心。 It's a bit much to understand, yes, but once you do web programming makes a lot more sense. 是的,要理解它有点多,但是一旦完成Web编程,意义就更大了。

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

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