0

I need some help putting together this PHP SQL update. I am pretty sure I need a foreach loop to post this query, but I am not sure how to write it.

Basically it needs to match ticketID from the string to ticketID in the database and update that row with the following developer.

The query string will look something like:

ticketID=1483&developer=Reme&ticketID=1484&developer=Reme&ticketID=1485&developer=Reme&isActive=1

Although there could be as many as 30/40 pairs with isActive being a variable to end it all. DBConn and all that is already set up, this is the last thing I need to solve before moving onto sessions.

This is being posted over using an Ajax call. Everything I need is arriving at its destination; it's just getting each pair and update in the database accordingly that I am stumped on.

2 Answers 2

1

You can't use the same parameter (ticketID) twice in a query string, because the second will overwrite the first.

In this case you have to use an array:

ticketID[]=1483&developer[]=Reme&ticketID[]=1484&developer[]=Reme&ticketID[]=1485&developer[]=Reme&isActive=1

And then you could use a foreach to loop.

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

6 Comments

Yes he can. He just won't be able to access the variables through $_REQUEST/$_GET/$_POST. He can access them however from the raw input.
@nl-x yes, you're right, but as he posted he will do this with an ajax post request, I assumed that he will access thie string with $_REQUEST (or similar).
I am actually using $_POST, never used $_REQUEST.
@nl-x sorry, but you mean that he can't get all the values in an array by using $_GET?
@ilpaijin not when posting it no. I can access my test sample using foreach ($_POST["ticketID"] as $ticket_id) { echo $ticket_id.','; }
|
0

It depends on how you want to update them, but I would suggest using JSON or some other more defined structure.

If you do

ticketID[]=1483&developer[]=Reme&ticketID[]=1484&developer[]=Reme&ticketID[]=1485&developer[]=Reme&isActive=1

you will have one array for ticketID, one array for developer, etc. Which means that you should be really careful with the other on which you are placing the parameters.

Instead of that I would prefer structure like this:

["isActive":1,
 "tickets":{"ticketID" :1483
            "developer": "Reme"},
           {"ticketID": 1484
            "developer": "Reme"},
           {"ticketID": 1485,
           "developer": "Reme"}]

On that you are confident that you are updating the right properties on the right object.

Comments

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.