MySQL Forums
Forum List  »  PHP

How to determine the value of a specific column in a row?
Posted by: Bob Anonymous
Date: November 07, 2012 06:26PM

I have some code which generates the html to dump an arbitrary MySQL table. And now I want to add Edit and Delete buttons to each row. But when my Edit and Delete code is invoked I need to know in which row the button was clicked. The right value to pass would seem to be the primary key. But how can my code know when it is seeing the primary key column? I suppose I could do it by position, right? I.E. if the nth column name in mysqli_fetch_field->name is the name of the primary key column then the nth value in mysqli_fetch_row will be the value of the primary key for that row. But I am hoping that there is a better way.

The table building code looks like this ...

// sending query
$result = mysqli_query($connection,"SELECT * FROM {$which_table_to_dump}");
if (!$result) {
    die("Query to show fields from table failed");
}
$fields_num = mysqli_num_fields($result);
echo "<h1>Table: {$which_table_to_dump} - with Edit and Delete buttons</h1>";
echo "<table border='1'><tr>";

// column headings for the buttons
echo "<td>Button1</td>";
echo "<td>Button2</td>";
// column headings for the table columns
for($i=0; $i<$fields_num; $i++){    
$field = mysqli_fetch_field($result);    
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result)){    
echo "<tr>";    
// $row is array... foreach( .. ) puts every element    
// of $row to $cell variable    
echo "<form action=\"handle editdelete.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"param\" value=\"somevalue\" />";  // need primary key value here !!
echo "<td><input type=submit value=\"EDIT\" name=\"edit\"></td>";
echo "<td><input type=submit value=\"DELETE\" name=\"delete\"></td>";
foreach($row as $cell) 
{
        echo "<td>$cell</td>";  
}
echo "</form></tr>\n";
}
mysqli_free_result($result);

Thanks, Bob

Options: ReplyQuote


Subject
Written By
Posted
How to determine the value of a specific column in a row?
November 07, 2012 06:26PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.