473,395 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

PHP/MySQL UPDATE problem

//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
4 2186
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
5
by: princevejita1 | last post by:
Hello I have problem with my MySQL server and transactions. I installed MySQL server 5.0.11 with MySQL administrator & MySql Query Browser on Win XP Prof. I would like to use transactions, so I...
2
by: David | last post by:
Hi, Has anyone had this problem ? I am using MySQL ODBC 3.51 Driver, with MS Access 2003 and MySQL 4.1.11 standard log. I created my tables in MS Access, then exported them via ODBC to an...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.