JamsHack
JamsHack

Reputation: 9

PHP update not working

I'm building a custom CMS and I've written a script that is supposed to edit already existing information in my database.

I've used the code for a different database before and it has worked without any troubles, but I've changed the index names to reference a new database and now it won't work.

The information is displayed on a page with an 'edit' button the links the user to a html form which displays the selected piece of info in a text box.

There's no problem displaying the info in the form, but once the submit button is pressed the code does not execute it, and the info is not updated and no error message is displayed..

so I'm fairly sure there's a problem somewhere in this... (Ignore the comments)

    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>

Upvotes: 0

Views: 546

Answers (2)

istos
istos

Reputation: 2662

Okay one thing that I see right away is this line:

$timeslotid = $_POST['timeslotid'];

Where is that form field in your form? I don't see it anywhere. Also try to assign the execution to a variable and var_dump it so you can see if it returns TRUE or FALSE:

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

Furthermore make sure that you DB column is named Time and not time with all lowercase.

Upvotes: 0

inhan
inhan

Reputation: 7470

Shouldn't WHERE timeslotid = timeslotid be WHERE timeslotid = $timeslotid ?

Also, using a form value directly is a bad idea. Use it at least like $timeslotid = (int)$_GET['timeslotid'];.

Upvotes: 1

Related Questions