2017-05-24 74 views
-1

php和sql新手。我不得不在Netbeans上运行本地数据库(使用db浏览器创建)。为什么我的SQL查询不改变我的数据库?

我的数据库连接正常,返回结果。但是当我通过界面编辑/更改结果时,它没有任何影响。我错过了什么吗?

这是我插入一个新的入口代码:

<?php 

    error_reporting(E_ALL); 
    ini_set('display_errors', 1); 

    $pdo = new PDO('sqlite:events.db'); 

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

    try { 

     $sql = "INSERT INTO events (type, name) VALUES (:eventsType, :eventsName)"; 
     //named paramaters 
     $stmt = $pdo->prepare($sql); 

     $eventstype = filter_input(INPUT_POST, 'eventstype'); 
     $stmt->bindValue(':eventstype', $eventstype, PDO::PARAM_STR); 

     $eventsName = filter_input(INPUT_POST, 'eventsName'); 
     $stmt->bindValue(':eventsName', $eventsName, PDO::PARAM_STR); 

     //$movieRating = filter_input(INPUT_POST, 'movieRating'); 
     //$stmt->bindValue(':movieRating', $movieRating, PDO::PARAM_INT); 

     $stmt->execute(); 
     $pdo = null; 

     echo "<h1>Event added to database.</h1>"; 
     echo '<a href="index.php">Return to main menu</a>'; 

    } catch (PDOException $e) { 
     //for development 
     print "We had an error: " . $e->getMessage() . "<br/>"; 
     die(); 
    } 
    ?> 

    <?php } else { ?> 

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
    Type: <input type="text" name="eventsType"><br> 
    Name: <input type="text" name="eventsName"><br> 
    //Rating: <input type="text" name="movieRating"><br> 
    <input type="submit" name="submit"> 
    </form> 

    <?php } ?> 

这是一种更新条目:

<?php 

    $pdo = new PDO('sqlite:events.db'); 


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

    try { 


     $sql = "UPDATE events SET type = :eventsType, name =:eventsName WHERE id=:movieId"; 
     //named paramaters 
     $stmt = $pdo->prepare($sql); 

     $eventsId = filter_input(INPUT_POST, 'eventsId'); 
     $stmt->bindValue(':eventsId', $eventsId, PDO::PARAM_INT); 

     $eventsType = filter_input(INPUT_POST, 'eventsType'); 
     $stmt->bindValue(':eventsType', $eventsType, PDO::PARAM_STR); 

     /*$movieDescription = filter_input(INPUT_POST, 'movieDescription'); 
     $stmt->bindValue(':movieDescription', $movieDescription, PDO::PARAM_STR);*/ 

     /*$movieRating = filter_input(INPUT_POST, 'movieRating'); 
     $stmt->bindValue(':movieRating', $movieRating, PDO::PARAM_STR);*/ 

     $stmt->execute(); 
     $pdo = null; 

     echo "<h1>Done</h1>"; 
     echo '<a href="index.php">Return to main menu</a>'; 


    } catch (PDOException $e) { 
     print "We had an error: " . $e->getMessage() . "<br/>"; 
     die(); 
    } 
    ?> 



    <?php } else { 
     try { 

     $sql = "SELECT * FROM events WHERE id=:eventsId LIMIT 1"; 

     //named paramaters 
     $stmt = $pdo->prepare($sql); 

     $id = filter_input(INPUT_GET, 'id'); 
     $stmt->bindValue(':eventsId', $id, PDO::PARAM_INT); 

     $stmt->execute(); 
     $r = $stmt->fetch(PDO::FETCH_ASSOC); 
     $pdo = null; 

     if (!$r){ 
      print "No events specified to update"; 
      exit(); 
     } 

     } catch (PDOException $e) { 
      print "We had an error: " . $e->getMessage() . "<br/>"; 
      die(); 
     } 
     ?> 



    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
    EventID: <input type="text" readonly name="movieId" value="<?= htmlspecialchars($r['id']); ?>"><br> 
    Type: <input type="text" name="movieTitle" value="<?= htmlspecialchars($r['title'])?>"><br> 
    Name: <input type="text" name="movieDescription" value="<?= htmlspecialchars($r['description']); ?>"><br> 

    <input type="submit" name="submit" value="Update record" > 
    </form> 

    <?php } ?> 

,这是删除:

<?php 

$pdo = new PDO('sqlite:events.db'); 


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

if (isset($_POST['confirm']) && $_POST['confirm'] == 'yes'){ 
    try { 
    //better, but still need to do more checking for security 
    $sql = "DELETE from events WHERE id=:eventId"; 
    //named paramaters 
    $stmt = $pdo->prepare($sql); 

    $eventsId = filter_input(INPUT_POST, 'eventsId'); 
    $eventsId = filter_input(INPUT_POST, 'eventsId', FILTER_SANITIZE_NUMBER_INT); 
    $stmt->bindValue(':eventsId', $eventsId, PDO::PARAM_INT); 

    $stmt->execute(); 
    $pdo = null; 
    echo '<h1>Film has been removed from database</h1>'; 
    echo '<a href="index.php">Return to main menu</a>'; 

    } catch (PDOException $e) { 
     print "We had an error: " . $e->getMessage() . "<br/>"; 
     die(); 
    } 

} else { 
    echo 'You need confirm.'; 

} 


?> 



<?php } else { 
    try { 

    $sql = "SELECT * FROM events WHERE id=:eventsId LIMIT 1"; 

    //named paramaters 
    $stmt = $pdo->prepare($sql); 

    $id = filter_input(INPUT_GET, 'id'); 
    $stmt->bindValue(':eventsId', $id, PDO::PARAM_INT); 

    $stmt->execute(); 
    $r = $stmt->fetch(PDO::FETCH_ASSOC); 
    $pdo = null; 

    if (!$r){ 
     print "No film specified to update"; 
     exit(); 
    } 

    } catch (PDOException $e) { 
     print "We had an error: " . $e->getMessage() . "<br/>"; 
     die(); 
    } 
    ?> 



    Title:<?= htmlspecialchars($r['type'])?> <br> 
    Desc:<?= htmlspecialchars($r['name']); ?> <br> 
    Year:<?= htmlspecialchars($r['rating']); ?> <br> 




<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
<input type="text" readonly name="eventsId" value="<?= htmlspecialchars($r['id']); ?>"><br> 
<input type="checkbox" name="confirm" value="yes">Yes, delete this record<br> 
<input type="submit" name="submit" value="Delete record"> 
</form> 

<?php } ?> 

我有不知道为什么它不工作。这是uni给我们的一个例子,并告诉我们要改变。我的表中的行是id,类型,名称和说明。

任何帮助非常感谢,因为我真的不知道发生了什么事!

+0

但是你注释掉了movie_id参数!?! – Strawberry

+0

不检查错误?愚蠢的教授技巧。 –

+0

更新应该抛出一个错误...有三个绑定占位符,但只有两个有价值的提供。 DELETE也应该抛出一个错误。对bindValue的调用引用了SQL语句中不存在的绑定占位符“:eventsId”。 (SQL语句中的占位符名称不同。)如果我们要从PDO中捕获异常,则需要使用PDO :: ATTR_ERRORMODE启用PDO异常。 (这个任务只是部分让代码运行,其中最重要的是学习调试程序所需的技能。) – spencer7593

回答

相关问题