Jump to content

Arrays + MySQL


lordfirex

Recommended Posts

Hey guys, i am having a problem and for the life of my i can't think of how to get around this problem.

 

All the variables below are arrays and the code below is what i have done so far. The problem is when updating the records it only puts in "Array" in all of the fields being updated. How should i go about re-doing it so that when the first array in $idnumber is called. All the other arrays (Date, Description, Name) have their 1st array information allocated to a variable??

 

 

$Date = $_POST['Date'];
$Name = $_POST['Name'];
$Description = $_POST['Description'];
$idnumber = $_POST['ID'];

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Picture_Blog", $con);


foreach ($idnumber as $ID) {

mysql_query("UPDATE slidorion SET Name = '$Name' WHERE ID = '$ID'");
mysql_query("UPDATE slidorion SET Description = '$Description' WHERE ID = '$ID'");
mysql_query("UPDATE slidorion SET Date = '$Date' WHERE ID = '$ID'");

}

Link to comment
Share on other sites

It works! Well sort of because it's edditting it's often getting $ID = 27, but it might be set out like ID[27, 3, 7, 9]

And of course im after Description[27, 3, 7,9]

So it's not working. Is there a way i can convert ID[27, 3, 7, 9] to say ref[1, 2, 3, 4] ???

 

 

Link to comment
Share on other sites

I managed to fix it by adding a count, although i must say thank you you were helpful in making me think :)

 

 

$Date = $_POST['Date'];
$Name = $_POST['Name'];
$Description = $_POST['Description'];
$idnumber = $_POST['ID'];

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Picture_Blog", $con);

$count = 0;

foreach ($idnumber as $ID) {


mysql_query("UPDATE slidorion SET Name = '{$Name["$count"]}' WHERE ID = '$ID'");
mysql_query("UPDATE slidorion SET Description = '{$Description["$count"]}' WHERE ID = '$ID'");
mysql_query("UPDATE slidorion SET Date = '{$Date["$count"]}' WHERE ID = '$ID'");


$count = $count +1;
}

Link to comment
Share on other sites

that will do it.  I just thought of another one:

foreach ($idnumber as $ref => $ID) {
  mysql_query("UPDATE slidorion SET Name = '{$Name['$ref']}' WHERE ID = '$ID'");
  mysql_query("UPDATE slidorion SET Description = '{$Description['$ref']}' WHERE ID = '$ID'");
  mysql_query("UPDATE slidorion SET Date = '{$Date['$ref']}' WHERE ID = '$ID'");
}

Don't know why I didn't think of that sooner...though it has been a long bloody week :/

 

Glad you got it working anyway!

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.