0

I'm trying to add Varchar values from checkbox to mysql and it adding string "1" in the checked checkboxes. How I can make it to add Varchar values like below?

<?php
if (isset($_POST['dr_add'])) {
  $lightv = isset($_POST['l_vehicle']);
  $van = isset($_POST['van']);
  $truck = isset($_POST['truck']);
  $trailer = isset($_POST['tr_trailer']);
  mysql_query("INSERT INTO driving(user_id, country,l_vehicle,van, truck , tr_trailer) VALUES('$user->employeeid','$lightv','$van','$truck','$trailer')") or die(mysql_error());
}
?>
<input type="checkbox" name="l_vehicle" value="Light Vehicle"/>Light Vehicle
<input type="checkbox" name="van" value="Van">Van
<input type="checkbox" name="truck" value="Truck"/>Truck
<input type="checkbox" name="tr_trailer" value="Truck&Trailer"/>Truck&Trailer
0

2 Answers 2

3

You are using $trailer = isset($_POST['tr_trailer']); and it is wrong :

User below code :

 $trailer = isset($_POST['tr_trailer'])?$_POST['tr_trailer']:"";

Use this for all checkboxes

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

2 Comments

Correct. It will return 0 or 1.
Thank you very much, exactly what I was looking for :)
1

isset — Determine if a variable is set and is not NULL

UPDATED CODE

<?php
if (isset($_POST['dr_add'])) {
  $lightv = isset($_POST['l_vehicle']) ? $_POST['l_vehicle'] : "";
  $van = isset($_POST['van']) ? $_POST['van'] : "";
  $truck = isset($_POST['truck']) ? $_POST['truck'] : "";
  $trailer = isset($_POST['tr_trailer']) ? $_POST['tr_trailer'] : "";

  mysql_query("INSERT INTO driving(user_id, country,l_vehicle,van, truck , tr_trailer) VALUES('$user->employeeid','$lightv','$van','$truck','$trailer')") or die(mysql_error());
}
?>

[NOTE:The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead ]

mysqli

(where, $con is connection)

$stmt = mysqli_prepare($con, "INSERT INTO driving(user_id,country,l_vehicle,van,truck,tr_trailer) VALUES (?, ?, ?,?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'dsssss',$user->employeeid,$lightv,$van,$truck,$trailer);

Read Prepared Statements

Comments

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.