Muncul Pesan Warning: odbc_exec(): supplied resource is not a valid saat Insert Data ke SQL Server

Saya sudah berhasil mengkoneksikan ke SQL Server,namun saat saya INSERT data muncul pesan Warning: odbc_exec(): supplied resource is not a valid ODBC-Link resource in C:\xampp\htdocs\phpCon\index.php on line 17.

Saya menggunakan XAMPP php versi 7.3.2 dan SQL Server 2012

Mohon bantuannya kak.

connect.php

<?php
$serverName = "DESKTOP-2P6S23P"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"LatihanDB");
$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));
}

index.php

<?php
include("connect.php");

function getData()
{
    $data = array();
    $data[1] = $_POST['nama'];
    $data[2] = $_POST['nim'];
    return $data;
}

if (isset($_POST['insert'])) {
    $info = getData();
    $insert = "INSERT INTO [tbl_mahasiswa] ([id]
        ,[nama]
        ,[nim]) VALUES ('$info[1]','$info[2]')";
    $result = odbc_exec($conn, $insert);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>

    <div class="jumbotron text-center">
        <h1>Coba</h1>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Alias eius illum non.!</p>

        <form method="POST">
            <div class="form-group">
                <label for="nama">Nama:</label>
                <input type="text" class="form-control" id="nama" name="nama">
            </div>
            <div class="form-group">
                <label for="nim">NIM:</label>
                <input type="text" class="form-control" id="nim" name="nim">
            </div>
            <button type="submit" class="btn btn-warning" style="color: #fff;" name="insert">Submit</button>
        </form>
    </div>
</body>

</html>

Outputnya :

Screenshot (83).png90.07 KB

avatar HendryScootKennedy
@HendryScootKennedy

19 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban