By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

PHP/MySQL UPDATE problem

P: n/a
//GET INSERTED CHUNK ID
$getid = mysql_query("SELECT id FROM chunks WHERE chunk = '$chunk'");
$idrow = mysql_fetch_array($getid, MYSQL_ASSOC);
$chunkid = $idrow["id"].",";
$eachtag = explode(",", $tags);
foreach($eachtag as $key=>$value) {
For some reason, this code:

$updated = $row["chunks"] . $chunkid;
mysql_query("UPDATE tagtochunk SET chunks = '$updated' WHERE tag =
'$value'");

only updates the field with $chunkid, leaving out row["chunks"]. I'm
guessing it may have something to do with scope within control
structures.... I'm at a loss.

$query = "SELECT chunks FROM tagtochunk WHERE tag = '$value'";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0) {

//CREATE NEW TAG ENTRY
mysql_query("INSERT INTO tagtochunk (tag, chunks) VALUES ('$value',
'$chunkid')");

} else {

//UPDATE CUR TAG WITH NEW CHUNKID
$updated = $row["chunks"].$chunkid;
mysql_query("UPDATE tagtochunk SET chunks = '$updated' WHERE tag =
'$value'");

}

}

mysql_close($con);
?>

Feb 17 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ma*****@gmail.com kirjoitti:
//GET INSERTED CHUNK ID
$getid = mysql_query("SELECT id FROM chunks WHERE chunk = '$chunk'");
$idrow = mysql_fetch_array($getid, MYSQL_ASSOC);
$chunkid = $idrow["id"].",";
$eachtag = explode(",", $tags);
foreach($eachtag as $key=>$value) {
For some reason, this code:

$updated = $row["chunks"] . $chunkid;
Where does $row come from? Earlier in the code you use $idrow. Try
echoing the value of $updated and print_r($row);

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 17 '07 #2

P: n/a
That was a good catch, unfortunately it didn't do the trick.
Here the code is updated with $row.

$getid = mysql_query("SELECT id FROM chunks WHERE chunk = '$chunk'");
$idrow = mysql_fetch_array($getid, MYSQL_ASSOC);
$chunkid = $idrow['id'] . ",";
$eachtag = explode(",", $tags);
foreach($eachtag as $key=>$value) {

$query = "SELECT tag FROM tagtochunk WHERE tag = '$value'";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0) {

//CREATE NEW TAG ENTRY
mysql_query("INSERT INTO tagtochunk VALUES ('$value', '$chunkid')");

} else {

//UPDATE CUR TAG WITH NEW CHUNKID
$query = "SELECT tag FROM tagtochunk WHERE tag = '$value'";
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$updated = $row["chunks"] . $chunkid;
mysql_query("UPDATE tagtochunk SET chunks = '$updated' WHERE tag =
'$value'");

}

}

Feb 17 '07 #3

P: n/a
That was a good catch, unfortunately it didn't do the trick.
Here the code is updated with $row.

//GET INSERTED CHUNK ID
$getid = mysql_query("SELECT id FROM chunks WHERE chunk = '$chunk'");
$idrow = mysql_fetch_array($getid, MYSQL_ASSOC);
$chunkid = $idrow["id"].",";
$eachtag = explode(",", $tags);
foreach($eachtag as $key=>$value) {

$query = "SELECT chunks FROM tagtochunk WHERE tag = '$value'";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0) {

//CREATE NEW TAG ENTRY
mysql_query("INSERT INTO tagtochunk (tag, chunks) VALUES ('$value',
'$chunkid')");

} else {

//UPDATE CUR TAG WITH NEW CHUNKID
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$updated = $row["chunks"].$chunkid;
mysql_query("UPDATE tagtochunk SET chunks = '$updated' WHERE tag =
'$value'");

}

}
Feb 17 '07 #4

P: n/a
ma*****@gmail.com kirjoitti:
$query = "SELECT chunks FROM tagtochunk WHERE tag = '$value'";
....
$row = mysql_fetch_array($query, MYSQL_ASSOC);
I've done this mistake many times. See, $query is the actual query
string, you need $result for mysql_fetch_array() :)

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.