Jump to content

Recommended Posts

Hello...

 

I'm inserting some values using php variables...

But when the variables are empty i do not want mysql to update that field...

 

This is how my code looks like now....

 

$sql = "UPDATE ${dbtable} SET 
renset_dato = '$renset_dato',
renset_usr = '$renset_usr',
verksted_dato = '$verksted_dato',
verksted_hvor = '$verksted_hvor',
verksted_hva = '$verksted_hva',
verksted_hvorfor = '$verksted_hvorfor',
skruer_dato = '$skruer_dato',
skruer_usr = '$skruer_usr',
lampe_tid_dato = '$lampe_tid_dato', 
lampe_tid_usr = '$lampe_tid_usr', 
lampe_timer = '$lampe_tid', 
lampe_strokes = '$lampe_strokes', 
intern_service_dato = '$internservice_dato', 
intern_service_usr = '$internservice_usr', 
km_stand_dato = '$km_stand_dato',
km_stand = '$km_stand_usr',
record_endret_timestamp = '$timestamp',
record_endret_usr = '$_SESSION[user]'
WHERE id = $_GET[id]";

$result = mysql_query($sql) or die (mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/167423-not-update-empty-variable-into-mysql/
Share on other sites

O, sorry for missing that out... Yes they are coming from an form, to create the variables i have this code:

$renset_dato = !empty($_POST ['renset_dato']) ? $_POST['renset_dato'] : '';

 

This is to make the variables to be not empty, i was told some time ago that a empty fields inserted in an mysql DB would make an error... I haven't really tried without this code.

Well, you would need to check if the variable is empty before adding it to the mysql query string. Like so:

 

<?php

$sql = "UPDATE ${dbtable} SET ";
if(isset($renset_dato) && $renset_dato !== "") { $sql .= "renset_dato = '$renset_dato',"; } // Do this for all of the variables.
renset_usr = '$renset_usr',
verksted_dato = '$verksted_dato',
verksted_hvor = '$verksted_hvor',
verksted_hva = '$verksted_hva',
verksted_hvorfor = '$verksted_hvorfor',
skruer_dato = '$skruer_dato',
skruer_usr = '$skruer_usr',
lampe_tid_dato = '$lampe_tid_dato', 
lampe_tid_usr = '$lampe_tid_usr', 
lampe_timer = '$lampe_tid', 
lampe_strokes = '$lampe_strokes', 
intern_service_dato = '$internservice_dato', 
intern_service_usr = '$internservice_usr', 
km_stand_dato = '$km_stand_dato',
km_stand = '$km_stand_usr',
record_endret_timestamp = '$timestamp',
record_endret_usr = '$_SESSION[user]'
WHERE id = $_GET[id]";

$result = mysql_query($sql) or die (mysql_error());

?>

 

I'm far too lazy to do it all for you :P

Thanks for the reply...

 

This works excellent, except when inserting in MySQL fields with date format.

Then the date that was stored changed to 1970-01-01.

 

So inserting updating an table without entering anything to date fields seams to be problem.

Maby i could fetch all the stored db info for that row and say:

 

<?php
if(isset($renset_dato) && $renset_dato !== "") { $sql .= "renset_dato = '$row[renset_dato]', "; } 
?>

Now I've have an issue that is bothering me... Can't seam to find out the problem.

 

I have this code:

<?php
$query = "SELECT * FROM $dbtable WHERE id = $_GET[id]";
$result = mysql_query($query) or die(mysql_error());
             while($row = mysql_fetch_array($result)) {
$test = $row['renset_dato'];
echo $test;
}
$sql = "UPDATE ${dbtable} SET ";
if(isset($renset_dato) && $renset_dato !== "") { $sql .= "renset_dato = '$renset_dato', "; } else { $sql .= "renset_dato = '$test', "; }

echo $sql;
?>

 

As you see I've echoed out two variables to see what happens.

$test results in: 2009-07-28

 

And when i leave $renset_dato empty by not filling in a date in the form, the $sql results in:

UPDATE dlight_service_records SET renset_dato = '1970-01-01',

 

Where does it get the 1970-01-01 date from at this point? it should look like:

UPDATE dlight_service_records SET renset_dato = '2009-07-28',

 

Thanks in advance...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.