0

I'm trying to update a table in my database, put my update query does not work. I have checked the code many times, but I can't see anything wrong with it. I'm calling a php-file and sending data over to it with javascript. Any ideas?

This is my php-file:

include 'db-connect.php';

$db->autocommit(false);

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

$id = $db->real_escape_string($request->ID);
$from = $db->real_escape_string($request->From);
$to = $db->real_escape_string($request->To);
$from_ampm = $db->real_escape_string($request->FromAMPM);
$to_ampm = $db->real_escape_string($request->ToAMPM);

$sql = "update OpeningHours set From = '$from', To = '$to', FromAMPM = '$from_ampm', ToAMPM = '$to_ampm' ";
$sql .= "where ID = '$id'";
$res = $db->query($sql);

$message = array();

if($db->affected_rows <= 0)
{
    $db->rollback();
    $message['error'] = "Could not edit. Contact IT manager.";
    echo json_encode($message);
    die();
}

$db->commit();
$db->close();
$message['error'] = "";
$message['success'] = "Edit Success!";
echo json_encode($message);

And this is the javascript part:

var request = $http({
                method: "post",
                url: "updateOpeningHoursItem.php",
                data: {
                    ID: $scope.openingHoursID,
                    From: $scope.from,
                    To: $scope.to,
                    FromAMPM: $scope.fromAMPM,
                    ToAMPM: $scope.toAMPM
                },
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            });

            request.success(function (data) {

                if(data.error === "")
                {
                    $scope.emptyOpeningHoursFields();
                    $scope.resetOpeningHoursEditForm();

                    swal("Success", data.success, "success");
                }
                else
                {
                    swal("Ops!", data.error, "error");
                }
            });
1
  • 1
    You might get a better response if you define more specifically what "not working" means. Commented Jun 2, 2015 at 4:54

3 Answers 3

2

Hello,

        $host = 'localhost'; 
        $user = 'root';
        $password = '';
        $database = 'youdatabasename';
        $con = mysql_connect($host,$user,$password);
        $res = mysql_select_db($database,$con);

        $postdata = file_get_contents("php://input");
        $request = json_decode($postdata);

        $id = $db->real_escape_string($request->ID);
        $from = $db->real_escape_string($request->From);
        $to = $db->real_escape_string($request->To);
        $from_ampm = $db->real_escape_string($request->FromAMPM);
        $to_ampm = $db->real_escape_string($request->ToAMPM);

        $sql = "UPDATE OpeningHours set From = '".$from."', To = '".$to."', FromAMPM = '".$from_ampm."', ToAMPM = '".$to_ampm."' where ID = '".$id."'";
        $result = mysql_query($sql) or die(mysql_error());

        if($result==0)
        {
            $message['error'] = "Could not edit. Contact IT manager.";
            echo json_encode($message);
            die();
        }

Please try with this code might be work if there any missing something.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but this will not work. Check Bjoern's answer
1

You used reserved words in your UPDATE statement, therefore you have to escape them properly using backticks.

Change your SQL statement to

$sql = "UPDATE OpeningHours 
        SET `From` = '".$from."', 
            `To` = '".$to."', 
            `FromAMPM` = '".$from_ampm."', 
            `ToAMPM` = '".$to_ampm."'
        WHERE `ID` = '".$id."'";

Comments

1

You have also used reserved words in you query. Put them in backticks. You can debug this by echo your query. The query should be like this.

$sql = "UPDATE OpeningHours set From = '".$from."', To = '".$to."', FromAMPM = '".$from_ampm."', ToAMPM = '".$to_ampm."' where ID = '".$id."'";
    $result = mysql_query($sql) or die(mysql_error());

1 Comment

'$from' is completely valid, in case you didn't know that. Check Bjoern's answer to see what I did wrong :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.