How do I insert data into a field of bit data type?

php, sql

Solution

I assume that the `active` field is of `bit` data type.

You don't use any quotation marks around the value being passed for the active field like what you do for customerid field.

Also, I think you have to convert the value true / false to 1 / 0.

Modified code: Noticed that the single quotes surrounding `" . $active . "` have been removed.

$sqlInsert = "INSERT INTO customers(customerID, registeredDate, givenName,
                familyName,  email, password, 
                phone, mobile, property, 
                street, locality, town,  
                area, postalCode, active) 
              VALUES(" . $newUser . "," . $date . ", ' " . $given . " ', 
              ' " . $family . " ', ' " . $email . " ', ' " . $pwd . " ', 
              ' " . $phone . " ', ' " . $mob . " ', ' " . $property . " ', 
              ' " . $street . " ', ' " . $locality . " ' , ' " . $town . " ', 
              ' " . $area . " ', ' " . $postalcode . " ', " . $active . ")";
$stmtInsert = sqlsrv_query($conn, $sqlInsert);

I am not sure why it worked with False value. I would suggest you to find out how the `INSERT` statement evaluates to after setting all the values. Instead of executing the statement, print the INSERT statement to screen/page and run it manually in the SQL Server Management Studio against the database.

Problem

I have a problem when trying to insert a new record into a database. The problem I think is with the bit field. When I assign the value True i get this error: ``` Failed: Array ( [0] => Array ( [0] => 22018 [SQLSTATE] => 22018 [1] => 245 [code] => 245 [2] => [Microsoft][SQL Server Native Client 10.0] [SQL Server]Conversion failed when converting the varchar value ' 1 ' to data type bit. [message] => [Microsoft][SQL Server Native Client 10.0] [SQL Server]Conversion failed when converting the varchar value ' 1 ' to data type bit. ) ) ``` but if i change it to false it works. I will show some of my code. I have cut out most of it as i have narrowed it down to this variable: ``` $active = True; ``` here is my insert query. ``` $sqlInsert = "INSERT INTO customers( customerID, registeredDate, givenName, familyName, email, password, phone, mobile, property, street, locality, town, area, postalCode, active ) VALUES(" . $newUser . "," . $date . ", ' " . $given . " ', ' " . $family . " ', ' " . $email . " ', ' " . $pwd . " ', ' " . $phone . " ', ' " . $mob . " ', ' " . $property . " ', ' " . $street . " ', ' " . $locality . " ' , ' " . $town . " ', ' " . $area . " ', ' " . $postalcode . " ', ' " . $active . " ')"; $stmtInsert = sqlsrv_query($conn, $sqlInsert); ```

Original source