greenpool
greenpool

Reputation: 561

php how to add edit button inside a table and load data to edit form

what i am trying to do: loading data from mysql database in a table with edit buttons.

what is going wrong: when clicking on edit buttons it doesn't load the data on the edit form.

what i have tried: i tried the following code at home on Firefox and it worked perfectly. but at work it fails to load the data in Firefox in the edit form and in IE clicking the button does nothing.

<?php
echo '<table id="viewAll">';
            echo '<tr>';
            echo '<th>Product</th>';
            echo '<th>Status</th>';
            echo '<th>Summary</th>';
            echo '<th>Created</th>';
            echo '<th>Updated</th>';
            echo '<th>Edit</th>';

            echo '</tr>';


            while ($data=mysqli_fetch_array($result)){

            echo '<tr align="center">

            <td>'.$data['product'].'</td>
            <td>'.$data['status'].'</td>
            <td align="left">'.$data['summary'].'</td>
            <td>'.$data['created'].'</td>
            <td >'.$data['updated'].'</td>';

            echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\"><input type=\"submit\" value=\"Edit\" /></a></td>";

            echo '</tr>';


                }#end of while


            }#end of if



echo '</table>';
?>

and then i do:

$id=$_GET['id'];



            $query="SELECT * FROM tickets WHERE id='$id'";


            $result = mysqli_query($db,$query) or die( "My query ($query) generated an error: ".mysql_error());

            $data=mysqli_fetch_array($result);





            mysqli_close($db);

I use the contents in the data array to load it into the field in the edit form..

unfortunately this doesn't work in IE. clicking edit button doesn't do anything. Can somebody please tell me how to fix it? Thanks.

Upvotes: 0

Views: 27059

Answers (5)

uınbɐɥs
uınbɐɥs

Reputation: 7341

Try using this:

echo "<td><form action=\"editForm.php\" method=\"get\"><input type=\"hidden\" name=\"id\" value=\"$data['id']\" style=\"text-decoration: none\" /><input type=\"submit\" value=\"Edit\" /></form></td>";

That will mean you can still use a button.

Also, I strongly suggest if you are not using tables for tabular data, find an alternative, such as <div>s.

And remember to sanitise your SQL queries, or you may be susceptible to SQL injection attacks. (e.g. $id=$_GET['id']; => $id=mysqli_real_escape_string($conn, $_GET['id']); where $conn is your MySQL connection object).

You can search for any of these things on Google.

Upvotes: 2

Tepken Vannkorn
Tepken Vannkorn

Reputation: 9713

You should not use <input type="submit" value="edit" name="edit" />.

Only "<a href='editForm.php?id='".$data['id']."' style='text-decoration: none' />Edit</a>" would be enough in sending you the data to the editForm.php page.

PS: submit button can only be used to submit all form elements within the form.

Upvotes: 2

Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

Or you could do this in a nice way with jQuery. Try reading this.

Upvotes: 0

jbrtrnd
jbrtrnd

Reputation: 3833

In the while loop in the first PHP script, the line which echoes the edit button is wrong.
You have to use the concatenate function . and quotes to get the value in $data["id"]
Your line echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\">Edit</a></td>"; has to be replaced by :

echo "<td><a href=\"editForm.php?id=".$data['id']."\" style=\"text-decoration: none\">Edit</a></td>";

Upvotes: 0

j_freyre
j_freyre

Reputation: 4738

Correct the line where you write the button into this way

echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\">Edit</a></td>";

Clicking the button try to submit a form. But what you want to do is access the page editForm.php

PS: You should read some tutorials about PHP and html to learn best practice ;)

Upvotes: 1

Related Questions