MSSQL Insert

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
netguykb
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Sep 14, 2021 6:13 pm

Tue Sep 14, 2021 6:20 pm

I have an HTML form page that redirects a user to a PHP page. The PHP page sends out an email. I would like to understand what PHP code I need on the same PHP page that inserts the form value into an MS SQL server database. I need to understand the connection string and how to pass the values from the form into SQL INSERT statement or MS SQL stored procedure. Here is the page for the email that again I would like to also include the SQL INSERT function into

Code: Select all

<?php
// Multiple recipients
$to = 'info@eaa309.club'; // note the comma


// Form http_post_fields
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$ownaircraft = $_POST['ownaircraft'];
$aircrafttype = $_POST['aircrafttype'];
$eaanumber = $_POST['eaanumber'];
$spouse = $_POST['spouse'];
$spousenametag = $_POST['spousenametag'];
$bio = $_POST['bio'];

$subject = 'New EAA Membership';
$cdateTime =  date("l jS \of F Y h:i:s A");
$urString = "Location: membership_confirmation.html?membername=$firstname \n $lastname";

// Message
$message = "
<html>
<head>
  <title>EAA Chapter 309 - New Membership</title>
</head>
<p><img src='https://eaa309.club/images/logos/small_logo_email2.png' alt='EAA 309 Logo></p>
<hr class='clear' /><br><br>
<p><font face='Arial'><strong>ATTENTION:</strong> You have just received a new membership email, please see the details below.<br><br>
<strong>From:</strong> \n $firstname \n $lastname <br>
<strong>Email:</strong> \n $email <br>
<strong>Phone:</strong> \n $phone <br>
<strong>Address:</strong> \n $address <br>
<strong>City:</strong> \n $city <br>
<strong>State:</strong> \n $state <br>
<strong>Zip Code:</strong> \n $zip <br>
<strong>Aircraft Ownership:</strong> \n $ownaircraft <br>
<strong>Aircraft Type:</strong> \n $aircrafttype <br>
<strong>National EAA Number:</strong> \n $eaanumber <br>
<strong>Spouse Name:</strong> \n $spouse <br>
<strong>Spouse Name Tag Needed:</strong> \n $spousenametag <br>
<strong>Brief Bio:</strong> \n $bio <br><br>
<strong>Date/Time Submitted:</strong>\n $cdateTime
</font></p>
<body>

</body>
</html>
";

// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers
$headers[] = 'To: EAA 309 <info@eaa309.club>'; // include a comma for more than one recipiant
$headers[] = 'From: EAA 309 <info@eaa309.club>';
//$headers[] = 'Cc: birthdayarchive@example.com';
//$headers[] = 'Bcc: birthdaycheck@example.com';

// Mail it
mail($to, $subject, $message, implode("\r\n", $headers)) or die("Email was not sent!");
//header('Location: membership_confirmation.html');
header('Location: membership_confirmation.html?firstname='.urlencode($firstname).'&lastname='.urlencode($lastname));

?>
netguykb
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Sep 14, 2021 6:13 pm

Tue Sep 14, 2021 6:49 pm

Just tried using this but no specific error reporting back to me. I'm using GoDaddy as my Website hosting so I'm not sure if i need to add a PHP connection or something on that side.

Code: Select all

//INSERT INTO MS SQL Database
//connect to sql
$dbc = mssql_connect('my ip address','user','password','database name')or die('Error connecting to
      the   SQL Server database.');

 // Input into staff database
  $query = "INSERT INTO dbo.[EAA_MEMBERS] ([First Name],[Last Name],[Email],
 [Home Phone],[Address],[City],[State],[Zip Code],[EAA Member ID],[Spouce],[Bio]) VALUES
('$firstname','$lastname','$email','$phone','$address','$city','$state','$zip','$eaanumber','$spouse','$bio')";
$r esult = mssql_query($query,$dbc)or die('Error querying MSSQL database');

//close to sql
mssql_close($dbc);
Post Reply