简体   繁体   中英

Inserting value in a certain column

I have a php program and i want to insert values into certain column. here is my table.

form_no | fullname | january | february | march | april | may | june | july
11111     smith, john

what is the right query code in doing this? and the right way in checking if the cell already contains a value? raw table: https://drive.google.com/file/d/0B99TeByt30n2eHdLM0FER210cWM/edit?usp=sharing

and here is my code in which i also tried to check if that cell has already a value.

$value_result = mysql_query("SELECT january FROM table_2014 WHERE form_no = '$formnumber'");
$value_rownum = mysql_num_rows($value_result);
if (!$value_result) {
    die(mysql_error());
}
if ( $value_rownum > 0 ) {
    echo "Already contributed!";
} else {
    $sql = "UPDATE `table_2014` SET january = '$contri_amnt' WHERE form_no = '$formnumber'";

    if (!mysql_query($sql,$con)) {
         die('Error: ' . mysql_error());
    }
}

Even if January is NULL you will still get a row, so counting the rows will give you 1, and you will never update it.

You need to use mysql_fetch_assoc or mysql_fetch_object to check if the result actually has a value. OR, change your query to

SELECT january FROM table_2014 WHERE form_no = '$formnumber' AND january IS NOT NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM