Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 07:08 PM   PM User | #1
Wilhelmina
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Wilhelmina is an unknown quantity at this point
Question Allow user to edit and delete row

I've created a table with movies and a webpage where you can add more and view all of them. Now I'm trying to add if statements for if the edit or delete link has been clicked. Here is the code so far:

PHP Code:
<?php // movielibrary.php
require_once 'login.php';
$query "SELECT * FROM library";

$result mysql_query($query);
if (!
$result) die ("Database access failed: " mysql_error());
$rows mysql_num_rows($result);

echo 
"<table border='1'><tr> <th>Title</th>
        <th>Director</th>
    <th>Year</th><th>Category</th>
        <th>Update</th><th>Delete</th></tr>"
;

for (
$j $j $rows ; ++$j)
{
    
$row mysql_fetch_row($result);
    echo 
"<tr><td>$row[1]</td><td>$row[2]</td> 
        <td>$row[3]</td><td>$row[4]</td>
    <td><a href='movielibrary.php?edit=$row[0]'>Edit</a</td>
        <td><a href='movielibrary.php?delete=$row[0]'>Delete</a></td></tr>"
;
}

echo 
"</table>";

mysql_close($db_server);
?>
So my question is: how could I test the condition of if one of the links has been clicked? Thanks beforehand!
Wilhelmina is offline   Reply With Quote
Old 10-09-2012, 08:42 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I don't think you should try to do edit in the same page. Editing a record is so different it deserves its own page. So I would change
Code:
<a href='movielibrary.php?edit=$row[0]'>Edit</a>
to
Code:
<a href='movieedit.php?id=$row[0]'>Edit</a>
or something along those lines.

Delete, though, is easy:

I don't use PHP, but something like this:
Code:
<?php // movielibrary.php 
require_once 'login.php'; 

$deleteWhich = $_GET["delete"];
if ( isset($deleteWhich ) ) 
{
    $sql = "DELETE FROM library WHERE id = " . ( (int) $deleteWhich );
    mysql_query( $sql ) or die( mysql_error() );
}

... rest of page same ...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Wilhelmina (10-10-2012)
Old 10-10-2012, 05:19 PM   PM User | #3
Wilhelmina
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Wilhelmina is an unknown quantity at this point
Thank you Old Pedant for your reply!

I would prefer having the edit on the same page though because once clicked the form for adding a new movie will change to a replicate of the form but with the details of the movie clicked to edit already in place. Could i not use the action attribute in the form to a seperate script where the UPDATE will take place? (Sorry, as you might have noticed I'm quite new to this.) Here is what the code looks like now:

PHP Code:
<?php // movielibrary.php
require_once 'login.php';
$query "SELECT * FROM library";
$result mysql_query($query);
if (!
$result) die ("Database access failed: " mysql_error());
$rows mysql_num_rows($result);

echo 
"<table border='1'><tr> <th>Title</th><th>Director</th>
    <th>Year</th><th>Category</th><th>Update</th><th>Delete</th></tr>"
;

for (
$j $j $rows ; ++$j)
{
    
$row mysql_fetch_row($result);
    echo 
"<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>
    <td><a href='movielibrary.php?edit=$row[0]'>Edit</a</td><td><a href='movielibrary.php?delete=$row[0]'>Delete</a></td></tr>"
;
}

echo 
"</table>";

if(isset(
$_GET['edit']))
{
echo <<<_END
<form action="update.php" method="post">
Choose a category:<br />
<input type="hidden" name="id" value="$row
[0]" />
<input type="radio" name="category" value="$row
[4]" />Thriller<br />
<input type="radio" name="category" value="$row
[4]" />Romantic<br />
<input type="radio" name="category" value="$row
[4]" />Swedish<br />
<input type="radio" name="category" value="$row
[4]" />Animated<br />
<input type="radio" name="category" value="$row
[4]" />Comedy<br /><br />
   Title <input type="text" name="title" value="$row
[1]" /><br />
Director <input type="text" name="director" value="$row
[2]" /><br />
    Year <input type="text" name="year" value="$row
[3]" /><br />
         <input type="submit" value="CHANGE" />
</form>
_END;
}

elseif(isset(
$_GET['delete'])){
   
$delete $_GET['delete'];
   
$query mysql_query("DELETE FROM library WHERE id='$row[0]'")or die(mysql_error());
   
header("Location: movielibrary.php"); 
}

else
{
echo <<<_END
<form action="store.php" method="post">
Choose a category: <br />
<input type="hidden" name="id" />
<input type="radio" name="category" value="Thriller" />Thriller<br />
<input type="radio" name="category" value="Romantic" />Romantic<br />
<input type="radio" name="category" value="Swedish" />Swedish<br />
<input type="radio" name="category" value="Animated" />Animated<br />
<input type="radio" name="category" value="Comedy" />Comedy<br /><br />
   Title: <input type="text" name="title" /><br />
Director: <input type="text" name="director" /><br />
    Year: <input type="text" name="year" /><br />
         <input type="submit" value="STORE" />
</form>
_END;
}

mysql_close($db_server);
?>
How can I get the values from the row with the id in the edit link to show in the edit form?

Thanks beforehand,
Wilhelmina
Wilhelmina is offline   Reply With Quote
Old 10-10-2012, 08:25 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Of course you can put it on the same page. Just complicates things a little bit.

But you need to do a *SEPARATE* query to get the data for the edit.

Just as I put the query for the DELETE *before* the SELECT * so you would put the query for the edit before the main query and main display. (Or after the main display, if you prefer.)

Your code WILL NOT WORK as is. Because you do the DELETE *after* you do the SELECT * you will get *ALL* the records *AGAIN*. And then, after displaying them all, you do the DELETE. Too late to change the display.

The DELETE *must* be the first code on the page. The EDIT can be either second or last.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 AM.


Advertisement
Log in to turn off these ads.