TheRod1990 has asked for the wisdom of the Perl Monks concerning the following question:
My question now: How is it possible to get myself the color from the MYSQL Table without reloading my perl document? Especially this code i thought get myself my variables on demand. But right it crashes when i make a reload.#!/usr/bin/perl -w use CGI; use warnings; use strict; use DBI; #use System::Timeout qw(system); my $cgi = CGI->new; my $color; my $history; my $table = "DBI:mysql:colorchanger"; my $username = "root"; my $password = ''; my %error = (PrintError=>0, RaiseError=>1); my $dbc = DBI->connect($table,$username,$password, \%error); #MYSQL Abfrage: Den letzten Werte us der Datenbank. # Params my $id = $cgi->param('hexcode'); if (defined $id) { $dbc->do( "INSERT INTO color02 ( hexcode ) VALUES ( ? )", undef, $id + ); #my $db_result = $dbc->prepare('SELECT hexcode FROM color02 ORDER BY + id DESC LIMIT 1'); #$db_result->execute() or die $db_result->err_str; my ($dbc) = @_; my @row; my $sql = "SELECT hexcode, ts FROM color02 ORDER BY id DESC LIM +IT 1"; my $sth = $dbc->prepare($sql); # # execute the query # $sth->execute(); # while(@row = $sth->fetchrow_array()) { # print "<tr id='tbl_row'><td>$row[1]</td>"; # print "<td style='background-color: $row[0]'>$row[0]</td></ +tr>"; # } # $sth->finish(); } my $db_result = $dbc->prepare('SELECT hexcode FROM color02 ORDER BY id + DESC LIMIT 1'); $db_result->execute() or die $db_result->err_str; while (my @row = $db_result->fetchrow_array()) { $id = $row[0]; } sub history_table { my ($dbc) = @_; my @row; my $sql = "SELECT hexcode, ts FROM color02 ORDER BY id DESC LIMI +T 10"; my $sth = $dbc->prepare($sql); # execute the query $sth->execute(); print "<div id='historytable' type='text' style='position: fixed +; top: 0px; width: 300px; height: 100%; background-color: #eee; margi +n-left: 0%; margin-top: 0px'> <input id='hsufu' type='text' name='hsufu'></input> <table id='table'>"; while(@row = $sth->fetchrow_array()) { print "<tr id='tbl_row'><td>$row[1]</td>"; print "<td style='background-color: $row[0]'>$row[0]</td></tr +>"; } print "</tr></table></div>"; $sth->finish(); } my $html = qq( <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www. +w3.org/TR/html4/strict.dtd"> <html> <head> <title>Dynamische Farbänderung</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5 +.2/jquery.min.js"></script> <script src="js/changecolor.js"></script> <link rel="stylesheet" href="css/changecolor.css"> </head> ); $html .= qq( <body style="background-color: $id"> <div id="formular"> <h1 id="h1" style="color: $id">Testaufgabe</h1> <h2 id="h2" style="color: $id">Dynamische HintergrundFarben Än +derung</h2> <input autofocus id='hexadezimal' type='text' name='hexadezi +mal'> </div> </body> </html> ); # Params #$id = $cgi->param('hexcode'); print $cgi->header(-charset => 'utf-8'); history_table($dbc); print $html; print $color;
Kind regards Daniel$dbc->do( "INSERT INTO color02 ( hexcode ) VALUES ( ? )", undef, $id + ); #my $db_result = $dbc->prepare('SELECT hexcode FROM color02 ORDER BY + id DESC LIMIT 1'); #$db_result->execute() or die $db_result->err_str; my ($dbc) = @_; my @row; my $sql = "SELECT hexcode, ts FROM color02 ORDER BY id DESC LIM +IT 1"; my $sth = $dbc->prepare($sql);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Print my latest MySQL Entries without page reload
by marto (Cardinal) on Mar 03, 2020 at 10:26 UTC | |
|