-1

So i have this code to update a table in my database. $C1, $C2, etc. are a set of array from previous calculation :

    $Cupdate = array($C1, $C2, $C3, $C4, $C5, $C6);

    array_unshift($Cupdate, null);
    $Cfinalupdate = call_user_func_array('array_map', $Cupdate);

    if(is_array($Cfinalupdate)){
        $valuesArr = array();
        foreach($Cfinalupdate as $row){
            $sqlup = "UPDATE tempselect SET ";

            $C1up = mysql_real_escape_string( $row['0'] );
            $C2up = mysql_real_escape_string( $row['1'] );
            $C3up = mysql_real_escape_string( $row['2'] );
            $C4up = mysql_real_escape_string( $row['3'] );
            $C5up = mysql_real_escape_string( $row['4'] );
            $C6up = mysql_real_escape_string( $row['5'] );

            $valuesArr[] = "C1 = '$C1up', C2 = '$C2up', C3 = '$C3up', C4 = '$C4up', C5 = '$C5up', C6 = '$C6up'";
            $sqlup .= implode(',', $valuesArr);
            mysql_query($sqlup) or exit(mysql_error()); 
        }
    }

But code above only put the last row of $Cfinalupdate, suppose i want to update my database become like this :

C1   C2   C3   C4   C5   C6
1    4    2    3    1    5
6    2    4    1    2    7
8    1    2    5    4    6

But it turns out like this when i see in my database table :

C1   C2   C3   C4   C5   C6
8    1    2    5    4    6
8    1    2    5    4    6
8    1    2    5    4    6

I've tried this code to INSERT a set of array into database, but it works perfectly, but why it only update with the last row of the array? thank you.

lacyeex
  • 175
  • 2
  • 10
  • 2
    probably because of no WHERE clause and it's updating your entire db – Funk Forty Niner May 18 '16 at 16:25
  • @Fred-ii- i see, wait i'll try – lacyeex May 18 '16 at 16:26
  • plus, if those are your total columns, you'll need to add an "id" (int) column as an example and possibly make it AI. it will go better also – Funk Forty Niner May 18 '16 at 16:27
  • 1
    my comments should be converted to an answer because that's what the solution here is. and you'll need to ALTER your table in order to add an id column or whatever name you want for it. – Funk Forty Niner May 18 '16 at 16:42
  • ok well no response. ping me whenever, I have other things to do. – Funk Forty Niner May 18 '16 at 16:48
  • @Fred-ii- hmm.. so i try to put `WHERE` clause in my code, but it occurred a new error `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'C1 = '2.00',` Ah i also try use back tick at C1, C2, etc. – lacyeex May 18 '16 at 16:49
  • you'll need to update your question then with the code you're now using along with what the db schema is. My *Spidey sense* is also tingling that some poor *you know what*, is sitting in the sidelines here..... oh as I am typing this....... I was right. *sigh* – Funk Forty Niner May 18 '16 at 16:50
  • after all the time I spent with you, you're asking the guy who gave you an answer. I am so out of here. bye – Funk Forty Niner May 18 '16 at 16:52
  • @Fred-ii- lol i'm trying to ask every guy who give me an answer, i even try your suggestion anyway. THANK YOU for responding – lacyeex May 18 '16 at 16:54
  • 1
    I told you to post your code in your question and you didn't do that. I'm sitting here like freaking bozo on a wire and wanting to provide you with a solution, then you go elsewhere, wow. well good luck with this. Your impression on me here wasn't good. I sincerely wish you well with this. Oh and all he did was give you a link... pretty low-quality answer. – Funk Forty Niner May 18 '16 at 16:56
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 18 '16 at 17:00
  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 18 '16 at 17:00
  • @Fred-ii- dude you need to calm down, and yes while you waiting for me, i'm trying to code right here, it doesn't mean i don't care with your answer dude, of course i need some time to think, and yeah sorry i'm a beginner anyway. yes you're right, your answer is the right one. sorry for my bad attitude – lacyeex May 18 '16 at 17:00

1 Answers1

0

It's simple...

As Fred -ii- says:

The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

More info: W3Schools - SQL UPDATE Statement

Thomas Galue
  • 160
  • 9
  • 2
    why are my instincts always "bang on". – Funk Forty Niner May 18 '16 at 16:51
  • Hello Thomas! thank you for responding my question. So as Fred said, i try to put `WHERE` clause in my code, but it occurred a new error `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'C1 = '2.00',....` Ah i also try use back tick at C1, C2, etc. – lacyeex May 18 '16 at 16:52
  • Rebuild your DB, add a ID field and write again almost all your code, essentially because you're using `Mysql_*` and it's deprecated, upgrade to `Mysqli_*`. – Thomas Galue May 18 '16 at 16:56