FForce2195 Posted October 18, 2008 Share Posted October 18, 2008 Hello. I'm still a beginner in writing PHP programs. (3 weeks or so...) I hate to ask such basic questions. But I have run out of choices after spending several hours by trial and error. (1) I have a table ($table02) with 4 fields. One of the fields (num) in this table shows the numbers of the times in which the same IP addresses are recorded in another table ($table01). (Please see the attachment if necessary.) If I wanted to insert the same numbers shown in 'num' also into 'percent', I could write $sql_01 = "SELECT * FROM $table01"; $result_01 = mysql_query($sql_01,$my_sql_connect) or die(mysql_error()); $num_rows = mysql_num_rows($result_01); $sql_02 = "SELECT * FROM $table02"; $result_02 = mysql_query($sql_02,$my_sql_connect) or die(mysql_error()); WHILE ($row_ip = mysql_fetch_array($result_02)) { $insert_percent = "INSERT INTO $table02 (percent) VALUES ('".$row_ip['num']."')"; $insert_result = mysql_query($insert_percent,$my_sql_connect) or die(mysql_error()); } But I actually want to insert a percentage in each cell under 'percent' where a percentage is calculated by $row_ip['num']/$num_rows*100. If I write $insert_percent = "INSERT INTO $table02 (percent) VALUES ('".$row_ip['num']."/$num_rows*100')"; or even if I write $insert_percent = "INSERT INTO $table02 (percent) VALUES ('".$row_ip['num']."/30')"; the numbers in 'num' will be duplicated under 'percent.' What am I doing wrong? (2) I only want to insert numbers under 'percent.' If I use INSERT new rows will be inserted on top of existing ones. But I suppose I cannot use UPDATE in this situation. What can I do so that I can enter numbers only under 'percentage' without creating new rows. I'm sorry to ask such a basic question. I search for 'insert rows mysql' and read a few dozen matches. But I didn't find anything particular. Thank you for your time. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
KaeVectori Posted October 18, 2008 Share Posted October 18, 2008 why would you think you can't use UPDATE? if I understood rightly, You want to update the value of percentage column for an IP, right? Then I think you can do something like: $percent = <calculate your percentage~>; $ip = <get the ip addres here>; mysql_query("UPDATE table_name_here SET `percent` = $percent WHERE ip = '$ip'"); I think... D: Quote Link to comment Share on other sites More sharing options...
FForce2195 Posted October 18, 2008 Author Share Posted October 18, 2008 Umm... When I used UPDATE, the same number filled the entire field under 'percent.' And somebody told me that that's what should happen with UPDATE. I'll try again with UPDATE, then. Thanks. Quote Link to comment Share on other sites More sharing options...
FForce2195 Posted October 18, 2008 Author Share Posted October 18, 2008 $ip = <get the ip addres here>; No. I' want to fill any row with (".$row_ip['num']."/$num_rows*100). That's why I'm using WHILE ($row_ip = mysql_fetch_array($result_02)) { } Anyway, I'm very confused about when to use INSERT or UPDATE. Quote Link to comment Share on other sites More sharing options...
FForce2195 Posted October 18, 2008 Author Share Posted October 18, 2008 WHILE ($row_ip = mysql_fetch_array($result_02)) { $$insert_percent = "UPDATE $table02 SET percent = '".$row_ip['num']."'/$num_rows*100 WHERE num >= 0" $insert_result = mysql_query($insert_percent,$my_sql_connect) or die(mysql_error()); } fills the percentage field with the same number. Why? ??? I think this mistake takes me back to the first post where I said that '".$row_ip['num']."' cannot be divided or multiplied by anything. Quote Link to comment Share on other sites More sharing options...
FForce2195 Posted October 18, 2008 Author Share Posted October 18, 2008 Okay. The following works good. $insert_percent = "UPDATE $table02 SET percent = num/$num_rows*100 WHERE num >= 0"; Quote Link to comment 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.