Jump to content

Update row in MySQL with array


virthen

Recommended Posts

Hello,I'm having trouble updating a row in my db using the values from an array. Is my syntax correct? If not, how should it be? Is there anything I can improve on with what I have?<?php//my test array$testarray = array($a,$b,$c,$d,$e,$f,$g);//my update statementdb_query("UPDATE testtable SET col1=" . $testarray[0] . ",col2=" . $testarray[1] . ",col3=" . $testarray[2] . ",col4=" . $testarray[3] . ",col5=" . $testarray[4] . ",col6=" . $testarray[5] . ",col7=" . $testarray[6] . " WHERE testid='" . mysql_real_escape_string($testid) . "' AND testid2='" . mysql_real_escape_string($testid2) . "'");?>Thanks in advance!

Link to comment
Share on other sites

Hello,I'm having trouble updating a row in my db using the values from an array. Is my syntax correct? If not, how should it be? Is there anything I can improve on with what I have?<?php//my test array$testarray = array($a,$b,$c,$d,$e,$f,$g);//my update statementdb_query("UPDATE testtable SET col1=" . $testarray[0] . ",col2=" . $testarray[1] . ",col3=" . $testarray[2] . ",col4=" . $testarray[3] . ",col5=" . $testarray[4] . ",col6=" . $testarray[5] . ",col7=" . $testarray[6] . " WHERE testid='" . mysql_real_escape_string($testid) . "' AND testid2='" . mysql_real_escape_string($testid2) . "'");?>Thanks in advance!
Bump!
Link to comment
Share on other sites

Yes, its fine. You could do this:

<?php//my test array$testarray = array("col1" => $a,"col2" => $b,"col3" => $c,"col4" => $d,"col5" => $e,"col6" => $f,"col7" => $g);//Form query$sql = "UPDATE testtable SET ";foreach ($testarray as $column => $value) {$sql .= "$column = \"$value\" ";}$sql .= "WHERE testid='" . mysql_real_escape_string($testid) . "' AND testid2='" . mysql_real_escape_string($testid2) . "'";//my update statementdb_query($sql);?>

If the query is not working, try echoing mysql_error() after the query and see what it says.

Link to comment
Share on other sites

Yes, its fine. You could do this:
<?php//my test array$testarray = array("col1" => $a,"col2" => $b,"col3" => $c,"col4" => $d,"col5" => $e,"col6" => $f,"col7" => $g);//Form query$sql = "UPDATE testtable SET ";foreach ($testarray as $column => $value) {$sql .= "$column = \"$value\" ";}$sql .= "WHERE testid='" . mysql_real_escape_string($testid) . "' AND testid2='" . mysql_real_escape_string($testid2) . "'";//my update statementdb_query($sql);?>

If the query is not working, try echoing mysql_error() after the query and see what it says.

Thanks for the tips Synook! Hopefully I'll have this part of my project working soon.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...