简体   繁体   中英

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

$('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

$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. (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.

I'm fairly new to programming in general, and have just started learning HTML, PHP, and JQuery within the past week. 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. For GET, the parameters are in the query string (eg ?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.
  • 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). This is where things like SQL Injection prevention (ie parametariazation) are extremely important.
  • The PHP page returns results, normally as JSON (javascript object notation), but technically can be anything that you will eventually interpret with JSON
  • The JSON call takes the results returned and does what you need it to do with them

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. The use of AJAX makes it possible to do so 'on-the-fly' without having to reload the page.

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. It's a bit much to understand, yes, but once you do web programming makes a lot more sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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