www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP + SQL connection sqlsrv_query

  1. #1
    Join Date
    Apr 2011
    Posts
    67

    PHP + SQL connection sqlsrv_query

    hi i'm trying to insert a query on sql db but its not successful

    PHP Code:
    <?php
    $se9rverName 
    ".";
    $connectionInfo = array( "UID"=>"WebsitePhp","PWD"=>"WebsitePhp""Database"=>"php");
    $conn sqlsrv_connect$serverName$connectionInfo);

    if( 
    $conn )
    {
        echo 
    "Connection established.\n";
    }
    else
    {
        echo 
    "Connection could not be established.\n";
        die( 
    print_rsqlsrv_errors(), true));
    }

    //-----------------------------------------------
    // Perform operations with connection.
    //-----------------------------------------------

    /* Close the connection. */
    //sqlsrv_close( $conn);
    ?>
    PHP Code:
    <?php
    include("connect.php");
    ?>

    <?php
    if($_POST['submit']){
        
    $sql="insert into dbo.Loan (Lon_FirstName,Lon_SurName) values (?,?)"
        
    $params= array("$_POST[fname]","$_POST[lname]");
        
    $test=sqlsrv_query($conn,$sql,$params);
        
        
    //header('Location: index.php');


        
    sqlsrv_close($conn);
    }
    //$test = sqlsrv_query('select * from loan');
    ?>
    <form method="POST" action="index.php">
    <table>
    <tr>
    <td>FirstName:<input type="textbox" name="fname"/></td>
    <td>LastName:<input type="textbox" name="lname"/></td>
    </tr>
    <tr>
    <td colspan=2><input type="submit" name="submit" value="submit"></td>
    </tr>

    </table>
    </form>

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    555
    Sorry to hear it is unsuccessful, but could you give US some idea in what way it is failing? You have error messages in this code - are you getting any of them?

    btw - I don't think you need quotes around the vars in the array.

  3. #3
    Join Date
    Apr 2011
    Posts
    67
    Quote Originally Posted by ginerjm View Post
    Sorry to hear it is unsuccessful, but could you give US some idea in what way it is failing? You have error messages in this code - are you getting any of them?

    btw - I don't think you need quotes around the vars in the array.

    hi
    PHP Code:
    if($_POST['submit']){
       
    // $sql="insert into dbo.Loan (Lon_FirstName,Lon_SurName) values (?,?)"; 
       // $params= array("$_POST[fname]","$_POST[lname]");
        //$test=sqlsrv_query($conn,$sql,$params);
        
        //header('Location: index.php');
        
    $sql "INSERT INTO loan (LoanID,Lon_FirstName, Lon_SurName) VALUES (?,?, ?)";
        
    $params = array('',$_POST[fname], $_POST[lname]);

        
    $stmt sqlsrv_query$conn$sql$params);
        if( 
    $stmt === false ) {
            die( 
    print_rsqlsrv_errors(), true));
        }


    this is the current code that i did...
    somehow i manage to insert the data on the sql database, but
    the LoanID requires me to insert the ID which is not what i want to happen, i already set the LoanID COLUMN into autoincrement.. but it give's me this error if i did not set any data on the column.

    Array ( [0] => Array ( [0] => 23000 [SQLSTATE] => 23000 [1] => 2627 [code] => 2627 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Violation of PRIMARY KEY constraint 'PK_Loan'. Cannot insert duplicate key in object 'dbo.Loan'. The duplicate key value is (5). [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Violation of PRIMARY KEY constraint 'PK_Loan'. Cannot insert duplicate key in object 'dbo.Loan'. The duplicate key value is (5). ) [1] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 3621 [code] => 3621 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]The statement has been terminated. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]The statement has been terminated. ) )

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    555
    Don't include loaned in your query statement then.

  5. #5
    Join Date
    Apr 2011
    Posts
    67
    hi

    if($_POST['submit']){
    // $sql="insert into dbo.Loan (Lon_FirstName,Lon_SurName) values (?,?)";
    // $params= array("$_POST[fname]","$_POST[lname]");
    //$test=sqlsrv_query($conn,$sql,$params);

    //header('Location: index.php');
    $sql = "INSERT INTO loan (Lon_FirstName, Lon_SurName) VALUES (?, ?)";
    $params = array($_POST[fname], $_POST[lname]);

    $stmt = sqlsrv_query( $conn, $sql, $params);
    if( $stmt === false ) {
    die( print_r( sqlsrv_errors(), true));
    }

    }
    i already remove the Primary Key column, still it throws me this..



    Array ( [0] => Array ( [0] => 23000 [SQLSTATE] => 23000 [1] => 515 [code] => 515 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert the value NULL into column 'LoanID', table 'php.dbo.Loan'; column does not allow nulls. INSERT fails. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert the value NULL into column 'LoanID', table 'php.dbo.Loan'; column does not allow nulls. INSERT fails. ) [1] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 3621 [code] => 3621 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]The statement has been terminated. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]The statement has been terminated. ) )

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    555
    Are you sure you defined that column as auto-increment in your structure?

  7. #7
    Join Date
    Apr 2011
    Posts
    67
    hi, i check the table, i thought it was already save once you switch the No to yes on autoincrementation setting, yep its already working, thanks,

    sql studio is a bit complicated compared to phpmyadmin

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles