Cómo pasar una cadena a una página diferente usando PHP en un evento de clic

Ok, so I've looked around all day and tried many different things, So i was wondering if you guys could help me out.

Esto es lo que obtuve:

Im building a webpage that Uses a database to update content. the content is updated based on what link the user clicks on. right now I'm using a session to pass a variable to another page to then use that variable to pull a piece of information from the database.

this is the link the user clicks on Page#1:

    session_start();
    <a href="Makes/Audi/Audi.php?brands=audi" name="audi">

and then on page two:

    session_start();

    if(isset($_GET['brands'])){
$_SESSION['brands'] = $_GET['brands'];
}
    // connect to database
mysql_connect ("localhost", "user", "pass") or die ('Error: ' . mysql_error());

    //select the DataBase
mysql_select_db ("Vehicle_Makes");

    //select make's logo and attach it to a variable
$logo = mysql_query("SELECT Logo FROM '".$_GET['brands']."'");
    ?>

and then I pull the path to an image using this:

    <div id="apDiv14"><?php echo $logo;?></div>

but the link to the image does not show up so I added this at the end of the query:

        $logo = mysql_query("SELECT Logo FROM '".$_GET['brands']."'") or die ('Error: ' . mysql_error());

and when I clicked on the link this is what displayed on a blank page:

    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''audi'' at line 1

Please help me out and thanks in advanced.

preguntado el 01 de febrero de 12 a las 04:02

You're not showing us how the GET data is being passed between pages. I would assume it's not coming in in a way that makes that SQL statement correct. Can you echo out $logo to see what it's creating? -

I just tried echoing $logo but thing came up -

This is what im trying to display on "apDiv14" : <img src="../../site-wide/car brands/Audi.png" width="247" height="214" alt="Audi"/> it's the value stored in Logo inside the database -

4 Respuestas

the FROM keyword in the MySQL query has to be either followed by table name without single quotes or must be within `.

Respondido 01 Feb 12, 08:02

$logo = mysql_query("SELECT Logo FROM '".$_GET['brands']."'");

Asi no es como mysql_query() works. It returns a result ID that you then need to pass to something like mysql_fetch_assoc() to get the actual result row.

Your query is also incorrect, given that you're not specifying the table nor an actual condition to filter on, so it's trying to select all rows from a table named whatever is in your $_GET['brands'] variable.

Respondido 01 Feb 12, 08:02

ok, how would I select what i needed from the table? and how would i use mysql_fetch_assoc() - GeneralCan

@user1095145 I'd suggest reading the docs. us.php.net/manual/en/function.mysql-fetch-assoc.php - Amber

OK, so i replaced the quotations and I get this when i echo $logo: Resource id #4. - GeneralCan

Sí porque mysql_query() returns a result id. Please actually read the docs. - Amber

Thanks for your help :) I read the material and did a lil bit more research on it and figured out how to do it. I used this: <?php while ($row = mysql_fetch_assoc($logo)) {echo $row["Logo"];};?> it worked like a charm :). thanks again - GeneralCan

Try using backticks instead of single quotes ` instead of ' in your query for the table that you're searching. Also, DON'T EVER USE UNSANITIZED INPUTS!!!!!!!!

Respondido 01 Feb 12, 08:02

"DON'T EVER USE UNSANITIZED INPUTS!!!!!!!!" ... Good advice for dating as well as database interaction. - user166560

I was getting to that, just wanted to make sure it worked first lol - GeneralCan

Yeah. It's a good thing to echo out your queries if you're having troubles with them. Then, if you can, get a visible representation of the tables you're working with. That way you can look at exactly what you're trying to do. It'll save you many wasted hours. - Tango bravo

Tu consulta es SELECT Logo FROM 'audi'. That makes 'audi' the name of the table and I think it's invalid syntax to enclose the table name in quotes. But maybe you meant to make the query something like SELECT Logo FROM brands where brand_name = 'audi'?

Respondido 01 Feb 12, 08:02

It's supposed to change the table based on what the user clicks, and thus why 'audi' must be a variable - GeneralCan

@user1095145 So you just need to get rid of the quotes. - user166560

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.