How to load a selected cell from sql table PHP
sorry my code is a bit long, please bear with me. i am trying to load the link stored inside a cell from my sql table via php. The user is able to click a checkbox and choose which link to load. however, what i have is a bit off. it loads all the links present in t开发者_如何学Pythonhe sql table instead of the one the user chooses. what did i do wrong? please guide. Thank you!
$sql = "SELECT * FROM previousbroadcast ORDER BY id DESC";
$result=mysql_query($sql);
$count = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
if (isset($_POST['re_b'])){
$xml = simplexml_load_file($row['bclink']);
}
}
and the HTML is like
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['id']; ?>">
OK, a new try with the additional information you gave:
This solution is based on the following assumptions:
- The value of the checkboxes you get is in some way related to a field in the database
- For the sake of simplicity, I have named that field
id
- it can be named differently in the database, but only you would know that...
That being said:
$sql = "SELECT * FROM previousbroadcast ORDER BY id DESC";
$result=mysql_query($sql);
$count = mysql_num_rows($result);
// in this array, we now have all the values of the checkboxes the user selected
$checkboxvalues = $_REQUEST['checkbox'];
while ($row = mysql_fetch_assoc($result)) {
if (isset($_POST['re_b'])){
// if the ID of this row is mentioned in the checkboxes the user clicked
// then - and only then - load the file
if (in_array($row[id], $checkboxvalues)) {
$xml = simplexml_load_file($row['bclink']);
}
}
}
ok, your code didn't come quite well, it's incomplete. but for what I can see the $del_record value is the checkbox array with key $i problem there is that you are calling the database every time, so it's easy to get lost.
you should store the fetched arrays in another array and then iterate there, instead of making lots of requests to the database, that will make your code run faster and you will have more control over it.
I would get SQL to do all the filtering for you:
if (isset($_POST['re_b'])) {
$checkboxvalues = isset($_REQUEST['checkbox']) ? $_REQUEST['checkbox'] : array();
$myCheckboxes = array();
foreach ($checkboxvalues as $cbv) {
$myCheckboxes[] = mysql_real_escape_string($cbv);
}
$sql = "SELECT * FROM previousbroadcast WHERE id IN ('" . implode(",", $myCheckboxes) . "') ORDER BY id DESC";
$result=mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$xml = simplexml_load_file($row['bclink']);
// do something here with $xml or it will get overwritten
}
}
精彩评论