0

我正在构建一个自定义 CMS,并且我编写了一个脚本,该脚本应该编辑我的数据库中已有的信息。

我之前已经将代码用于不同的数据库,并且它可以正常工作,但是我已经更改了索引名称以引用新数据库,现在它无法正常工作。

信息显示在带有“编辑”按钮的页面上,该按钮将用户链接到 html 表单,该表单在文本框中显示选定的信息。

在表单中显示信息没有问题,但是一旦按下提交按钮,代码就不会执行它,并且信息不会更新,也不会显示错误消息..

所以我相当确定这其中某处存在问题......(忽略评论)

    if (isset($_GET['edittimeslot']))
    {   
       $timeslotid = $_GET['timeslotid']; 

       try
       {
          $sql = "SELECT timeslotid, Time FROM timeslots WHERE timeslotid = timeslotid" ;
          //echo $sql;
          $data = $pdo->query($sql);
          $timeslots = $data->fetch();
          //print_r($acts);
       }
       catch(PDOException $e)
       {
          echo "this didnt work" .$e->getMessage()  ;
       }

       $pagetitle = 'Edit your date here';
       $timeslotid = $timeslots['timeslotid'];
       $time = $timeslots['Time'];
       $button = 'Edit timeslot';
       include 'timeslot.form.php';
       exit();
    }


     // is all of the requested feilds appropiate
    if (isset($_POST['submit']) && $_POST['submit'] == 'Edit timeslot')
    {       
       // get the form data that was posted ready to insert into the stage database
       $timeslotid = $_POST['timeslotid'];
       $time= htmlspecialchars($_POST['time']);

       try
       {
           // prepare the query to insert data into stages table
           $sql = "UPDATE timeslots 
                                    SET Time = :Time,
                                    WHERE timeslotid = :timeslotid";

           $stmt = $pdo->prepare($sql);
           $stmt->bindParam(':Time', $time);
           $stmt->bindParam(':timeslotid', $timeslotid);
           $stmt->execute();
        }
        catch(PDOException $e)
        {
           //error message goes here if the insert fails
        }
  }

HTML:

        <!doctype html>
<head>
    <style type="text/css">
        .container {
            width: 800px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div class="container">
<h1><?php echo $pagetitle;?></h1>


<form action='.' method='post'>
    <!-- stage name -->
    <p><label for='time'> What is the timeslots you would like to add? 00:00-00:00 </label></p>
    <p><input type='text' name='time' id='time' value='<?php echo $time;?>'> </p>
    <p><input type='submit' name='submit' value='<?php echo $button;?>'></p>
</form>

</div>
</body>
4

2 回答 2

1

WHERE timeslotid = timeslotid应该WHERE timeslotid = $timeslotid吗?

此外,直接使用表单值是一个坏主意。至少像$timeslotid = (int)$_GET['timeslotid'];.

于 2013-01-28T05:56:36.247 回答
0

好的,我马上看到的一件事是这一行:

$timeslotid = $_POST['timeslotid'];

表单中的那个表单域在哪里?我在任何地方都看不到它。还尝试将执行分配给一个变量并 var_dump 它,以便您可以查看它是返回 TRUE 还是 FALSE:

$success = $stmt->execute();
var_dump($success);

此外,请确保您的数据库列被命名为时间,而不是全部小写的时间。

于 2013-01-28T06:15:53.960 回答