1

I dont now where my mistake is, my code is as follows:

<?php

mysql_set_charset('utf8');
$result = mysql_query("SELECT * FROM obedy ORDER BY datum DESC LIMIT 30");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
{
    $time = strtotime( $row[0] );
    $myDate = date( 'd.m.', $time );
    $w_day = date( 'N', $time );
    $ww_day = $w_day-1;
    $the_row= $row[0];

    echo "<p>" . $myDate . "&emsp;";
    echo "<input type='hidden' value='$row[0]' name='tdel'>";
    echo "<input type='text' name='menu1' id='menu1' class='input' value='". $row[1] ."' size='37'/>";
    echo "<input type='text' name='menu2' id='menu2' class='input' value='". $row[2] ."' size='37'/>";
    echo "<input type='text' name='menu3' id='menu3' class='input' value='". $row[3] ."' size='37'/>";
    echo "<input type='submit' formaction='del_menu.php' class='button' value='Smazat' />";
    echo "</p>";                    
}

mysql_free_result($result);

?>

del_menu.php

<?php

$huh = mysql_connect("juxcore.ipagemysql.com", "*", "*") or
die("Could not connect: " . mysql_error());

mysql_select_db("jux_mms");
mysql_set_charset('utf8');

$watta="DELETE FROM obedy WHERE datum = '$_POST[tdel]'";

if (!mysql_query($watta,$huh))
{
    die('Error: ' . mysql_error());
}

header('Location: http://www.juxcore.com/x/vita/menu.php');

The thing is, I don't know why it deletes the last displayed row, instead the one clicked. Any ideas how to solve that?

6
  • 2
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Oct 24, 2012 at 18:04
  • 1
    Your form is going to have multiple entries called tdel. When you hit submit, they're all sent to the server, and each one over-rides the preceding one, so the last one is the one that's processed. Commented Oct 24, 2012 at 18:06
  • Are you wrapping each separate form in a <form> tag? Commented Oct 24, 2012 at 18:06
  • @lc. that's where I got confused, he is using HTML5 formaction='del_menu.php' Commented Oct 24, 2012 at 18:07
  • 2
    @user1505027 - that's not in your code. Are you wrapping that whole while loop in a single form? Commented Oct 24, 2012 at 18:10

1 Answer 1

6

You should use <form> tags:

echo "<form action='del_menu.php' method='post'><p>" . $myDate . "&emsp;";
echo "<input type='hidden' value='".$row[0]."' name='tdel'>";
echo "<input type='text' name='menu1' id='menu1_".$row[0]."' class='input' value='". $row[1] ."' size='37'/>";
echo "<input type='text' name='menu2' id='menu2_".$row[0]."' class='input' value='". $row[2] ."' size='37'/>";
echo "<input type='text' name='menu3' id='menu3_".$row[0]."' class='input' value='". $row[3] ."' size='37'/>";
echo "<input type='submit'  class='button' value='Smazat' />";
echo "</p></form>";

The problem is that you can only have one item with the same name in one form. If you use multiple items with the same name, only the last one defined will get trough. You can use multiple forms to solve that.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I see, I should place them INSIDE the loop. Thanks a lot :)
if you put <form> there you are including it in the loop

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.