0

THIS IS MY CODE

    <?php
$conn=mysqli_connect('localhost','root','','admin');
$sql="SELECT * FROM admin";
$result=mysqli_query($conn,$sql);
while($row=$result->fetch_assoc())
{
    $id=$row['id'];
    $name=$row['name'];
    $email=$row['email'];   
?>
<table>
        <tr><td><?php echo $id ?></td><td><?php echo $name ?></td><td><?php echo $email ?></td><td><form action="111.php" method="post"><input type="hidden" name="row_id" id="row_id"> value="<?php echo $id ?>"><input type="submit" name="edit" id="row_id" value="edit"></form></td><td><form action="111.php" method="post"><input type="submit" name="delete" value="delete"></form></td></tr>

</table>
<?php
}
?>

another php file after click button

<?php
if(isset($_POST['edit']))
{
    $id=$_POST['row_id'];
    echo $id;
}

if(isset($_POST['delete']))
{
    $id=$_POST['row_id'];
    echo $name;
}
?>

My OUTPUT IS

1   aaa aaaaa value="1">edit(button) delete(button)
2   bbb bbbbbb  value="2">edit(button) delete(button)
3   ccc cccccc  value="3">edit(button) delete(button)

when i click on edit button OR delete button no print the value of id MY EXPECTED OUTPUT IS

when click button only specific id print
Mazhar Hussain
  • 125
  • 1
  • 11

2 Answers2

1

Currently you don't have a <form>, so there's nothing to "submit" when clicking the button. Wrap the button in a form, and include in that form an element with the id value:

<td>
    <form method="POST" action="yourPHPScript.php">
        <input type="hidden" name="id" value="<?php echo $row['id'] ?>">
        <input type="submit" name="delete" value="delete">
    </form>
<td>

When this form is posted, $_POST['id'] will contain the value you're looking to bind to your query.

Note: You're also creating multiple tables in your code. Are you sure you want to do that? Or, rather, just multiple rows within a table? Structurally that would be more like this:

<table>
    <?php while($row = $result->fetch_assoc()) ?>
        <tr>
            ....
        </tr>
    <?php } ?>
</table>

Additionally, your DELETE query shouldn't be specifying columns. You delete an entire row, not specific values from that row:

DELETE FROM user WHERE id=?
David
  • 208,112
  • 36
  • 198
  • 279
  • I would suggest to not create multiple tables, just one with multiple rows / forms. [Incorrect html](https://stackoverflow.com/questions/48420995/without-checkbox-i-want-to-delete-specific-value-on-delete-button#comment83832082_48420995) is also present in the code, but that's not as important as – FirstOne Jan 24 '18 at 11:28
  • The [query is wrong](https://stackoverflow.com/questions/48420995/without-checkbox-i-want-to-delete-specific-value-on-delete-button#comment83831813_48420995), so you might want to mention that in the answer. – FirstOne Jan 24 '18 at 11:29
  • i use this code but if you see my above output of my question 2 delete button first button working only but second not working using your code why – Mazhar Hussain Jan 24 '18 at 11:32
  • @MazharHussain: Define "not working". What code did you write and in what way does it fail? We need to see the code/problem in order to help. – David Jan 24 '18 at 11:33
  • same your code i am trying but only first button working and second not working or even not change page on second – Mazhar Hussain Jan 24 '18 at 11:36
  • @MazharHussain: I’m afraid nobody can help you without knowing anything about the problem. It seems possible, even likely, that your updated code has a mistake in it. But if you won’t show that code then we can’t know that mistake. – David Jan 24 '18 at 11:40
  • their edit contains a parse error and they *think* their connection is correct; it isn't. I feel the question needs a total rewrite and you may have been chased down a rabbit hole here. – Funk Forty Niner Jan 24 '18 at 11:57
  • see my connection i again write ok i thing you are not know the answer of this question – Mazhar Hussain Jan 24 '18 at 12:07
  • @MazharHussain: For one thing, your updated code has a SQL injection vulnerability. You should certainly correct that. The HTML looks potentially invalid too. The form should be inside the td, I’m not sure if it’s valid inside just the tr. You should check how the browser is interpreting the DOM on that and what the POST request ends up containing. As for how it’s failing, specifically what happens when you debug? The code you’re showing does indeed have a parser error. Have you checked the PHP logs? – David Jan 24 '18 at 12:19
  • *"i thing you are not know the answer of this question"* - @MazharHussain David knows his stuff quite well. You're the one who's not cooperating by telling him what errors you're getting. Why is that? Because.... you're **NOT** checking for them and I seem to have fallen onto deaf ears in comments I placed under your post. This type of method isn't so hard and there are plenty of examples out there but you seem to want to stick to what you're using now. – Funk Forty Niner Jan 24 '18 at 12:21
  • in this site some code missing but in my file all connections are ok and if you wise then thing if my connection is not ok how first button working and second not working so my issue is only button related all connection is ok just its writing mistake on thi site – Mazhar Hussain Jan 24 '18 at 12:21
  • @MazharHussain: Are you arguing with me or with the other user in this thread? Making use of the `@username` construct in comments clarifies the dialogue. Either way, please focus on the problem being solved. How specifically is the code failing? "It doesn't work" doesn't tell us anything about the problem. – David Jan 24 '18 at 12:28
-1
(1.) @funk-forty-niner is Correct. 
     Your Connection Query is Wrong.It is mysql_connect();

   <?php
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db('db_name',$conn);
    ?>

    But How you can get output from database.

(2.). and For particular delete button, try this code.

<?php

     //Connection code
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db('db_name',$conn);

if(isset($_GET['del'])){
   $idGet = $_GET['del'];

  $del = "delete from user where id=$idGet";
   $exe = mysql_query($del);
header('location:view.php');    

}

$sql_view = "select * from user";

$exe_view = mysql_query($sql_view);
        $table="";
while($arr_view=mysql_fetch_array($exe_view)){
    $id = $arr_view['id'];          
    $name = $arr_view['name'];  


    $table.="<tr>
                <td>$id</td>
                <td>$name</td>
                <td><a href='view.php?del=$id'>DELETE</a></td>
                </tr>";
}

?>  
Hope This will Help U.
Mohit Kumar
  • 952
  • 2
  • 7
  • 18
  • 1
    `mysql_connect` has been discontinued since a long time, you shouldn't even consider using at anymore, let alone suggest anyone else to use it. See [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Oldskool Jan 24 '18 at 14:28