pufAmuf
pufAmuf

Reputation: 7805

Use variable as ID and get table from mysql (edited)

I'm using a multiple select option form to get a table of venues. Each venue has an ID and this is what I used:

<?php
require("db_access.php");

if(isset($_POST['select3'])) 
{
  $aVenues = $_POST['select3'];

  if(!isset($aVenues))
  {
    echo("<p>You didn't select any venues!</p>\n");
  }
  else
  {
    $nVenues = count($aVenues);

    echo("<p>You selected $nVenues venues: ");
    for($i=0; $i < $nVenues; $i++)
    {
      echo($aVenues[$i] . " ");
    }
    echo("</p>");

$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $aVenues);
echo $comma_separated;



  }
}

?>

It results in this: enter image description here

However I thought that the code would use those two numbers below and draw out a table with those id's I used :/ ? Am I missing something?

Upvotes: 0

Views: 93

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270647

$array is used in implode(",", $array); but is not defined anywhere else that we can see. It is perhaps intended to be:

implode(",", $aVenues);

UPDATE

Per comments, it does not draw a table because you never actually query your database.

You build your SQL statement, but you need to execute it and fetch the result set.

// Make sure you actually have a database connection
$conn = mysql_connect('localhost', $username, $password);
mysql_select_db($database);

$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $array);
echo $comma_separated;

// Execute query and fetch result rowset
$result = mysql_query($sql);
if ($result) {
  $rowset = array();
  while ($row = mysql_fetch_array($result)) {
    $rowset[] = $row;
  }
  var_dump($rowset);
}
else echo mysql_error();

Upvotes: 1

Related Questions