I can't update my database by using select .. i've tried searching for different solutions but those didn't helped me ..
any ideas why this doesn't update my DB??

i need to update the row with such conditions:
the query/queries i used

$records = mysql_query("SELECT * FROM cot_cargovolume WHERE VoyageNo = '$_POST[txtVoyageNo]'"); 
$cvCOC = array($records);
$i = 0;
while($con = mysql_fetch_array($records)){
    $cvCOC[$i] = $con['COC'];

    $CV_contents = array("$_POST[cargoVol1]","$_POST[cargoVol2]");
    $sql = "UPDATE cot_cargovolume SET CIC='CV_contents[$j]' WHERE VoyageNo='$_POST[txtVoyageNo]' AND COC = '$cvCOC[$j]'";

    /*ERROR Report*/
         if (!mysql_query($sql))
          die('Error: ' . mysql_error());
        echo "Updated - Details</br>";


the first query is that i want to retrieve the data from the column coc and use it for the condition as to where i want the update to happen. the VoyageNo works properly but i need the COC condition too..

i don't get any error using this code and it says "Updated details" but it doesn't really ..
thanks for any help, greatly appreciate it ..

Edited by diafol: This is PHP - it's not javascript

5 Years
Discussion Span
Last Post by diafol

Echo the query before it runs, to see what it contains. Then you can run it against your database and see what happens.


ok i'll take that idea, though i changed my approach regarding this problem, i just retrieved the data from the DB and used it for the condition WHERE of the UPDATE .


I really can't see what you're trying to do here. You're retrieving all records that has a certain voyage number to get their 'coc' values.
Then you are trying to update the same recordset.
Do you need cvCOC at all? It will be the same as last time (select) anyway?
THe $j variable bothers me too. Say $i = 10 (after running in your loop 10 times), then you run $j 10 times in its own loop, BUT you have:

CV_contents[$j] in the SQL - which probably should be $CV_contents[$j] with a set of braces around it: {$CV_contents[$j]} and the variable itself only has 2 items

$CV_contents = array("$_POST[cargoVol1]","$_POST[cargoVol2]");

BTW - no need for the surrounding x2 quotes and I'd suggest wrapping the item name in single quotes. BUT as this is to be used in a mysql statement, it needs to be cleaned with something like mysql_real_escape_string().

$CV_contents = array($_POST['cargoVol1'],$_POST['cargoVol2']);

So you'll soon get an error that the array index for $CV_contents (e.g. $CV_contents[3]) does not exist.

This is pretty messy. The more I look at it, the less sense it makes.

Could you have another think and let us know what exactly you are trying to accomplish with the routine?


as far as i can remember i've tried using single quotes but it didn't work, so i tried to use double quotes which actually worked. but oh well, i'll take your advice, i'll rewrite my code then.

you're right btw it looks messy especially now that i've solve my problem but the thing is i don't think that this is the right approach to what i've been trying to achieve.

actually the problem is i just simply want to update a row based from the voyageNo provided by the user and based from the COC which has already been saved in the database .. (i know it's confusing, and i think i made a mistake in creating the database due to some reason and i just need to go with what i have now because i'm running out of time..)


variables should be cleaned using mysql_real_escape_string()
which i suppose the variable will look like this (for example)
$var = mysql_real_escape_string($var);

but what if i have a bunch of variables let's say

$portA = array(
$portB = array(
$portC = array(
$portD = array(
$portE = array(
$portF = array(
$portG = array(

this are the cell values from the table i have in the form ..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.