tabatsoy Posted March 25, 2008 Share Posted March 25, 2008 Greetings earthlings! how can i add my data in sql thru php? example i have a table scores math = 3, english = 2, logic = 5, total = null how can i add them thru php Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/ Share on other sites More sharing options...
xtrafile Posted March 25, 2008 Share Posted March 25, 2008 1. Connect to your DB mysql_connect("localhost", "admin", "admin") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); 2. Insert that information into the DB mysql_query("INSERT INTO table (math,english,logic,total) VALUES('3', '2','5',NULL ) ") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500245 Share on other sites More sharing options...
ansarka Posted March 25, 2008 Share Posted March 25, 2008 write the sql code and run the code <?php // we connect to example.com and port 3307 $link = mysql_connect('host', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname); $insert_query="insert into tablename values('1','2','3')"; $result=mysql_query($insert_query); ?> Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500246 Share on other sites More sharing options...
tabatsoy Posted March 25, 2008 Author Share Posted March 25, 2008 thanks for the early reply but my problem is how to add the scores and total it in php math = 3, english = 2, logic = 3, totalscore null how can i add those 3 columns so i can get their sum in php example is totalscore is 8 Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500253 Share on other sites More sharing options...
ansarka Posted March 25, 2008 Share Posted March 25, 2008 <? ......... $total=$row_result['math']+$row_result['english']+$row_result['logic']; ?> Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500255 Share on other sites More sharing options...
tabatsoy Posted March 25, 2008 Author Share Posted March 25, 2008 do i still need to run the connection? please help i'm confused ??? Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500259 Share on other sites More sharing options...
tabatsoy Posted March 25, 2008 Author Share Posted March 25, 2008 <? $result = mysql_query("select * from examinee where firstname = '".$_SESSION['bago']."' and lastname = '".$_SESSION['bago2']."'"); $total = $row_result['math'] + $row_result['english'] + $row_result['logic']; echo $total; ?> is this correct? Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500262 Share on other sites More sharing options...
ansarka Posted March 25, 2008 Share Posted March 25, 2008 complete code <?php $link = mysql_connect('host', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname); $result = mysql_query("select * from examinee where firstname = '".$_SESSION['bago']."' and lastname = '".$_SESSION['bago2']."'"); while ($row_result = mysql_fetch_array($result)) { $total = $row_result['math'] + $row_result['english'] + $row_result['logic']; echo $total; } ?> Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500266 Share on other sites More sharing options...
tabatsoy Posted March 25, 2008 Author Share Posted March 25, 2008 thanks for the help it worked. i love this site. Link to comment https://forums.phpfreaks.com/topic/97770-how-to-add-data-in-mysql-thru-php/#findComment-500303 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.