2 Replies - 173 Views - Last Post: 13 December 2017 - 10:52 AM Rate Topic: -----

#1 thomascr9695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-December 17

Delete row in table

Posted 13 December 2017 - 01:33 AM

Hi there,

I'm trying to get a simple delete button in my table for deleting a row by ID. I've searched on the internet and tried this but is seems it doesn't work. I get no errors so I'm not sure why it's not working. Could anyone tell me what is wrong with me code?

<form action="" method="post">
</form>
<form action="invullen.php" enctype="multipart/form-data" method="post">
    <input type="text" name="fname" required placeholder="First Name"><br><br>
    <input type="text" name="desc" required placeholder="desc"><br><br>
    <input type="text" name="prijs" required placeholder="prijs"><br><br>
    <input name="image" type="file"><input name="submit" type="submit" value="Upload">
</form>
<?php
$db_server = "localhost";
$db_username = "root";
$db_password = "";
$db_database = "meubelfabriek";
$conn = new PDO("mysql:host=$db_server;dbname=$db_database", $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['submit']))
{
    $prijs = $_POST['prijs'];
    $naam = $_POST['fname'];
    $beschrijving = $_POST['desc'];
    $imageName = $_FILES["image"]["name"];
    $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
    $imageType = $_FILES["image"]["type"];
    if(substr($imageType,0,5)=="image")
    {
        $stmt = $conn->prepare("INSERT INTO producten ( naam, prijs, beschrijving, namen, image ) VALUES (  :naam, :prijs, :beschrijving,'$imageName', " . $conn->quote($imageData) . ")");
        $stmt->execute(array(":naam"=>$naam, ":beschrijving"=>$beschrijving, ":prijs"=>$prijs));;
    }
    else
    {
        echo "only images are allowed";    }
}
?>
<?php
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("meubelfabriek");
    $result = mysql_query("SELECT * FROM producten") or die(mysql_error());
?>
<table border="1" cellpadding="5" cellspacing="5">
    <tr> <th>Meubelen - Tafels </th></tr>
    <?php
    while($rowz = mysql_fetch_array($result)) {
    $id = $rowz['id'];
    ?>
    <td>
        <img src="data:image/jpeg;base64,<?php echo base64_encode($rowz['image']); ?>" width="125"
             height="125"/>
        <a class="formtitellink" href="remove.php?id=rowid=<?= $rowz['ID'] ?>">  <?php echo '<h7>' . $rowz['naam'] . '</h7></a>
        <input type="submit" name="delete" value="Delete">
';

if (isset($_POST['delete'])) {
    $conn = sprintf("DELETE FROM producten WHERE id = '".$_GET['id']."'");
    $result = mysql_query($sql);
    } ?>

            <?php } ?>
</table>
</div>


Is This A Good Question/Topic? 0
  • +

Replies To: Delete row in table

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4139
  • View blog
  • Posts: 13,089
  • Joined: 08-June 10

Re: Delete row in table

Posted 13 December 2017 - 01:49 AM

Quote

Could anyone tell me what is wrong with me code?

You have a submit button but no form to submit.

And another thing, never ever use a link or any other GET request to trigger a delete. Browsers tend to cache such requests which may cause accidental delete.
Was This Post Helpful? 0
  • +
  • -

#3 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 142
  • Joined: 28-March 15

Re: Delete row in table

Posted 13 December 2017 - 10:52 AM

Whats wrong with it? How about starting with mixing PDO with obsolete and dangerous mysql_* code and the SQL Injection vulnerabilities.

Stop what your doing and take a minute to learn how to properly use PDO. PDO Tutorial

This post has been edited by benanamen: 13 December 2017 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1