1

I would like to insert data from a form using php into mssql 2012.

enter image description here

The above picture shows I installed drivers for php to work with mssql and after testing the connection it was success.

Following is the script written to do the insertion:

$serverName = "SUPERMAN";

$connectionInfo = array('Database'=>'xikwambene', "UID"=>"develop", "PWD"=>"develop");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if($conn) {
    echo "Connection established.<br />";
}else {
    echo "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}


if(empty($_POST) === false && empty($errors)=== true)
        {

            $id = $_POST['id'];
            $name = $_POST['name'];

            $query = "INSERT INTO dbo.test (id,name)
                        VALUES  ('$id','$name')GO";

            $result = sqlsrv_prepare($conn,$query)or die('Error querying MSSQL database');
            sqlsrv_execute($result);





            //redirect
            header('Location: Address_insert.php');
            exit();                                 
        }


?>

Now the problem is nothing gets inserted into the database, and it successfully redirects to

Address_insert.php

Please Assist. its my first day working with php and mssql

4
  • You don't check for errors. Commented May 19, 2014 at 13:39
  • The connection is established? Commented May 19, 2014 at 13:43
  • even when I add error_reporting(E_ALL); to the script I still don't get any errors Commented May 19, 2014 at 13:43
  • Yes! connection was successfully established Commented May 19, 2014 at 13:43

3 Answers 3

1
if(empty($_POST) === false && empty($errors)=== true)
        {

            $id = $_POST['id'];
            $name = $_POST['name'];
            print_r($_POST);



            $query = "INSERT INTO dbo.test (id,name)
                        VALUES  (?,?)";

                $params = array($id, $name);        
            $stmt = sqlsrv_query( $conn, $query, $params);

            if( $stmt === false ) {
         die( print_r( sqlsrv_errors(), true));
                }

The answer is based on information from: http://www.php.net/manual/en/function.sqlsrv-query.php

Sign up to request clarification or add additional context in comments.

Comments

0

After this code:

 $id = $_POST['id'];
 $name = $_POST['name'];

add this :

$id = $_POST['id'];
$name = $_POST['name'];
print_r($_POST) 

to see if the script receive the php variables sent thru $_POST and comment :

//header('Location: Address_insert.php'); so you can see the print.

This method for me always give results.

Best regards!

3 Comments

When add print_r. I get the following output : **Array ( [id] => 1 [name] => motho ) **
After try to execute this query: "INSERT INTO dbo.test (id,name) VALUES ('1','motho ')GO " direclty to database.
I am not sure what you mean. are you saying I must type the script into my sql studio to see if the query works ?
0
    $query = "INSERT INTO dbo.test (id,name)
                VALUES  ('$id','$name')GO";


   $result = sqlsrv_prepare($conn,$query)or die('Error querying MSSQL database');

Here, $query is not compatible for a prepared statement. You don't assign parameters in queries in prepared statements, you use place holders like ?. If you wish to use prepared statements, you are doing it in a wrong way. If you wish to use regular queries, you would want to remove the sqlsrv_prepare ( ) line of code.

1 Comment

what should I replace the prepare statement with ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.