Treps
Treps

Reputation: 800

MySQL: UPDATE database with JQuery sortable new value

I've got this:

foreach($_POST['pos'] as $value) {
   $new_value = "UPDATE users SET regnr='" . $value . "' 
   WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
}

// Connect to database
$opendb = mysql_connect($dbhost, $dbuser, $dbpass) or die("Kunde inte ansluta till MySQL:<br>" . mysql_error());
mysql_select_db($dbname) or die("Kunde inte ansluta till databasen:<br>" . mysql_error());

mysql_query($new_value) or die(mysql_error());  

// Close database
mysql_close($opendb);

Information:

$_POST['pos'] holds a value from the database in a hidden input. This value I have choosen to split with str_split($r['regnr'], 6); into a JQuery sortable list. If I type echo $value; in the foreach loop I've got the new value (not splitted, as I want) from the JQuery sortable list. I need all values from the list, and I get it with echo. But if I use $value variable to UPDATE the database that it came from, it just updates with the last value from the JQuery sortable list.

Can someone solve that? :D

Upvotes: 0

Views: 377

Answers (1)

Treps
Treps

Reputation: 800

Here is the solution:

$str = '';

foreach($_POST['pos'] as $value) {
  $str = $str.$value;
}

// Connect to database
$opendb = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$new_value = "UPDATE users SET regnr='" . $str . "' WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
mysql_query($new_value) or die(mysql_error());

// Close database
mysql_close($opendb);

Upvotes: 1

Related Questions