user3728747
user3728747

Reputation: 1

How to get php row values?

management.php is the code which get information in php by a table. And managementdel.php is the code which del the information. I use mysql_fetch_array to show all data,and beside every information have a herf to delete echo data in database.

When I del ,there no error inside.but database information haven't delete.

management.php

 $con1 = mysql_connect("127.0.0.1","root","password");

      mysql_select_db("babytradeapps");

      $sql1 = "Select LoginID , Password , Permission
              from loginacc where Permission = 2 ";


      $results = mysql_query($sql1,$con1);

       echo "<tr><th>會員帳號</th></tr>";

      echo "<table border=5 cellpadding=10>";

      echo "<tr><th></th><th>帳號</th><th>密碼</th><th>權限</th><th></th><th></th></tr>";

      while($row = mysql_fetch_array($results)) { 

       echo "<tr><td>
        <a href='searchtable.php?lid=$row[0]'>get information</a></td><td>$row[0]</td><td><input type=text id='row1' name='row1' value='$row[1]' /></td>
                  <td>$row[2]</td><td>
                  <a href='searchtable.php?lid=$row[0]'>Change</a></td><td>
                  <a href='managementdel.php?lid=$row[0]'>Delete</a></td></tr>";

      }

      echo "</table>";

managementdel.php

<?php

$ac = $_GET['rowname'];

 $con = mysql_connect("127.0.0.1","root","password");

  mysql_select_db("babytradeapps");

  $sql = "delete from loginacc where LoginID = '$ac'";   




  if(mysql_query($sql))

  {
               echo '<meta http-equiv=REFRESH CONTENT=2;url=management.php>';

        }
        else
        {
                echo 'fail!';
                echo '<meta http-equiv=REFRESH CONTENT=2;url=management.php>';
        }


echo mysql_error()
?>

Upvotes: 0

Views: 87

Answers (3)

Darren
Darren

Reputation: 13128

There is so much stuff wrong with your script, I won't address it all. What I will do is answer your question that you asked which is: "Why it won't delete from database."

What is wrong in your script:

  • Using depreciated mysql_* library (See notes below)
  • Using meta refresh to redirect instead of something like header('Location: link');
  • Not sanitizing user input -> $_GET['lid'].
  • Posting user password in the table. (Hopefully not being stored as plaintext)

As stated, you're trying to get:

$_GET['rowname']

When you are sending lid -> managementdel.php?lid=$row[0]. You have to change that to:

$ac = $_GET['lid'];

NOTES

Please stay away from mysql_* functions as the library is depreciated.

Use either of the following two instead:

And if you aren't going to do that, atleast try and sanitize your user inputs to prevent SQL Injections.

Using functions like intval() and mysql_real_escape_string() will help you but won't be as comprehensive as PDO/mysqli.

Upvotes: 1

Shi Wei
Shi Wei

Reputation: 272

Try

management.php

<?php
 $con1 = mysql_connect("127.0.0.1","root","password");

      mysql_select_db("babytradeapps");

      $sql1 = "Select LoginID , Password , Permission
              from loginacc where Permission = 2 ";


      $results = mysql_query($sql1,$con1);

       echo "<tr><th>會員帳號</th></tr>";

      echo "<table border=5 cellpadding=10>";

      echo "<tr><th></th><th>帳號</th><th>密碼</th><th>權限</th><th></th><th></th></tr>";

      while($row = mysql_fetch_array($results)) { 

       echo "<tr><td>
        <a href='searchtable.php?lid= " . $row[0] . "'>get information</a></td><td>" . $row[0] . "</td><td><input type=text id='row1' name='row1' value='" . $row[1] . "' /></td>
                  <td>" . $row[2] . "</td><td>
                  <a href='searchtable.php?lid=" . $row[0] . "'>Change</a></td><td>
                  <a href='managementdel.php?lid=" . $row[0] . "'>Delete</a></td></tr>";

      }

      echo "</table>";

?>

managementdel.php

<?php

$ac = $_GET['lid'];

 $con = mysql_connect("127.0.0.1","root","password");

  mysql_select_db("babytradeapps");

  $sql = "delete from loginacc where LoginID = '$ac'";   




  if(mysql_query($sql))

  {
               echo '<meta http-equiv=REFRESH CONTENT=2;url=management.php>';

        }
        else
        {
                echo 'fail!';
                echo '<meta http-equiv=REFRESH CONTENT=2;url=management.php>';
        }


echo mysql_error()
?>

Upvotes: 0

rack_nilesh
rack_nilesh

Reputation: 553

in managementdel.php file instead of

$ac = $_GET['rowname'];

there should be

$ac = $_GET['lid'];

Upvotes: 0

Related Questions