Lines 48-90
my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!!
Link Here
|
48 |
|
48 |
|
49 |
print $cgi->header('application/json; charset=utf-8'); |
49 |
print $cgi->header('application/json; charset=utf-8'); |
50 |
|
50 |
|
51 |
if ($params->{'action'} eq 'add') { |
51 |
my $action = $params->{'action'} || 'get'; |
|
|
52 |
if ($action eq 'add') { |
52 |
my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);'); |
53 |
my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);'); |
53 |
$sth->execute($params->{'source'}, $params->{'text'}); |
54 |
$sth->execute($params->{'source'}, $params->{'text'}); |
54 |
if ($sth->err) { |
55 |
if ($sth->err) { |
55 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
56 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
56 |
exit 0; |
57 |
exit 1; |
57 |
} |
58 |
} |
58 |
my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here |
59 |
my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here |
59 |
$sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;'); |
60 |
$sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;'); |
60 |
$sth->execute($new_quote_id); |
61 |
$sth->execute($new_quote_id); |
61 |
print to_json($sth->fetchall_arrayref, {utf8 =>1}); |
62 |
print to_json($sth->fetchall_arrayref, {utf8 =>1}); |
62 |
exit 1; |
63 |
exit 0; |
63 |
} |
64 |
} |
64 |
elsif ($params->{'action'} eq 'edit') { |
65 |
elsif ($action eq 'edit') { |
65 |
my $aaData = []; |
66 |
my $aaData = []; |
66 |
my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns |
67 |
my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns |
67 |
my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1] = ? WHERE id = ?;"); |
68 |
my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1] = ? WHERE id = ?;"); |
68 |
$sth->execute($params->{'value'}, $params->{'id'}); |
69 |
$sth->execute($params->{'value'}, $params->{'id'}); |
69 |
if ($sth->err) { |
70 |
if ($sth->err) { |
70 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
71 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
71 |
exit 0; |
72 |
exit 1; |
72 |
} |
73 |
} |
73 |
$sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;"); |
74 |
$sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;"); |
74 |
$sth->execute($params->{'id'}); |
75 |
$sth->execute($params->{'id'}); |
75 |
$aaData = $sth->fetchrow_array(); |
76 |
$aaData = $sth->fetchrow_array(); |
76 |
print Encode::encode('utf8', $aaData); |
77 |
print Encode::encode('utf8', $aaData); |
77 |
|
78 |
|
78 |
exit 1; |
79 |
exit 0; |
79 |
} |
80 |
} |
80 |
elsif ($params->{'action'} eq 'delete') { |
81 |
elsif ($action eq 'delete') { |
81 |
my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;"); |
82 |
my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;"); |
82 |
$sth->execute($params->{'id'}); |
83 |
$sth->execute($params->{'id'}); |
83 |
if ($sth->err) { |
84 |
if ($sth->err) { |
84 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
85 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
85 |
exit 0; |
86 |
exit 1; |
86 |
} |
87 |
} |
87 |
exit 1; |
88 |
exit 0; |
88 |
} |
89 |
} |
89 |
else { |
90 |
else { |
90 |
my $aaData = []; |
91 |
my $aaData = []; |
Lines 97-103
else {
Link Here
|
97 |
$sth->execute(); |
98 |
$sth->execute(); |
98 |
if ($sth->err) { |
99 |
if ($sth->err) { |
99 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
100 |
warn sprintf('Database returned the following error: %s', $sth->errstr); |
100 |
exit 0; |
101 |
exit 1; |
101 |
} |
102 |
} |
102 |
|
103 |
|
103 |
$aaData = $sth->fetchall_arrayref; |
104 |
$aaData = $sth->fetchall_arrayref; |
104 |
- |
|
|