We help IT Professionals succeed at work.

PHP/MySQL query producing errors - problems with multiple query parameters

Sandy Sailer
Sandy Sailer asked
Can anybody tell me what is wrong with this query?  
$con = mysql_connect("localhost","username","password");
if (!$con)
  die('Could not connect: ' . mysql_error());
$startDate_getData = "-1";
if (isset($_POST['startDate'])) {
  $startDate_getData = date("Y-m-d", strtotime($_POST["startDate"]));
if (isset($_POST['endDate'])) {
  $endDate_getData = date("Y-m-d", strtotime($_POST["endDate"]));
mysql_select_db("timesheets", $con);
mysql_query("UPDATE employeesxjobsxlaborcodes SET employeesxjobsxlaborcodes.committed = 1
WHERE dateWorked >= $startDate_getData AND dateWorked <= $endDate_getData", GetSQLValueString($startDate_getData, "date"),GetSQLValueString($endDate_getData, "date"));

When I load the page with the query on it, I get the following error:
Warning: mysql_query() expects at most 2 parameters, 3 given in /home/citstime/domains/citstime.com/public_html/qryCommit.php on line 77.

Line 77 is this line:  WHERE dateWorked >= $startDate_getData AND dateWorked <= $endDate_getData", GetSQLValueString($startDate_getData, "date"),GetSQLValueString($endDate_getData, "date"));
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

The 'parameters' are comma delimited and you have three sections.  The second two..

, GetSQLValueString($startDate_getData, "date"),GetSQLValueString($endDate_getData, "date")

Open in new window

don't make any sense in that context.  "GetSQLValueString" appears to be a routine from Dreamweaver.  While it might work to do a substitution in a different function, it's not going to work in mysql_query().
Top Expert 2012
mysql_query only accepts the SQL string and the optional connection information (if not specified, it uses current connection).  The problem here is you are sending GetSQLValueString($startDate_getData, "date"),GetSQLValueString($endDate_getData, "date") as additional parameters.  You should form your SQL string and store it to a variable first then just pass that variable to mysql_query.  Something like:

$my_query = "UPDATE employeesxjobsxlaborcodes SET employeesxjobsxlaborcodes.committed = 1 WHERE dateWorked >= ". GetSQLValueString($startDate_getData, "date") ." AND dateWorked <= ". GetSQLValueString($endDate_getData, "date");


Open in new window

Note: untested.  I don't really code in PHP but I understand it a bit.
Sandy SailerWebSailer Web Design


The example you provided was perfect and flawless!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.