5 Replies - 710 Views - Last Post: 28 August 2016 - 08:43 PM

#1 tonyal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Inserting data from web form to MySQL database.

Posted 27 August 2016 - 12:50 PM

Hello, I'm trying to take data from a web form and then enter it into a database, and I'm getting an error. I've done it with PHP, but with Perl I'm completely lost. I've taken bits from different examples I've found on the net, but I haven't found one that does what I need or shows the whole code together. I was hoping someone might tell me what I'm doing wrong, or if they know somewhere that has an (up-to-date) example/tutorial. I'll post the html then the script - along with the error I get when I try to upload a contact. For now, I don't care much if it's not "pretty" I just want it to function right.

HTML:
<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Address Book</title>
  <link rel="stylesheet" href="stylesheet.css">
</head>

<body>
	<h2>Address Book</h2>
	<form name="myForm" method="post" action="myScript.pl" onsubmit="return validateForm()">
		<fieldset>
			<legend>Contact Details</legend>
			<div>
				<label for="First_Name">First Name:  </label>
				<input type="text" id="First_Name" name="First_Name" placeholder="John" required><br>
			</div>
			
			<div>
				<label for="Last_Name">Last Name:  </label>
				<input type="text" id="Last_Name" name="Last_Name" placeholder="Doe" required><br>
			</div>
		
			<div>
				<label for="Address">Address:  </label>
				<input type="text" id="Address" name="Address" placeholder="123 My Street Anytown, US 45678" required><br>
			</div>
		
			<div>
				<label for="Phone">Phone:  </label>
				<input <input pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" id="Phone" name="Phone" placeholder="(xxx)xxx-xxxx" maxlength="13" required> <br>
			</div>
		
			<div>
				<label for="Birthday">Birthday:  </label>
				<input type="date" id="Birthday" name="Birthday" value="" required><br>
			</div>
		
			<div>
				<label for="Email">Email:  </label>
				<input type="email" id="Email" name="Email" placeholder="[email protected]" required><br>
			</div>
			
			<div>
				<label for="Relationship">Relationship:  </label>
				<input type="text" id="Relationship" name="Relationship" placeholder="brother" required><br>
			</div>
		
			<div>
				<input type="submit" value="Submit">
				<input type="reset" value="Reset">
			</div>
	</form>
</body>
</html>



Script:
use CGI;
use DBI;
use DBD::mysql;

$myConnection = DBI->connect("DBI:mysql:address_book:**.**.***.***", "user", "password");

#Get the parameter from your html form.
$Contact_ID=$q->param('Contact_ID');
$First_Name=$q->param('First_Name');
$Last_Name=$q->param('Last_Name');
$Address=$q->param('Address');
$Phone=$q->param('Phone');
$Birthday=$q->param('Birthday');
$Email=$q->param('Email');
$Relationship=$q->param('Relationship');
print $q->header; 

$sql="INSERT INTO address_book.Contacts(Contact_ID,First_Name,Last_Name,Address,Phone,Birthday,Email,Relationship) values('0','$fname','$First_Name','$Last_Name','$Address','$Phone','$Birthday','$Email','$Relationship')";
$sth = $myConnection->prepare($sql)
or die "Can't prepare $sql: $myConnection->errstrn";
#pass sql query to database handle..

$rv = $sth->execute
or die "can't execute the query: $sth->errstrn";
#execute your query

if ($rv==1){
print "Record has been successfully updated !!!n";
}else{
print "Error!!while inserting recordn";
exit;
}


This post has been edited by tonyal: 27 August 2016 - 03:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Inserting data from web form to MySQL database.

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: Inserting data from web form to MySQL database.

Posted 27 August 2016 - 01:00 PM

You haven't posted the error.
Was This Post Helpful? 0
  • +
  • -

#3 tonyal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Re: Inserting data from web form to MySQL database.

Posted 27 August 2016 - 03:35 PM

Doh! Sorry, it was:

HTTP Error 502.2 - Bad Gateway
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "Can't locate DBD/mysql.pm in @INC (@INC contains: C:/Program Files (x86)/Parallels/Plesk/Additional/Perl/site/lib C:/Program Files (x86)/Parallels/Plesk/Additional/Perl/lib .) at G:\PleskVhosts\herpaderpa.com\httpdocs\project\myScript.pl line 3. BEGIN failed--compilation aborted at G:\PleskVhosts\herpaderpa.com\httpdocs\project\myScript.pl line 3. "
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: Inserting data from web form to MySQL database.

Posted 27 August 2016 - 03:51 PM

What is your interpretation of the error message?

It looks quite detailed to my outsider's view, but it refers to line 3; presumably use DBD::mysql;. So, even without a clear reading of the error, we might assume that the module failed to load. Perhaps it was installed incorrectly, to the wrong location, or an older version, or incomplete, etc..

You'll probably want to wait for someone with more direct experience to contribute, but line 3 provides you with a starting point to investigate. Searching part of the error text is also a first step.
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: Inserting data from web form to MySQL database.

Posted 27 August 2016 - 04:36 PM

I would agree the driver doesn't look installed, or isn't installed correctly.

DBD driver


Any reason why you are using perl? I know perl developers that will maintain legacy, but even they don't create new perl scripts.
Was This Post Helpful? 1
  • +
  • -

#6 tonyal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Re: Inserting data from web form to MySQL database.

Posted 28 August 2016 - 08:43 PM

View Postastonecipher, on 27 August 2016 - 04:36 PM, said:

Any reason why you are using perl? I know perl developers that will maintain legacy, but even they don't create new perl scripts.


It's for a Perl course I am taking for school. The book isn't very helpful with giving good examples for what we are tasked with doing for this project (Creating a web form that takes contacts for a personal phone book, and then upload it to a database table). I'm having a hard time finding full examples - it's usually just snippets, and I don't know how they piece together. The teacher doesn't really answer my questions, so I'm just trying to do what I can. That's why I love this forum! :)

View Postandrewsw, on 27 August 2016 - 03:51 PM, said:

..


Thank you though! I'm very new (and very confused) when it comes to Perl, but I will get this done. *plays Eye of the Tiger*
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1