Jump to content

Need to query mysql databe with random number of variables from a post


billgod

Recommended Posts

I have no idea where to begin here.  I am pretty new to php and am writing a site trying to learn the language.  I have a form that populates from a select statement from my database.  on this page you select a few random inputs via check boxes.  hit the submit button.  Those check boxes pass my ID's of a table.  I need to be able to run a query against those ID's. So I basically need to generate a dynamic query based upon the post.  I hope this make sense to some one.  From what I read so far I need to use $_POST['query']  but have no idea how to use it.

ummm Post some of your code for better help. It is hard to code "blind" as it be.

 

<?php

foreach ($_POST as $key => $val) {
     if (stristr($key, "chk")) {
          $ids[] = $val;
     }
}

$ids = implode(", ", $val);

$sql = "SELECT * FROM table_name WHERE column IN(" . $ids . ")";
$res = mysql_query($sql);

while ($row = mysql_fetch_assoc($res)) {
      // etc etc
}

?>

 

The above is assuming that in your form you use "chk" as part of each checkbox's name.

Thanks for the quick response.  Nice to see a forum people participate in.

 

here is the code for the form. (i know most of it is probably crappy code)

 

<?php

 

 

ob_start();

$host="cartman"; // Host name

$username="bill"; // Mysql username

$password="mypassword"; // Mysql password

$db_name="heroscape"; // Database name

$tbl_name="characters"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

 

$sql="SELECT * FROM $tbl_name WHERE available='yes'";

$result=mysql_query($sql);

 

?><head>

<script type="text/javascript" src="lightbox.js"></script>

 

<TITLE></TITLE></head>

 

<form action="dopickteamverify.php" method="POST">

<table border="1" cellpadding="1">

<tr>

<td><? echo "Choose"; ?></td>

<td width="35"><? echo "Image"; ?></td>

<td width="150"><? echo "Name"; ?></td>

<td ><? echo "Points"; ?></td>

<td><? echo "Life"; ?></td>

<td><? echo "Move"; ?></td>

<td><? echo "Range"; ?></td>

<td><? echo "Attack"; ?></td>

<td><? echo "Defense"; ?></td>

<!--<td><? echo "Special"; ?></td>-->

</tr>

<?

while($rows=mysql_fetch_array($result)){

 

 

?>

<tr>

<td><? $myid=$rows['id']; echo "<input name='$myid' type='checkbox' id='$myid'>"; ?></td>

<td width="25"><? $myimage= $rows['images']; echo "<a href='$myimage'  rel='lightbox'><img src='$myimage' width='35'></a>

" ; ?></td>

<td width="150"><? echo $rows['name']; ?></td>

<td ><? echo $rows['points']; ?></td>

<td><? echo $rows['life']; ?></td>

<td><? echo $rows['move']; ?></td>

<td><? echo $rows['range']; ?></td>

<td><? echo $rows['attack']; ?></td>

<td><? echo $rows['defense']; ?></td>

<!--<td><? echo $rows['special']; ?></td>-->

</tr>

 

<?

// close while loop

}

?>

</table>

<input type="submit" name="Submit" value="Go">

</form>

<?

// ob_end_flush();

?>

 

Now I need to update the database to set column "available" to no where ID=X

 

 

change line

<td><? $myid=$rows['id']; echo "<input name='$myid' type='checkbox' id='$myid'>"; ?></td>

to

<td><? $myid=$rows['id']; echo "<input name='id[]' value='$myid' type='checkbox' id='$myid'>"; ?></td>

and in next page print_r($_POST['id']);

I am aware of how to do an SQL query to update a record.  print_r($_POST['id']); Is already in my second page for testing.  It does list all my 'ID' on the screen.  What I need to know is how to build an update query from the values returned in the $_post.  I will try Sasa's changes tonight but have a feeling I will still be in the same boat since I don't know how to loop through the $_POST variable and query on each ID.

foreach ($_POST as $key => $val) {
    if ($key == "id") {
          foreach ($val as $val2) {
               $ids[] = $val2;
          }
    }
}

$ids = implode(", ", $ids);

mysql_query("UPDATE table_name SET col1 = 'test' WHERE id IN(" . $ids . ")");

 

<?php

if (isset($_POST['id']) && is_array($_POST['id'])) {
  foreach ($_POST['id'] as $key => $val) {
          foreach ($val as $val2) {
               $ids[] = $val2;
          }
  }

  $ids = implode(", ", $ids);

  mysql_query("UPDATE table_name SET col1 = 'test' WHERE id IN(" . $ids . ")");
}else {
    echo 'No POST data';
}

 

Chances are it was because the post data wasnt there. Try that and see what happens.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.