Jump to content

HTML Form Value to MSSQL Query


bicky

Recommended Posts

Hello All

 

Only just started using PHP in the last week or so, and have a issue where I can't get my PHP code to query an MS SQL table using values taken from a form.

 

Basically I have an email address passed into a form.  I then want PHP to query MS SQL and display the results.

 

(My fieldname in HTML is 'emailcheck')

 

<?php

/* Connect to the server. */

$serverName = "(local)";

$connectionInfo = array( "Database"=>"ashdev");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false )

{

    echo "Could not connect.\n";

    die( print_r( sqlsrv_errors(), true));

}

 

/* Set up and execute the query. */

 

$email = array($_POST['emailcheck']);

 

$tsql = "SELECT forename,surname,emailaddress

        FROM email

        WHERE emailaddress=$email";

 

$stmt = sqlsrv_query( $conn, $tsql);

 

/* Free statement and connection resources. */

sqlsrv_free_stmt( $stmt);

sqlsrv_close( $conn);

?>

 

Thanks in advance for your help

 

Bicky

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/207618-html-form-value-to-mssql-query/
Share on other sites

Well, I appear to have solved the initial problem of passing the variable to PHP

 

Needed to change the $tsql to:

 

$tsql = "SELECT forename,surname,emailaddress

        FROM email

        WHERE emailaddress='$email'";

 

I now need to return the results into a new page (diaplay the forename,surname and email address

 

Any ideas anyone?

 

Thanks

 

Bicky

I have now solved my problem...Thanks for the help?

 

Anyway...The problem was I was running the SQL statement before getting the variable for the form...

 

New code is as follows: It may help someone...

 

<?php
/* Connect to the local server */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"sqltablename");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
     echo "Could not connect.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Set up the SQL query. */
$email = $_POST['emailcheck'];
$tsql = "select forename,surname,emailaddress,source,date from sqltablename.dbo.email2	 where emailaddress='$email'";

/*Prepare and execute the query.*/
$stmt = sqlsrv_query( $conn, $tsql);

echo "<table border='1'>
<tr>
<th>Forename</th>
<th>Surname</th>
<th>Email Address</th>
<th>Source</th>
<th>Date</th>
</tr>";

while($row = sqlsrv_fetch_array($stmt))
  {
  echo "<tr>";
  echo "<td>" . $row['forename'] . "</td>";
  echo "<td>" . $row['surname'] . "</td>";
  echo "<td>" . $row['emailaddress'] . "</td>";
  echo "<td>" . $row['source'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);

sqlsrv_close( $conn);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.