MySQL Forums
Forum List  »  Perl

What's wrong with my code??
Posted by: Dain Hedgpeth
Date: October 22, 2011 02:20AM

I'm a total noob to Perl. Why isn't this working?? I don't get an error message, it just returns '0' and nothing is added to the database:

---------------------
#!/usr/bin/perl

use Mysql;
use CGI;
use DBI;

print "Content-type: text/html \n\n";

# Read form data etc. dh
my $cgi = new CGI;

my $game = $cgi->param('game');#next 3 dh
my $playerName = $cgi->param('player');
my $score = $cgi->param('score');

exit 0 unless $game; # dh

# MYSQL CONFIG VARIABLES
$host = "xxxx";
$database = "xxxx";
$tablename = "xxxx";
$user = "xxxx";
$pw = "xxxx";

# PERL MYSQL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT DB
$connect->selectdb($database);

# DEFINE A MySQL QUERY
$myquery = "INSERT INTO $tablename (game, player, score) VALUES (?,?,?)";

# PREPARE THE QUERY FUNCTION DH
$execute = $connect->prepare($myquery);

# EXECUTE THE QUERY FUNCTION DH
$execute->execute($games, $player, $score);

# AFFECTED ROWS
$affectedrows = $execute->affectedrows($myquery);

# ID OF LAST INSERT
$lastid = $execute->insertid($myquery);

print $affectedrows."<br />";
print $lastid."<br />";

-------------------------------

If I replace the lines:

# PREPARE THE QUERY FUNCTION DH
$execute = $connect->prepare($myquery);

# EXECUTE THE QUERY FUNCTION DH
$execute->execute($games, $player, $score);

With:

# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);

And I put explicit VALUES in the definition of $myquery preceding this line the values are inserted with no issues...

If it helps at all the HTML form code I'm using is:

<form action="xxx" method="POST">
<p> Game Name: <input type="text" name="game"/> </p>
<p> Player Name: <input type="text" name="player"/> </p>
<p> Score: <input type="text" name="score"/> </p>
<input type="submit" value="Submit"/>
</form>


What's goin on here? I feel like my code matches every example I can find to a tee...

Options: ReplyQuote


Subject
Written By
Posted
What's wrong with my code??
October 22, 2011 02:20AM
October 22, 2011 08:58PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.