Jump to content

Addition problem


kahodges

Recommended Posts

I'm trying to build a small php script to help automate adding vacation time to keep track of employees available vacation. I can run the sql below, and it executes fine:

Update `vacation` set avail_vacation = `avail_vacation` - `used` where employee = "employee1";

I'm trying to build this into a php function. This is what I have so far:

<?php
function sql_addition()
{
  global $conn;
  global $_POST;

  $sql = "update `vacation` set  `avail_vacation`= `avail_vacation` + `added` where employee = .sqlvalue(@$_POST["employee"])";
  mysql_query($sql, $conn) or die(mysql_error());
}
<html>
<tr>
<td class="hr"><?php echo htmlspecialchars("Add Hours")." " ?></td>
<td class="dr"><input type="text" name="added" value="<?php echo sql_addition('"', '"', trim($row["added"])) ?>"></td>
</tr>
?>
</html>

It's showing the error below where the field should be for submitting hours to add:

<input type="text" name="added" value="You have an error in your SQL syntax; check the manual that

corresponds to your MySQL server version for the right syntax to use near '' at line 1

Help would be greatly appreciated.

Link to comment
Share on other sites

Couple things:

 

-you call sql_addition with 3 arguments. the function declaration doesn't take in any

-missing a semi colon  <?php echo htmlspecialchars("Add Hours")." " ?>

-need single quotes on your query. double quotes is breaking it. "update `vacation` set  `avail_vacation`= `avail_vacation` + `added` where employee = .sqlvalue(@$_POST['employee'])"

Link to comment
Share on other sites

Your calling the function sql_addition() and passing it three arguments

<?php echo sql_addition('"', '"', trim($row["added"])) ?>

But you never use them

function sql_addition()
{
  global $conn;
  global $_POST;

  $sql = "update `vacation` set  `avail_vacation`= `avail_vacation` + `added` where employee = .sqlvalue(@$_POST["employee"])";
  mysql_query($sql, $conn) or die(mysql_error());
}

It is generally bad practice to define variables as global. You should instead pass these variables to the function

function sql_addition(&$conn, $employee)
{
    $sql = "update `vacation` set  `avail_vacation`= `avail_vacation` + `added` where employee = " . sqlvalue($employee);
    mysql_query($sql, $conn) or die(mysql_error());
}

echo sql_addition($conn, $_POST['employee']) ?>

 

 

Link to comment
Share on other sites

And, it is never necessary to declare "global $_POST;" - $_POST is automatically global.

 

To fix your error message, the first thing to do is to print out $sql whenever there is an error.  For example:

 

mysql_query($sql, $conn) or die("Error in $sql\n" . mysql_error());

 

Then you can see exactly what query is getting sent to mysql and having the error.  And use the code wildteen88 posted under "It is generally bad practice to ...", that has fixed the syntax problems, assuming sqlvalue() does what we're expecting it to.

Link to comment
Share on other sites

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.