开发者

Trying to pass a variable to another php page based on the data from checkbox

I'm trying to use php with mysql. basically i've an index page where user fills a form and another page where all rows are displayed. i've checkboxes for each row for deleting the selected row/rows. i'm trying to create a new page (namely details) where it shows only the selected row. I'm trying to use $_GET but i could not do it. maybe the syntax is wrong. any help is welcome.

here are the relative code parts:

display.php:

<?
require_once('auth.php');?>
<html>
<head>
<title>Goruntule</title>

<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>

<body bgcolor="#ffffff" t开发者_StackOverflow社区ext="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?


require "config.php";           // All database details will be included here 

$page_name="display.php";

$start=$_GET['start'];        // To take care global variable if OFF
if(!($start > 0)) {                // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start -0);                
$limit = 10;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 


// WE have to find out the number of records in our table. We will use this to break the pages
$query2=" SELECT * FROM table1  ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo "<TABLE width=80% align=center  cellpadding=5 cellspacing=0> <tr>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='2'>#</font></td>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='2'>ID</font></td>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='2'>Time</font></td>";
echo "</tr>";

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM table1  ORDER BY id DESC limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();

//////////////// Now we will display the returned records in side the rows of the table/////////
while($rows = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' value='" . $rows[id] . "'></td>";  
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='1'>$rows[id]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='1'>$rows[DateTime]</font></td>"; 
echo "<td><a href=\"detail.php?var1=<?php echo $rows[id]; ?>\">Details</a></td>";
//here is the problematic line i guess
echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records ////////////////////////

///// Variables set for advance paging///////////
$p_limit=100; // This should be more than $limit and set to a value for whick links to be breaked

$p_f=$_GET['p_f'];                // To take care global variable if OFF
if(!($p_f > 0)) {                         // This variable is set to zero for the first page
$p_f = 0;
}



$p_fwd=$p_f+$p_limit;
$p_back=$p_f-$p_limit;
//////////// End of variables for advance paging ///////////////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='20%'>";
if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }
echo "</td><td  align='left' width='10%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f)) { 
print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2'>PREV</font></a>"; 
} 
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
if($i <> $eu){
$i2=$i+$p_f;
echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$i</font>";}        /// Current page is not displayed as link and given font color red

}


echo "</td><td  align='right' width='10%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($p_f+$p_limit)) { 
print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2'>NEXT</font></a>";} 
echo "</td><td  align='right' width='20%'>";
if($p_fwd < $nume){
print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>NEXT $p_limit</font></a>"; 
}
echo "</td></tr></table>";


?>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<INPUT TYPE="BUTTON" VALUE="Previous" ONCLICK="window.location.href='http://......../util'"> 
</FORM></td>
</tr>
<?php

$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $id => $value)
{$sql="DELETE FROM table1 WHERE id='$value'";
$result = mysql_query($sql);
}

if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
?>
<a href="detail.php?var1=<?php echo $rows[id]; ?>">details</a>
<?
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

detail.php

<?php
require_once('auth.php');
$host="localhost";  
$username=""; 
$password=""; 
$db_name=""; 
$tbl_name="table1"; 

mysql_connect("$host", "$username", "$password")or die("Cannot connect ". mysql_error()); 
mysql_select_db("$db_name")or die("Cannot select DB ". mysql_error());

$num=$_GET['var1'];
$query = "SELECT * FROM table1 where id='$num'"; 

$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_row($result) or die(mysql_error());
?>
<table border="0" align="center" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="13" align="center" bgcolor="#FFFFFF"><strong>Bölge</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Time</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['13']; ?></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['0']; ?></td>
</tr>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<input type=button value="Close" onClick="javascript:window.close();">
</form> 
</tr>
<?php

$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $key=>$value)
{$sql="DELETE FROM $tbl_name WHERE id='$value'";
$result = mysql_query($sql);
}

if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
<html><head><link href="loginmodule.css" rel="stylesheet" type="text/css" /></head></html>

As I've said, the only problem i'm guessing is with the syntax, or something small as i can echo the row when i gave the var1 a specific id. I'm sorry if i'm reposting but i couldn't find an answer. Thanks!

Edit: I'm thinking of deleting checkbox parts and adding gif links in the while loop where users can delete, edit or detailed view of the corresponding row. seems easier i guess.


Make sure that the details link is within the while block.

<?php
while($rows = mysql_fetch_array($result)) {
    echo '<a href="detail.php?var1='.$rows[id].'">Details</a>';
}
?>

Looks like you should not be using double quotes.

<? echo "$rows[id]" ?>

Should be

<?php echo $rows[id]; ?>

I also suggest you use 'id' as the name of the get rather than 'var1'. 'var1' does not mean anything whereas 'id' makes more sense.

<a href="detail.php?id=<?php echo $rows[id]; ?>">Details</a>


It's because you are using $rows outside your while-loop. You have to put the

<a href="detail.php?var1=<? echo $rows[id]; ?>">Details</a>

inside the while loop.


//EDIT:

Alright, I've stripped your code to the very neccessary according to your problem. So don't just copy/paste the code, it probably won't work. But read it carefully, and I hope you get the idea and see what may be wrong with your code ;)

display.php

<?php
require_once('auth.php');
require "config.php";

$page_name="display.php";
$start = (isset($_GET['start']) && $_GET['start'] < 1) ? 0 : $_GET['start'];

$eu = ($start-0);
$limit = 10;

$query="SELECT * FROM table1  ORDER BY id DESC limit $eu, $limit";
$result=mysql_query($query);
echo mysql_error();

$i = 0; //counter for the bg-color
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<table width="80%" align="center" cellpadding="5" cellspacing="0"> 
<?php while($rows = mysql_fetch_array($result)) : 
$bgcolor = $i%2 == 0 ? '#ffffff' : '#f1f1f1';
?>
    <tr>
        <td>
            <input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>">
        </td>
        <td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="title">
            &nbsp;<?php echo $rows['id']; ?>
        </td>
       <td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="date">
            &nbsp;<?php echo $rows['DateTime']; ?>
       </td>"; 
       <td>
            <a href="detail.php?var1=<?php echo $rows[id]; ?>">Details</a>
       </td>
    </tr>
<?php endwhile; ?>
</table>
</form>

detail.php

<?php
require_once('auth.php');

$num= isset($_GET['var1']) ? $_GET['var1'] : '';

$query = "SELECT * FROM table1 where id='$num'"; 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());

?>

<form name="form1" method="post" action="">
<table border="0" align="center" cellspacing="1" cellpadding="0">
    <input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>">
    <? echo $row['13']; ?>
    <? echo $row['0']; ?>
    <input name="delete" type="submit" id="delete" value="Delete">
    <button onClick="javascript:window.close();">Close</button>
</table>
</form>

And not only for security's sake, you should get more familiar with PHP and some design patterns before publishing your website.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