user3346678
user3346678

Reputation: 19

edit row from mysql

Ok, I'm not very good at php but trying to learn as much as possible. So I've made website with admin panel. In admin panel I show all rows from database with 2 buttons - 'Delete' and 'Edit'. Delete button is working but I have trouble with Edit. So here is how I show results with the buttons

for ($i = $start; $i < $end; $i++)
    {
             // make sure that PHP doesn't try to show results that don't exist
     if ($i == $total_results) { break; }
             // echo out the contents of each row into a table
        echo "<tr>";
        echo '<td><p>' . mysql_result($result, $i, 'id') . '</p></td>';
        echo '<td>' . mysql_result($result, $i, 'caption') . '</td>';
        echo '<td>' . mysql_result($result, $i, 'name') . '</td>';
        echo '<td>' . mysql_result($result, $i, 'alt') . '</td>';
        echo '<td>' . mysql_result($result, $i, 'title') . '</td>';
        echo '<td><a href="delete.php?id=' . mysql_result($result, $i, 'id') . '">Delete</a></td>';
        echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';

        echo "</tr>";
    }

When I click on Edit page goes to edit.php and url is with ID of choosen image. For ex (/edit.php?id=68). Here is edit.php

<form action="" method="post" enctype="multipart/form-data">
            Choose category
         <select name="img_category">
            <option value="1">Cars</option>
            <option value="2">Animals</option>
            <option value="3" >PC's</option>
            <option value="4" >Sport</option>
        </select><br/><br />
            Caption
            <input type="text" name="caption" /><br /><br />
            Alt
            <input type="text" name="alt" /><br /><br />
            Title
            <input type="text" name="title" /><br /><br />
            <input type="submit" name="submit" id="submit" value="Edit" /><br /><br />
</form>
<?php
if (isset($_POST['submit']))
{
    require_once("../include/db.php");
    $id =$_POST['id'];
    $caption = $_POST['caption'];
    $title = $_POST['title'];

    $query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";

    $result = mysqli_query($con, $query) or die("Error in query: ".mysqli_error($con));
}
?>

I want to be able to edit caption, alt and title of the image. Now when I press 'Edit' nothing happen. I'm sure is not so hard but for me is kind of.

Upvotes: 0

Views: 286

Answers (5)

L7Lynx
L7Lynx

Reputation: 103

In your sql-statement, you don't seem to add in your id.

 $query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = 'id'";

You should add the $

$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";

Update:

There doesn't seem to be an ID field in your form. You try to retrieve it in POST, but there is no such field to retrieve data from. You should pass the id to the query through the form or an other way.

Update2:

As others have said, easiest way to do this is gonna be to get your ID from GET instead of POST.

Change your

$id =$_POST['id'];

to

$id =$_GET['id'];

Upvotes: 4

Boopathi Rajan
Boopathi Rajan

Reputation: 1210

try this

<?php
if (isset($_POST['submit']))
{
    require_once("../include/db.php");
    $id =$_GET['id'];
    $caption = $_POST['caption'];
    $title = $_POST['title'];

    $query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";

    $result = mysqli_query($con, $query) or die("Error in query: ".mysqli_error($con));
}
?>

Upvotes: 1

snitch182
snitch182

Reputation: 723

Add an

<input type="hidden" name="id"  value='<?=(int)$_GET['id'] ?>'/>

somewhere in the form.

Have to add: there are a couple of security issues with your code. Remember your code is not production ready! But that is not your question so i wont go into that.

Upvotes: 2

user1825814
user1825814

Reputation: 51

You are trying to retrieve a $_GET value with a $_POST, which would leave the $id variable empty.

Change the $id =$_POST['id']; to $id = $_GET['id'];

Also change your query to

$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";

Upvotes: 2

Dinesh
Dinesh

Reputation: 4110

use GET method to catch id from url

just change

$id =$_POST['id'];

to

$id =$_GET['id'];

Upvotes: 2

Related Questions