INSERT准备语句PDO + WHERE

时间:2017-06-19 18:27:34

标签: php mysql pdo

我有一个脚本,script1.php从我的数据库中获取数据,并且脚本几乎相同,但在我的数据库中搜索选项script2.php

script1.php按下删除按钮或个人资料按钮时,我会得到所需的结果。行被删除或我得到一个包含该特定行数据的个人资料。

script2.php中,当我按下删除按钮时,我的行被删除但是在使用脚本1时我得不到更多结果,所有结果都保留在页面上。个人资料按钮根本不起作用,我不明白为什么。我会嘲笑一个很好的探索,所以我现在为未来。

SCRIPT1

<head>
<title>AJ Recruit - Kandidaten Database</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>AJ Recruit - Kandidaten Database</label>
</div>
<div id="body">
    <table width="80%" border="1">
    <tr>
    <th colspan="9"><form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Zoek" />
    </form></th>
    </tr>
    <tr>
    <th colspan="9"><label><a href="test.php">Voeg nieuwe kandidaat toe</a></label></th>
    </tr>
    <tr>
    <td>ID</td>
    <td>geslacht</td>
    <td>Voornaam</td>
    <td>Familienaam</td>
    <td>email</td>
    <td>Telefoon</td>
    <td>Bekijk CV</td>
    <td>Datum</td>
    <td>Profiel</td>
    <td>Verwijder</td>

    </tr>
<?php

$servername = "xxxxxx";
$username = "xxxxxx";
$password = "xxxxx";
$dbname = "xxxxxx";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT id, geslacht, voornaam, familienaam, email, tel, afile, app_datum FROM sollicitatie_form"); 
    $stmt->bindParam(':id', $id,  PDO::PARAM_STR);
    $stmt->bindParam(':geslacht', $geslacht,  PDO::PARAM_STR);
    $stmt->bindParam(':voornaam', $voornaam,  PDO::PARAM_STR);
    $stmt->bindParam(':familienaam', $familienaam,  PDO::PARAM_STR);
    $stmt->bindParam(':email', $email,  PDO::PARAM_STR);
    $stmt->bindParam(':tel', $tel,  PDO::PARAM_STR);
    $stmt->bindParam(':tel', $afile,  PDO::PARAM_STR);
    $stmt->bindParam(':app_datum', $app_datum,  PDO::PARAM_STR);
    $stmt->execute();

    $result = $stmt->fetchAll();

    foreach ($result as $row){
        ?>

        <tr>
            <td><?php echo "{$row['id']}"; ?></td>
            <td><?php echo "{$row['geslacht']}"; ?></td>
            <td><?php echo "{$row['voornaam']}"; ?></td>
            <td><?php echo "{$row['familienaam']}"; ?></td>
            <td><?php echo "{$row['email']}"; ?></td>
            <td><?php echo "{$row['tel']}"; ?></td>
            <td><a href="uploads/<?php echo "{$row['afile']}" ?>" target="_blank">view file</a></td>
            <td><?php echo "{$row['app_datum']}"; ?></td>
            <td class= "contact-profiel">
                <form action="profile.php"<?php echo "{$row['id']}"; ?> method="post">
                    <input type="hidden" name="idprofile" value="<?php echo "{$row['id']}"; ?>">
                    <input type="submit" name="submit" value="Profiel">
                </form>
            </td>
            <td class= "contact-delete">
                <form action="delete.php"<?php echo "{$row['id']}"; ?> method="post">
                    <input type="hidden" name="id" value="<?php echo "{$row['id']}"; ?>">
                    <input type="submit" name="submit" value="Delete">
                </form>
            </td>

        </tr>
        <?php

    }



}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
?>

脚本2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" /></meta>

<head>
    <title>Zoek Formulier</title>
    <link href="styletest.css" rel="stylesheet">
</head>
<body>

