简体   繁体   中英

html form inside php loop

<?php
    try{
        include("dbconnectie.php");
        $query = $db->prepare("SELECT * FROM shop WHERE id_u = :id");
        $query->bindParam("id", $_SESSION['id_u']);
        $query->execute();
        $result = $query->fetchALL(PDO::FETCH_ASSOC);
        echo "<table>";
            foreach($result as &$data) {
                echo "<tr>";
                    $img = $data['img_url'];
                    echo "<td>" . $data["brand"] . "</td>";
                    echo "<td>" . $data["model"] . "</td>";
                    echo "<td> Condition: " . $data["cond"] . "/100 </td>";
                    echo "<td> Prijs: &dollar; " . number_format($data["price"],2,",",".") . "</td>";
                    echo "<td> <img src='$img' width='400' height='300' ></img> </td>";
                    echo "<td>" . "<form method="post" action"">" . 
                    "<input type="submit" name="delete" value="$data['id_img']">" . "</form>" . "</td>";
                echo "</tr>";
            }
        echo "</table>";
    } catch(PDOException $e) {
        die("Error!: " . $e->getMessage());
    }
?>
<html>
    <body>
    </body>
</html>

on line 17 and line 18 i'm trying to make a button for every time it loops so that i can delete those posts out of my database, but i'm unsure on how to make it loop the button making because it doesn't work how i wrote it.

This is your problem: At line 17 you have this:

echo "<td>" . "<form method="post" action"">" . 
                "<input type="submit" name="delete" value="$data['id_img']">" . "</form>" . "</td>";

instead you should have this:

echo "<td>" . "<form method='post' action=''>" . 
                "<input type='submit' name='delete' value='".$data['id_img']."'>" . "</form>" . "</td>";

OR:

echo '<td>' . '<form method="post" action="">' . 
                '<input type="submit" name="delete" value="'.$data['id_img'].'">' . '</form>' . '</td>';

You have a couple formatting errors. First, if you want the elements to show up on the page, the php echo needs to occur w/in the tags like so:

<html>
<body>
<?php
    PHP STUFF HERE
?>
</body>
</html>

Second, HTML tags must always use double quotes in their syntax, so PHP code that echos them must either escape them, or the string specifying the must be in single quotes. Eg:

<?php
    // bad
    echo "<img src="http://myimage.com/funny.jpg">";

    // bad
    echo "<img src='http://myimage.com/funny.jpg'>";

    // good
    echo '<img src="http://myimage.com/funny.jpg">';
?>

PHP doesn't know what to do with the strings you are telling it to echo because they are improperly formatted. Notice in the syntax highlighting above how the first bad example doesn't show the string all highlighted the same, whereas the good example does. If you aren't already, I recommend switching to a text editor with syntax highlighting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM