我只使用此代码获得一个结果

时间:2021-11-16 09:13:05
<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>

good morning , in the above code iam trying to create a table of 2 columns delete and update , being able to manage mysql through this page , but i get only 1 row from mysql table although i expect 4 (4 rows are saved in mysql table) what's the wrong here , thanks in advance

早上好,在上面的代码iam试图创建一个2列的表删除和更新,能够通过这个页面管理mysql,但我从mysql表只得到1行,虽然我期待4(4行保存在mysql表中这里有什么不对,提前谢谢

3 个解决方案

#1


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>

#2


1  

Your fetching the result and counting the rows in the wrong places, and you have a sub while loop that basically does nothing.

你获取结果并计算错误位置的行,并且你有一个sub while循环基本上什么都不做。

Here Try this:

试试这个:

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

#3


1  

Good approach but it really require a better implementation.

好方法,但它确实需要更好的实施。

First, get yourself a function and put it in mysql.php for the frequent use.

首先,给自己一个函数并将其放在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;
}

then write a code to get the data

然后编写代码来获取数据

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

then write a template to make your approach with HTML template complete:

然后编写一个模板,使您的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>

Look: your code become 2 times shorter yet WAY more readable!

看:你的代码变短了两倍,但更具可读性!

Note that you shouldn't pass whole data to the editing scripts. Only id is enough and required! Fetch the data to edit from the DB in the update script.

请注意,您不应将整个数据传递给编辑脚本。只有id就足够了!从更新脚本中的DB获取要编辑的数据。

Also note that you shouldn't use GET method to delete records - only post. So, let me suggest you to have a "Delete" button not in the table but in the update form.

另请注意,您不应使用GET方法删除记录 - 仅发布。所以,我建议你有一个“删除”按钮,不在表格中,而是在更新表格中。

#1


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>

#2


1  

Your fetching the result and counting the rows in the wrong places, and you have a sub while loop that basically does nothing.

你获取结果并计算错误位置的行,并且你有一个sub while循环基本上什么都不做。

Here Try this:

试试这个:

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

#3


1  

Good approach but it really require a better implementation.

好方法,但它确实需要更好的实施。

First, get yourself a function and put it in mysql.php for the frequent use.

首先,给自己一个函数并将其放在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;
}

then write a code to get the data

然后编写代码来获取数据

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

then write a template to make your approach with HTML template complete:

然后编写一个模板,使您的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>

Look: your code become 2 times shorter yet WAY more readable!

看:你的代码变短了两倍,但更具可读性!

Note that you shouldn't pass whole data to the editing scripts. Only id is enough and required! Fetch the data to edit from the DB in the update script.

请注意,您不应将整个数据传递给编辑脚本。只有id就足够了!从更新脚本中的DB获取要编辑的数据。

Also note that you shouldn't use GET method to delete records - only post. So, let me suggest you to have a "Delete" button not in the table but in the update form.

另请注意,您不应使用GET方法删除记录 - 仅发布。所以,我建议你有一个“删除”按钮,不在表格中,而是在更新表格中。