2012-04-28 69 views
1
<a href="add.php">add new product</a><br> 
<br> 

<?php 

include("mysql.php"); 

$result = mysql_query("SELECT * FROM gallery "); 

$just = mysql_fetch_array($result); 
$num=mysql_num_rows($result); 

$table=""; 
$table.="<td>delete</td>"; 

$table.="<td>update</td>"; 
if ($num > 0) { 
$i=0; 

while($just = mysql_fetch_array($result)) 
{ 
$num=mysql_num_rows($result); 
    { 

$table .= "<tr>"; 

$table .= "<td><a href=\"delete.php?name=".$just['name']."&id=".$just['id']."\">".$just['title']."</a></td>"; 
$table .= "<td><a href=\"update.php?description=".$just['description']."&title=".$just['title']."&id=".$just['id']."\">".$just['title']."</a></td>"; 

    } 
$table .= "</tr>"; 
while ($i < $num) { 

$name = stripslashes(mysql_result($result,$i,"name")); 
$title = stripslashes(mysql_result($result,$i,"title")); 
$description = stripslashes(mysql_result($result,$i,"description")); 

++$i; } 
} 
} 


else { $table = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; } 

?> 

<table border="1" cellpadding="1" cellspacing="2"><? echo $table ?></table> 

早上好,在上面的代碼iam試圖創建一個2列的表刪除和更新,能夠通過這個頁面管理mysql,但我只得到1行mysql表雖然我預計4(4行被保存在MySQL表) 有什麼錯在這裏,在此先感謝我只使用這段代碼得到一個結果

+0

另外,你真的需要,以防止[XSS漏洞(http://stackoverflow.com/questions/3129899/what-are-the-common-defenses-against-xss)。 – DCoder 2012-04-28 04:50:52

回答

1
<a href="add.php">add new product</a><br> 
<br> 

<?php 

include("mysql.php"); 

$result = mysql_query("SELECT * FROM `gallery`"); 

$num = mysql_num_rows($result); 

$table = ""; 
$table .= "<td>delete</td>"; 
$table.="<td>update</td>"; 

if ($num > 0) { 
    $i=0; 
    while($just = mysql_fetch_assoc($result)) { 
     $num=mysql_num_rows($result); 
     $table .= "<tr>"; 
     $table .= "<td><a href=\"delete.php?name=".$just['name']."&id=".$just['id']."\">".$just['title']."</a></td>"; 
     $table .= "<td><a href=\"update.php?description=".$just['description']."&title=".$just['title']."&id=".$just['id']."\">".$just['title']."</a></td>"; 
    } 
    $table .= "</tr>"; 
    while ($i < $num) { 
     $name = stripslashes(mysql_result($result,$i,"name")); 
     $title = stripslashes(mysql_result($result,$i,"title")); 
     $description = stripslashes(mysql_result($result,$i,"description")); 
     ++$i; 
    } 
} else { 
    $table = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; 
} 
?> 

<table border="1" cellpadding="1" cellspacing="2"><? echo $table ?></table> 
+0

嗨,大家好,所有的答案都是正確的謝謝,但最後給了我正是我需要我的代碼感謝所有 – user1337175 2012-04-30 05:40:50

1

你獲取的結果,並在錯誤的地方計數行,你必須在一個子循環基本上什麼都不做。

這裏試試這個:

<?php 
include("mysql.php"); 

$result = mysql_query("SELECT `id`,`name`,`title`,`description` FROM gallery"); 

$table=null; 
if (mysql_num_rows($result) > 0) { 
    while($just = mysql_fetch_assoc($result)){ 
     $table .= "<tr>".PHP_EOL; 
     $table .= "<td><a href=\"delete.php?name=".$just['name']."&id=".$just['id']."\">".$just['title']."</a></td>".PHP_EOL; 
     $table .= "<td><a href=\"update.php?description=".$just['description']."&title=".$just['title']."&id=".$just['id']."\">".$just['title']."</a></td>".PHP_EOL; 
     $table .= "</tr>".PHP_EOL; 
    } 
}else{ 
    $table = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; 
} 
?> 

<table border="1" cellpadding="1" cellspacing="2"><? echo $table; ?></table> 
1

好方法,但它確實需要一個更好的實現。

首先,讓自己的一個函數,並把它放在mysql.php頻繁使用。

function sqlArr($sql){ 
    $ret = array(); 
    $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql); 
    if ($res) { 
    while($row = mysql_fetch_array($res)){ 
     $ret[] = $row; 
    } 
    } 
    return $ret; 
} 

然後編寫代碼來獲取數據

<?php 
include("mysql.php"); 
$data = sqlArr("SELECT * FROM tbl_names"); 
foreach ($data as $k => $value) $data[$k] = htmlspecialchars($value); 
include 'template.php'; 

然後寫一個模板,以與HTML模板完成你的方法:

<table border="1" cellpadding="1" cellspacing="2"> 
<? if (!$data)): ?> 
    <tr> 
    <td colspan="2" align="center">Nothing found</td> 
    </tr> 
<? else: ?> 
<?  foreach($data as $just): ?> 
    <tr> 
    <td><a href="update.php??id="<?=$just['id']?>"><?=$just['title']?></a></td> 
    </tr> 
<?  endforeach ?> 
<? endif ?> 
</table> 

看:你的代碼變得更短的2倍但更可讀性更強!

請注意,您不應該通整個數據到編輯腳本。只有id是足夠的和必需的!在更新腳本中從數據庫獲取要編輯的數據。

另外請注意,你不應該使用GET方法刪除記錄 - 只發布。所以,讓我建議你不要在表格中使用「刪除」按鈕,而是在更新表單中。

相關問題