0

I have a problem. The story so far:

PHP1 reads an array from the database, that contains only integers - it has the following form:
[0, 70, 44, ...]

Because the number of entities it contains varies, I want to be able to read and store the whole array into one cell of the database - the solution must not disregard this, then.

PHP1 contains some JS, that allows a user to do something on the website, which alters one of the entities in the array, which makes it, e.g.;
[0, 75, 44, ...]

So far, so good.

Now I want this new array to replace the one in the database, this is the central goal that I fail to achieve.


What I'm currently working with - which isn't working:

PHP1 executes some AJAX magic, and sends this array to PHP2, which works fine:

var arrayX = [0, 75, 44, ...];
var arrayY = JSON.stringify(arrayX);

$.ajax({
    url: 'PHP2.php',
    type: 'post',
    data: {arrayY: arrayY }
});

PHP2 then connects to the DB, and attempts to update that one cell with the new array, by means of the following, which doesn't work (!):

$arrayZ = json_decode($_POST['arrayY'], true);

mysql_query("UPDATE userbase SET db_column = $arrayZ WHERE id=0", $con);

mysql_close($con);

I've tried serializing $arrayZ in PHP2, as well as a whole set of other solutions I found on Stackoverflow, but none of them worked (or I didn't apply them correctly of course) and now I've found myself deadstruck...


I'm hoping your talents will get me further than my own have!

6
  • 2
    Your database schema is flawed - having multivalued cells is a horrible idea. Commented Jul 17, 2013 at 14:13
  • "dont work" is not much...please provide more informations why it is not working Commented Jul 17, 2013 at 14:13
  • please do a print_r($_POST); at the top of PHP2 and post your output here. Commented Jul 17, 2013 at 14:14
  • it seems that you dont need to JSON.stringify because its already json Commented Jul 17, 2013 at 14:15
  • Steven, this is what I get: Array ( [arrayZ] => [1,70] ) <br /> <b>Warning</b>: Cannot modify header information - headers already sent by (output started at .../PHP2.php:3) in <b>.../PHP2.php</b> on line <b>5</b><br /> <br /> <b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at .../PHP2.php:3) in <b>.../PHP2.php</b> on line <b>7</b><br /> Commented Jul 17, 2013 at 14:39

1 Answer 1

3

I assume db_column holds a string value, and as such you probably just need quotes around $arrayZ in your SQL string.

mysql_query("UPDATE userbase SET db_column = '$arrayZ' WHERE id=0", $con);

But as Fake51 pointed out, your database schema is flawed.

Also, you're susceptible to a SQL Injection attack.

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

2 Comments

then the DB contains only 'array', instead of the actual array.
@victor Create a one-to-many relationship to store these values. What you're doing here is a bad idea.

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.