Deletting from a database with php

6 replies
<?php
$username="user";
$password="pass";
$database="db";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM table";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td><font face="Arial, Helvetica, sans-serif">Date Entered</font></td>
<td><font face="Arial, Helvetica, sans-serif">Referring Site</font></td>
<td><font face="Arial, Helvetica, sans-serif">Name</font></td>
<td><font face="Arial, Helvetica, sans-serif">Email Address</font></td>
<td><font face="Arial, Helvetica, sans-serif">Phone Number</font></td>
<td><font face="Arial, Helvetica, sans-serif">Type of Vehicle</font></td>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"date");
$f2=mysql_result($result,$i,"referrer");
$f3=mysql_result($result,$i,"first_name");
$f4=mysql_result($result,$i,"email");
$f5=mysql_result($result,$i,"phone");
$f6=mysql_result($result,$i,"vehicle_type_id1");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
</tr>

<?php
$i++;
}
?>
</table>


All I wanted to do was automatically add a delete button column at the end of each query for easy editing. What would need to be done for this?
#database #posting
  • Profile picture of the author WebThinker
    <?php
    $username="user";
    $password="pass";
    $database="db";

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM table";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();
    ?>
    <table border="1" cellspacing="2" cellpadding="2">
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Date Entered</font></td>
    <td><font face="Arial, Helvetica, sans-serif">Referring Site</font></td>
    <td><font face="Arial, Helvetica, sans-serif">Name</font></td>
    <td><font face="Arial, Helvetica, sans-serif">Email Address</font></td>
    <td><font face="Arial, Helvetica, sans-serif">Phone Number</font></td>
    <td><font face="Arial, Helvetica, sans-serif">Type of Vehicle</font></td>
    <td><font face="Arial, Helvetica, sans-serif">Delete</td>
    </tr>

    <?php
    $i=0;
    while ($i < $num) {

    $f1=mysql_result($result,$i,"date");
    $f2=mysql_result($result,$i,"referrer");
    $f3=mysql_result($result,$i,"first_name");
    $f4=mysql_result($result,$i,"email");
    $f5=mysql_result($result,$i,"phone");
    $f6=mysql_result($result,$i,"vehicle_type_id1");
    ?>

    <tr>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><a href="delete.php?email=<?php echo $email;?>">Delete</a></font></td>
    </tr>

    <?php
    $i++;
    }
    ?>
    </table>

    and then you write a delete.php which delete a row for a given email address. However, this can be done ONLY if you have a unique email... otherwise, you'll loose consistency.
    Usually you should have an ID field in each table.

    I would let somebody else to make it, as this way looks quite deprecated.
    {{ DiscussionBoard.errors[7295237].message }}
  • Profile picture of the author otfromtot
    this is only for admin use, it doesn't need to be anything special. I just wanted other admins to be able to delete things when needed so that it doesn't have to be done by logging in through mysql. I can set it to delete through id, since there is an auto-increment id.
    thank you though
    {{ DiscussionBoard.errors[7295276].message }}
    • Profile picture of the author cgimaster
      Originally Posted by otfromtot View Post

      this is only for admin use, it doesn't need to be anything special. I just wanted other admins to be able to delete things when needed so that it doesn't have to be done by logging in through mysql. I can set it to delete through id, since there is an auto-increment id.
      thank you though
      TIP: stop using mysql_* as it is a deprecated method and move on to either PDO or mysqli as suggested http://www.php.net/manual/en/function.mysql-query.php at the top on the red box.

      Also PDO and mysqli offer extra security for querying the database to protect yourself against injection and other malicious actions that can occur.
      {{ DiscussionBoard.errors[7295423].message }}
      • Profile picture of the author kohashi
        Agreed on upgrading to mysqli or PDO. But be aware that it doesn't stop all possible injection vectors. It protects against a lot of them by things like LIMIT $num are still unprotected I believe. So be careful even with new classes by sanitizing your data.
        {{ DiscussionBoard.errors[7317572].message }}
        • Profile picture of the author cgimaster
          Originally Posted by kohashi View Post

          Agreed on upgrading to mysqli or PDO. But be aware that it doesn't stop all possible injection vectors. It protects against a lot of them by things like LIMIT are still unprotected I believe. So be careful even with new classes by sanitizing your data.
          Thats why I love PDO you can use bindparam with field type

          PHP: PDOStatement::bindParam - Manual
          {{ DiscussionBoard.errors[7317588].message }}
  • Profile picture of the author otfromtot
    cgimaster,
    That's something I'll have to look in to, thank's for the tips. I hadn't seen anything on those until now, and hope to be able to implement it soon.
    {{ DiscussionBoard.errors[7298313].message }}

Trending Topics