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>

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

1
  • In addition, you really need to protect against XSS vulnerabilities. Commented Apr 28, 2012 at 4:50

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

hi guys all answers are correct thanks , but the last gave me exactly what i need for my code thanks all
1

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

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>

Comments

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.

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:

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.