<div class="table">

    <div class="tab-content">
        <h1>Zoek Formulier</h1>

        <form method="post" >

            <div class="top-row-search">
                <div class="field-wrap">
                    <label>Zoek Voornaam</label>
                    <input type="text" name="zoekvn" value="<?= isset($_POST['voornaam']) ? $_POST['voornaam'] : ''; ?>">
                </div> <!-- /field-wrap-->

                <div class="field-wrap">
                    <label>Zoek Familienaam</label>
                    <input type="text" name="zoekfn" value="">
                </div> <!-- /field-wrap-->

                <div class="field-wrap">
                    <label>Zoek email</label>
                    <input type="text" name="zoekem" value="">
                </div> <!-- /field-wrap-->

                <div class="field-wrap">
                    <label>Zoek tel</label>
                    <input type="text" name="zoektel" value="">
                </div> <!-- /field-wrap-->                                         

            </div> <!-- /top-row -->

            <button type="submit" class="button button-block" name="submit" />Zoek</button>

        </form> 

    </div><!-- /tab-content-->
</div><!-- /table-->

<br>

<div class="table">

    <div class="tab-content">
        <h1>Resultaten</h1>

        <form>

            <div id="body">
                <table width="100%" border="1">
                <tr>
                <th colspan="10">
                    <form action="search.php" method="post">
                        <input type="text" name="query" />
                        <input type="submit" value="Zoek" />
                    </form></th>
                </tr>
            <tr>
                <th colspan="10" color="#fff"><label><a href="test.php">Voeg nieuwe kandidaat toe</a></label></th>
            </tr>
            <tr>
                <td><input name="delete" type="submit" id="delete" value="Delete selected"></td></td>
                <td>ID</td>
                <td>geslacht</td>
                <td>Voornaam</td>
                <td>Familienaam</td>
                <td>email</td>
                <td>Telefoon</td>
                <td>Bekijk CV</td>
                <td>Datum</td>
                <td>Profiel</td>
                <td>Verwijder</td>
            </tr>

        </form>

    </div><!-- /tab-content-->
</div><!-- /table-->
</body>
</html>


<?php

$servername = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
$dbname = "xxxxx";

if(isset($_POST['submit'])):

    try{
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


        $stmt = $conn->prepare("SELECT id, geslacht, voornaam, familienaam, email, tel, afile, app_datum FROM sollicitatie_form WHERE voornaam = ? or familienaam = ? or email = ? or tel = ?");

        $stmt->execute(array($_POST['zoekvn'], $_POST['zoekfn'], $_POST['zoekem'], $_POST['zoektel']));

        $result = $stmt->fetchAll();

        foreach ($result as $row){?>

        <form>

            <tr>
            <td><input name="checkbox[]" type="checkbox" value="<?php echo $row['id']; ?>"></td>
            <td><?php echo "{$row['id']}"; ?></td>
            <td><?php echo "{$row['geslacht']}"; ?></td>
            <td><?php echo "{$row['voornaam']}"; ?></td>
            <td><?php echo "{$row['familienaam']}"; ?></td>
            <td><?php echo "{$row['email']}"; ?></td>
            <td><?php echo "{$row['tel']}"; ?></td>
            <td><a href="uploads/<?php echo "{$row['afile']}" ?>" target="_blank">view file</a></td>
            <td><?php echo "{$row['app_datum']}"; ?></td>
            <td class= "contact-profiel">
                <form action="profile.php"<?php echo "{$row['id']}"; ?> method="post">
                    <input type="hidden" name="idprofile" value="<?php echo "{$row['id']}"; ?>">
                    <input type="submit" name="submit" value="Profiel">
                </form>
            </td>
            <td class= "contact-delete">
                <form action="delete.php"<?php echo "{$row['id']}"; ?> method="post">
                    <input type="hidden" name="id" value="<?php echo "{$row['id']}"; ?>">
                    <input type="submit" name="submit" value="Delete">
                </form>
            </td>

        </tr>

        </form>


        <?php
        }
    }
        // use exec() because no results are returned
        catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage();
        }

$conn = null;
endif;
?>

0 个答案:

没有答案