Jump to content

Recommended Posts

So, hmm.. I'd like to UPDATE a datetime field ONLY IF the database holds "" null already. This works using the checkbox. However, if I do not tick the checkbox, then it tries to update the datetime field in the database with "" null. This gives the error:

 

Error: Incorrect datetime value: '' for column 'Started' at row 1

 

Of course it is correct, "" is not a correct datetime value. Does anyone know how I can simply get it to do nothing if the 'Started' box is not checked?

 

cod:

 

<?php
include("functionlibrary.php");
include("menu.php");
connect();
mysql_select_db("itdb");

// After selecting DB, some database field's values are put into vars. **
$started_query = mysql_query("SELECT Started FROM jobs
WHERE
`ID`='".$_POST["id"]."'");
$query_row = mysql_fetch_array($started_query, MYSQL_ASSOC);

IF (($_POST['started'] == "chk") && ($query_row['Started'] == ""))
	{ 
	$started_store = date('Y-m-d H:i:s');
	}ELSE
	{
	}
// Checks if Box is checked AND(&&) if database has no value in that field already. 
// ---------------------- Start Update ----- 
$sql="UPDATE jobs SET Priority='".$_POST["priority"]."', `IT Staff`='".$_POST["itstaff"]."', `Started`='".$started_store."'
WHERE
`ID`='".$_POST["id"]."'";

// echo $sql."\n<br>\n<br>";
if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
echo "<font color=\"green\"><br><br>1 record amended";
mysql_close();

?>

 

Thankyou in advance and kisses.

Link to comment
https://forums.phpfreaks.com/topic/71930-updating-null-into-mysql/
Share on other sites

RULE #0) Never use any raw user submitted data in any output. Ever.

 

"Any output" includes all sql queries.  Even if you're only running a select query, you have to consider the text of the query as output to the database.  The piece of code I quoted is open to sql injection attacks.

 

Filtering data simply means putting it through a series of checks to make sure that it is safe to be used in sql queries or output as html.  It should be standard practice.  A good approach would be something this.

 

<?php

// this array is for storing filtered data
// it will eventually have an element corresponding to every element in $_POST
$clean = Array()
$sqldata = Array() // contains escaped output for use in queries

if(isset($_POST['username']))
{
  if($_POST['username'] != '')
  {
    if(preg_match(CONST_REGEX, $_POST['username']))
    {
      if(...   )
      {
        $clean['username'] = $_POST['username'];
        $sqldata['username'] = mysql_real_escape_string($clean['username']);
        $html['username'] = htmlentities($clean['username']);
      }
    }
  }
}

// from this point forward you don't touch $_POST
// you only use $clean, $sqldata, and $html

?>

 

In the filtering process, you can check the value of $clean['username'] and set $sqldata['username'] accordingly so that it will drop into your sql statement correctly.

I see, thankyou Vomit ;] (brings such a pleasant image... ._.)

 

I definetly needed to know that, although, in this case it doesn't matter, because the site im putting together is just something that will be used internally in a company, and not open to the outside www.

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.