cadi2108
cadi2108

Reputation: 1280

Empty textbox as NULL in database

I have problem with set empty value in UPDATE when textbox is empty. It's my value:

$kod = $_POST['Kod'];
$kod = !empty($kod) ? "'$kod'" : "NULL";

and query:

$query = "UPDATE Sprzety SET Kod = $kod, Wlasciciel = '$wlasciciel', Konfiguracja = '$konfiguracja' WHERE SprzetID = '$id'";

What is wrong here?

Upvotes: 0

Views: 638

Answers (1)

Kermit
Kermit

Reputation: 34054

You should use mysqli_ or PDO functions. Below is a mysqli_ procedural solution:

$kod = !empty($_POST['Kod']) ? $_POST['Kod'] : null;

//$link refers to your mysqli_ connection
if ($stmt = mysqli_prepare($link, 
    "UPDATE UPDATE Sprzety 
     SET Kod = ?, Wlasciciel = ?, Konfiguracja = ?
     WHERE SprzetID = ?")) {

/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "sssi", $kod, $wlasciciel, $konfiguracja, $id);

/* execute query */
mysqli_stmt_execute($stmt);

Upvotes: 3

Related Questions