【问题标题】:Trying to pass a variable to another php page based on the data from checkbox尝试根据复选框中的数据将变量传递到另一个 php 页面
【发布时间】:2011-08-05 18:49:36
【问题描述】:

我正在尝试将 php 与 mysql 一起使用。基本上我有一个用户填写表格的索引页面和显示所有行的另一个页面。我为每一行设置了复选框,用于删除选定的行/行。我正在尝试创建一个仅显示所选行的新页面(即详细信息)。 我正在尝试使用 $_GET 但我做不到。也许语法是错误的。欢迎任何帮助。

以下是相关代码部分:

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" text="#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>

正如我所说,我猜测的唯一问题是语法,或者当我给 var1 一个特定的 id 时我可以回显该行的一些小问题。 如果我要重新发布,我很抱歉,但我找不到答案。谢谢!

编辑:我正在考虑删除复选框部分并在 while 循环中添加 gif 链接,用户可以在其中删除、编辑或详细查看相应行。我猜似乎更容易。

【问题讨论】:

    标签: php variables get


    【解决方案1】:

    确保详细信息链接在 while 块内。

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

    看来您不应该使用双引号。

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

    应该是

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

    我还建议您使用 'id' 作为 get 的名称,而不是 'var1'。 'var1' 没有任何意义,而 'id' 更有意义。

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

    【讨论】:

    • 感谢您的快速回复,我已更正但没有区别。不回声任何东西。显示 http://...../detail.php?var1= 的网址
    • 我也不明白为什么它不显示表格。
    • 很难从您的部分代码中分辨出来 - 但请确保该行在您的 while 块内,看起来它可能在它之外。
    • 另外,看起来你对这个很陌生。所以保持简单。删除除详细信息链接之外的所有内容。确保它有效。然后开始重新添加表格代码。
    • 是的,我是新手,完全取决于互联网教程 =)
    【解决方案2】:

    这是因为您在 while 循环之外使用了 $rows。你必须把

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

    在while循环内。


    //编辑:

    好的,根据您的问题,我已将您的代码剥离为非常必要的。所以不要只是复制/粘贴代码,它可能不起作用。但请仔细阅读,我希望您能明白这一点,看看您的代码可能有什么问题;)

    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>
    

    不仅出于安全考虑,您还应该在发布网站之前更加熟悉 PHP 和一些设计模式。

    【讨论】:

    • 我已包含在 while 循环中,现在每一行都有单独的详细信息链接。 'echo "\">详情";'但又没有显示任何内容,并且 url 显示 http://........./detail.php?var1=
    • 好吧,顺便说一句,它不会是一个严肃的网站,它甚至不在互联网上,只是在内部。但无论如何,感谢您的努力!
    猜你喜欢
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2020-11-20
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多