0

I want to update my database with an SQL statement once someone clicks a button on the website. I've tried something, no success. Can you guys help me ? Here's the code: http://pastebin.com/D0S83Jgh
Don't know if I made this question correctly, I'm new here.

unm4sk
  • 335
  • 1
  • 8

2 Answers2

1

Your prepared statement is wrong.

The code I use with pdo to do a query is this:

$sqlUpd = $upd->prepare("UPDATE league_signups SET approved='1' WHERE id = :id");
$q->bindParam(':id', $id, PDO::PARAM_STR);
$q->execute();

Should work like a charm.

Chris G
  • 787
  • 6
  • 20
  • Thanks for editing that part, but the button still doesn't work :/ Like it's not functioning. – unm4sk Dec 29 '15 at 16:54
  • error_reporting(-1); Add this to the top of your page inside the php. Let me know the error you're getting. – Chris G Dec 29 '15 at 16:57
  • ^ that, is what I told them already, as per [this comment](http://stackoverflow.com/questions/34515177/button-to-update-mysql-column-not-working#comment56771256_34515177) but it seems like they're already "checking" for errors. – Funk Forty Niner Dec 29 '15 at 16:58
  • @Fred-ii- : I know, but sometimes it's easier and faster just to give them a code to work with :). – Chris G Dec 29 '15 at 16:59
  • I've added the error_reporting(-1) but still nothing. I think the problem is that PHP doesn't even get that button click. – unm4sk Dec 29 '15 at 16:59
  • @iDentity: Don't forget to put a ; after the (-1) or it won't work :). – Chris G Dec 29 '15 at 17:00
  • @ChrisG I know, but still, nothing. Usually when there is an error i would catch it while the page is loading, but nothing's happening, no errors. – unm4sk Dec 29 '15 at 17:01
  • `error_reporting(-1)` will only log and not "display". If they don't know how to check logs, or don't have access to logs, then they need to "display". If they're not getting any, then the OP's using JS/Ajax and isn't checking their console. I've done what I could to help here. – Funk Forty Niner Dec 29 '15 at 17:03
  • @Fred-ii-: I use it all the time and it does display errors. – Chris G Dec 29 '15 at 17:07
  • 1
    @ChrisG sure, "you", but we don't know if the OP's system is setup the same way. Best bet: `error_reporting(E_ALL); ini_set('display_errors', 1);` I think the OP's not showing us everything, or has failed to use form tags, which name attributes require a form, or at least a GET method in an href, or jQuery/Ajax. Again, too many unknowns and I've already stated that and others about the possible missing `
    ` tag with GET/POST. I think you may have fallen into a rabbit hole here. I sincerely wish the best for all.
    – Funk Forty Niner Dec 29 '15 at 17:10
  • http://prntscr.com/9jx6hv And yes, `error_repoting(-1);` is in – unm4sk Dec 29 '15 at 17:11
  • @Fred-ii-: Hmm, you might be right about that. Good thing there is more then only me here hahahha :D – Chris G Dec 29 '15 at 17:12
  • `error_reporting(-1);` not `error_repoting(-1);` if that's what you're really using @iDentity – Funk Forty Niner Dec 29 '15 at 17:12
  • @iDentity: Where is that error coming from with mData? Might be the cause. – Chris G Dec 29 '15 at 17:14
  • 1
    @iDentity `mData`? where's that coming from? See, I knew it. We're all chasing a rabbit down an endless hole. You need to debug here. That's your job, not ours and you failed to show us relevant code. Although you state as "being new" here, you seem to know how *Stack rolls*. – Funk Forty Niner Dec 29 '15 at 17:14
  • @Fred-ii- mistyped in comments, it's correct in code. mData is coming from Datatables.js – unm4sk Dec 29 '15 at 17:15
  • that `mData` seems to be JS related. This question should be edited to show FULL code. @iDentity unless you're trolling us which I hope you're not. – Funk Forty Niner Dec 29 '15 at 17:16
  • @iDentity: Would it be possible to have a link to the webpage you are getting this problem from. We might be able to get more out of that. Or make a JSFiddle with all your html code to start off with. That way we can make sure your html is fine/ – Chris G Dec 29 '15 at 17:18
  • mData error is there just because of the table formation problems, nothing that could break the button. I'll give you guys the whole page in a few minutes, since I know it's hard to help like this. – unm4sk Dec 29 '15 at 17:24
  • @ChrisG There's the html/php. If .js is needed i'll upload them all somewhere. – unm4sk Dec 29 '15 at 17:32
  • @iDentity: You should wrap your table into a form. Only then will your button work. – Chris G Dec 29 '15 at 17:53
  • @ChrisG That was the answer for the button. Thanks :) – unm4sk Dec 29 '15 at 18:25
  • You're welcome. Be sure to check this as your answer so people know it's answered :) – Chris G Dec 29 '15 at 18:28
0

Get this code out of the main loop: while($row = $q->fetch(PDO::FETCH_ASSOC)) {}

<?php
include('pdoconnect.php');
$id = isset($row['id'];
if(isset($_REQUEST['approve']))
{
    $sqlUpd = "UPDATE league_signups SET approved='1' WHERE id=$id";
    $q = $upd->prepare($sqlUpd);
    $q->execute();
}
if(isset($_REQUEST['unapprove']))
{
    $sqlUpd = "UPDATE league_signups SET approved='0' WHERE id=$id";
    $q = $upd->prepare($sqlUpd);
    $q->execute();
}
?>

Put this code after the loop ending or the beginning of your code...

The data you want to update comes from the checkbox am I right? then you may want to make a loop to update all the values selected with checkbox to the corresponding action 'approve' or 'unapproved'

remove include('pdoconnect.php'); its utterly unnecessary if you are including this file from the beginning already

<?php

// checkbox[] it's an array...
$UpdateIDs = (isset($_REQUEST['checkbox'])) ? $_REQUEST['checkbox'] : [];
// check if $_REQUEST['approve'] is set else check if $_REQUEST['unapprove'] is set else set $approve to null;
$approved = (isset($_REQUEST['approve']) ? 
     $_REQUEST['approve'] : 
     (isset($_REQUEST['unapprove'])) ? $_REQUEST['unapprove'] : null;

if(!is_null($approved))
{
     try {
         foreach($UpdateIDs as $ID)
         {
             $stmt = $upd->prepare("UPDATE league_signups SET approved=:approved WHERE id=:id");
             $stmt->execute([
                 ':approved' => $approved,
                 ':id' => $ID
             ]);
         }
     } catch(PDOException $e) {
         echo 'ERROR: ' . $e->getMessage();
     }
}
David Lavieri
  • 1,060
  • 1
  • 8
  • 19
  • Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\league_panel\admin\index.php on line 315 That's this line: `(isset($_REQUEST['unapprove'])) ? $_REQUEST['unapprove'] : null;` – unm4sk Dec 29 '15 at 18:22
  • Sorry yeah i think you right i have missed to enclose that line in parenthesis. did it work at the end? – David Lavieri Dec 29 '15 at 18:49
  • I've marked Chris' answer as question answer for button not working, but I haven't resolved the issue completely. I've tried your code and added single Approve/Un-Approve buttons to the page, but it doesn't update data in MySQL. – unm4sk Dec 29 '15 at 18:52
  • You want to select specific rows and update all selected at once or want it to update one by one using approve/unapprove button for each row? – David Lavieri Dec 29 '15 at 19:18
  • Oh nevermind in your main post says one by one... gimme a sec im fixing it – David Lavieri Dec 29 '15 at 19:21
  • Well, i'm up for any solution, checkboxes or one by one doesn't matter. – unm4sk Dec 29 '15 at 19:23
  • Nope. I think i'm gonna rewrite everything :@ – unm4sk Dec 29 '15 at 19:40
  • did you add the file to 'action' attribute at form tag? the method is post so try change $_REQUEST to $_POST x.x – David Lavieri Dec 29 '15 at 19:45
  • this: should be set to value='0' and the other well set to 1 now im noticing it – David Lavieri Dec 29 '15 at 19:48
  • Works now. Thanks so much man! Dunno if i can set the answer checkmark thing multiple times here, if I can comment again so I can do it. Again, thanks :) – unm4sk Dec 29 '15 at 19:54
  • If you need any other help you can contact me through github glad to help people about PHP – David Lavieri Dec 29 '15 at 19:59