2011-08-05 2 views
0

mysqlでphpを使用しようとしています。基本的には、ユーザーがフォームを塗りつぶすインデックスページと、すべての行が表示される別のページがあります。私は選択された行/行を削除するための各行のチェックボックスを持っています。私は選択された行だけを表示する新しいページ(つまり詳細)を作成しようとしています。 $ _GETを使用しようとしていますが、できませんでした。構文が間違っている可能性があります。助けを歓迎します。私が言ったように私は推測している唯一の問題はある、チェックボックスのデータに基づいて別の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> 

detail.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> 

display.php:ここ

は、相対的なコードの一部です私はvar1に特定のIDを与えたときに行をエコーすることができるように、構文や、何か小さい。 私は再送しても申し訳ありませんが、答えが見つかりませんでした。ありがとう!

編集:チェックボックスの部分を削除し、ユーザーが対応する行の削除、編集、または詳細表示ができるwhileループでgifリンクを追加することを考えています。私はより簡単に思える。

答えて

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]; ?> 

であるべき私はまた、あなたが得るの名前ではなく、 'VAR1' として 'ID' を使用することをお勧め。 'var1'は何も意味しませんが、 'id'は意味があります。

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

私は訂正しましたが、違いはありません。何もエコーしません。 http://...../detail.php?var1 = – forsvinne

+0

を表示しているURL私はなぜそれがテーブルを表示しないのか分かりません。 – forsvinne

+0

あなたの部分的なコードから伝えるのは難しいですが、その行があなたのwhileブロック内にあることを確認してください。 – Boz

1

あなたwhile -loop外$rowsを使用しているからです。あなたはwhileループ内に

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

を入れなければなりません。


// EDIT:

さてさて、私はあなたの問題に応じて非常にneccessaryにあなたのコードを取り除いてきました。コードをコピー/ペーストするだけでは機能しません。しかし、慎重にそれを読んで、私はあなたのアイデアを取得し、あなたのコードが間違っているかもしれないものを参照願う。

<?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

display.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といくつかのデザインパターンに精通している必要があります。

+0

私はwhileループに含まれていますが、すべての行には別個の詳細リンクがあります。 'echo "​​\">details";' もう一度何も表示されず、URLにhttp://.../detail.php?var1 = <?php%20echo%20164;%20?> – forsvinne

+0

が表示されます途中でウェブサイトの真剣さ、それはインターネット上でさえ、内部だけではありません。しかし、とにかく、努力のおかげで! – forsvinne

関連する問題