sitinuraini Posted March 21, 2011 Share Posted March 21, 2011 HELLO, I HAVE A PROBLEM TO INSERT DATA FROM OUTPUT FROM FUNCTION()... I WANT TO STORE THE OUTPUT IN DATABASE, BUT FAILED TO INSERT... =============================================== BELOW IS MY FUNCTION() CODE, academiaTotal.php =============================================== <?php $page = 'fkpacademia.php'; //connect db function totalMac_ac1(){ $query1 = "SELECT AVG(MAC) FROM academia1"; $result1 = mysql_query($query1) or die(mysql_error()); while($row1 = mysql_fetch_array($result1)){ echo "".number_format($row1['AVG(MAC)'],1); $sql = "INSERT INTO fkpacademia (MAC) VALUES ('$row1['AVG(MAC)']')"; } } ---?> ====================================== MY CALL FUNCTION fkpacademia.php ====================================== <?php require 'academiaTotal.php' ?> <? totalMac_ac1(); ?> ********************************************** MY PROBLEM NOW IS THE sql(INSERT, (AVG(MAC) output... )) NOT SUCCESS... NO DATA INSERTED IN fkpacademia table.. AND I WANT TO UPDATE ALWAYS (AVG(MAC) from academiaTotal )... (MEANS THAT fkpacademia only have 1 record of AVG(MAC)...) hope anyone can help me... Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/ Share on other sites More sharing options...
sunfighter Posted March 21, 2011 Share Posted March 21, 2011 This looks like your doing an INSERT and you should be doing an UPDATE. Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1190137 Share on other sites More sharing options...
sitinuraini Posted March 21, 2011 Author Share Posted March 21, 2011 i have ask my friend about that.. should i need to insert and update also.. update must do in sql right? but, the problem now is i think the insert above not send any output. cause my table still empty.. i need to store the output from function(), store into db.. so that i do insert but not working... or me myself do wrong for above code..? can u explain to me clearly? Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1190140 Share on other sites More sharing options...
sitinuraini Posted March 21, 2011 Author Share Posted March 21, 2011 phpMyAdmin - 2.11.7 Server version: 5.0.51b-community-nt before this.... ......................... function totalMac_ac1(){ $query1 = "SELECT AVG(MAC) FROM academia1"; $result1 = mysql_query($query1) or die(mysql_error()); while($row1 = mysql_fetch_array($result1)){ echo "".number_format($row1['AVG(MAC)'],1); $sql = "INSERT INTO fkpacademia (MAC) VALUES ('$row1['AVG(MAC)']')"; $result=mysql_query($sql); } } .......................... just now i have this, bigTotal.php ============== function bigMac_ac1(){ $sql = "INSERT INTO fkpacademia (MAC) VALUES ('$row1['AVG(MAC)']')"; $result=mysql_query($sql); $query1a = "SELECT AVG(MAC) FROM fkpacademia"; $result1a = mysql_query($query1a) or die(mysql_error()); while($row1 = mysql_fetch_array($result1a)){ echo "".number_format($row1['AVG(MAC)'],1); } } ======================== fkpacademia.php ======================== <?php require 'bigTotal.php' ?> <? bigMac_ac1(); ?> problem: no data store in table fkpacademia.. please somenone help me where the wrong... Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1190203 Share on other sites More sharing options...
sunfighter Posted March 22, 2011 Share Posted March 22, 2011 I put your original code into my editor and it showed an error on your insert line. Try changing things to this and let me know.. echo "".number_format($row1['AVG(fcmb)'],1); $NUM = number_format($row1['AVG(fcmb)'],1); $sql = "INSERT INTO fkpacademia (MAC) VALUES ($NUM)"; $result=mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1190671 Share on other sites More sharing options...
sitinuraini Posted March 22, 2011 Author Share Posted March 22, 2011 thanks sunfighter... i have try ur idea, it give what i want (to store in fkpacademia table), but a little problem, it goes to double row from the academia... (speak broken english..sorry), and so that i just change back to be like this, echo "".number_format($row1['AVG(MAC)'],1); $NUM = number_format($row1['AVG(MAC)'],1); $sql = "INSERT INTO fkpacademia (MAC) VALUES ($NUM)"; $result=mysql_query($sql); just change the name variable, it give only one row inserted.. and every changes of AVG, it go to next row in the table... (means i should do UPDATE here)... but its okay.. my problem solved right now.. THANKS A LOT... Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1190674 Share on other sites More sharing options...
sitinuraini Posted March 22, 2011 Author Share Posted March 22, 2011 ------------------------------------------- $sqll = "UPDATE fkpacademia SET MAC = '$NUM' WHERE BIL='$BIL' "; $result=mysql_query($sqll); anyone, data above will be updated, after next to next row.. i want it updated from their row only... like i want to set, $NUM1 = $BIL1, $NUM2 = $BIL2, something like that... how can i do that..? i'm beginner in programming... Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1190740 Share on other sites More sharing options...
fenway Posted March 30, 2011 Share Posted March 30, 2011 So, just use multiple name=val for SET. Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1193966 Share on other sites More sharing options...
sitinuraini Posted March 30, 2011 Author Share Posted March 30, 2011 thanks fenway... i have solved this.. see other example.. can also use ON DUPLICATE KEY UPDATE is it the message "Rules" sent for my attention? im sorry.. Quote Link to comment https://forums.phpfreaks.com/topic/231238-failed-to-insert-data-from-function/#findComment-1193980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.