【问题标题】:PHP update not workingPHP更新不起作用
【发布时间】:2013-01-11 10:37:36
【问题描述】:

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

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

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

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

所以我相当确定这其中的某个地方存在问题...(忽略 cmets)

    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>

【问题讨论】:

  • 你能把ini_set('display_error', )`放在页面顶部吗?
  • 错误显示功能已开启,不显示任何错误。

标签: php sql pdo sql-update


【解决方案1】:

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

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

【讨论】:

  • 错误:“注意:未定义的索引:C:\xampp\htdocs\Festival\Admin\Timeslots\index.php 中第 105 行的时间段”
  • 我认为这是一个整数。不用管用(int) 进行数据转换。但是您仍然应该清理从用户那里收集的数据。
  • @user2017005 你有机会尝试这个还是现在有任何其他错误?
【解决方案2】:

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

$timeslotid = $_POST['timeslotid'];

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

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

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

【讨论】:

  • 时间段未更新,“时间”列已更新。我应该删除这条线吗?
猜你喜欢
  • 2016-01-07
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
相关资源
最近更新 更多