1

I am trying to insert boolean values into mysql table through PHP Script.

Here is my code:

$id = "104"
  $bool1 = true
  $bool2 = false

$sql = "INSERT INTO Monday  SET Id = '$id' , morning = '$bool1' , night = '$bool2' " ;

//Store the result in $result
$result = $conn->prepare($sql) ;

// Get the result  in a Returnvalue var
if (!$result) {
    throw new Exception($result->error);
}
$result->bind_param("sii" ,$patientId , $morning , $night );
$returnValue = $result ->execute();
return $returnValue ;`

In MYSQL table structure

morning and night both are of type BOOLEAN (tinyint(1))

When I execute this code true and false both inserts value '0' .

10
  • yes thats because your structure of column is tinyint 1 that means only one value can be inserted, change the column to varchar(4) and check again Commented Jun 14, 2017 at 8:14
  • what are the sample values for your $bool1 and $bool2 Commented Jun 14, 2017 at 8:14
  • The code does what you have made it to. So I wonder a bit what the actual question is. Saying that, interpolating the variables into the query-string directly often is error prone. That might but must not be the cause of your issue. Commented Jun 14, 2017 at 8:15
  • TinyInt(1) only knows 0(false) and 1(true). so instead of inserting true or false insert 0 and 1. Commented Jun 14, 2017 at 8:15
  • Show us a var_dump($bool1); and var_dump($bool2); Commented Jun 14, 2017 at 8:18

1 Answer 1

1

Because morning and night are tinyint you should pass numbers(integers) 0=false and 1=true.

Also your mysqli_ code is a little odd, when using prepared parameterised queries you do not concatenate values into the query text, you use ? parameters

$id = "104"
$bool1 = 1
$bool2 = 0

$sql = "INSERT INTO Monday  SET Id = ? , morning = ? , night = ?" ;

$result = $conn->prepare($sql) ;

// Get the result  in a Returnvalue var
if (!$result) {
    throw new Exception($result->error);
}
$result->bind_param("sii" ,$patientId , $morning , $night );
$returnValue = $result ->execute();
return $returnValue;

You might also like to rethink your database design. Having a table for each day is very odd. Instead consider a table with a more generic name, and in it have a column called dayname or something like that.

So your table might look like this

id
dayname
morning
night

and a single table would hold all the days of the week

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

6 Comments

I did tried entering table name as a Variable but it failed."INSERT INTO '$dayOfWeek' SET Id = ? , morning = ? , night = ?"
No thats not what I ment! You cannot use a parameter for a table name or a column name
Actually I have 7 tables Monday , Tuesday , Wednesday .... etc . All 7 tables structure is same. I want this one query to apply for all 7 tables. How can I make this query dynamic ? Shall I give table name as variable $dayOfWeek which can have any value from Monday till Sunday.
No. Make ONE TABLE to hold all 7 days. That is what I have been trying to say
No I am not allowed to do so.
|

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.