redarrow Posted February 24, 2009 Share Posted February 24, 2009 if i want to use the $podcast_programming_exspertise varable to insert to a database, how can i post it to the database without sticking the insert query inside the loop. <?php if($_POST['podcast_programming_exspertise']){foreach($_POST['podcast_programming_exspertise'] as $podcast_programming_exspertise) { //dont want a database query here what my option's echo "$podcast_programming_exspertise <br><br>"; }} >< because there around 100 off foreach on the page i need a way to get the info in the database. the data comes out lovely with around 10 different words but i need a way to post the variable in it coded way but not adding a insert in the current code. The way it works, is a user get to answer questions via a form the form is arrayed to get all the questions. Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/ Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 Erm.. What! I guess you could use MySQLi and implode the array! but the simple truth is if you want to insert a 100 rows then you need to insert 100 rows! If you gave reasoning why you don't want to do it this why may help! Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769964 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 sorry i was told that if you use a loop with a sql query it bad. sorry i read this on here from users, that suppose to be expert advice. so are you saying putting a insert in a loop is ok. Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769966 Share on other sites More sharing options...
trq Posted February 24, 2009 Share Posted February 24, 2009 sorry i was told that if you use a loop with a sql query it bad. sorry i read this on here from users, that suppose to be experts going buy that. Yeah, and those few sentenses make heaps of sense too. PHP's mysql extension will not let you execute multiple queries at once so (depending on your database schema) it is likely you need to execute mltiple quiries within a loop. We need to see your database schema however and a small example of some data. Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769967 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 if you need to insert 100 rows every post or 5 minutes then yeah it will overload your SQL server, its about the design of the system! but your question was how can i insert 100 records without using inserting 100 records.. Now you may want to look into MySQLI, you could structor your SQL query like this INSERT INTO `list1` (`ID`, `Name`) VALUES (1, 'Dogs'), (2, 'Trees'), (3, 'Apples'); but thats more SQL than PHP, i won't insert more than 50 records like that but that would mean you need to open 2 connections instead of 100 Either way it depends on the design.. For example the statment above would insert 3 records, now you could assume thats more data then 1 record! but not if the one records contains an 16gb Blog!.. again its all about the design! Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769970 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 what do i do to get the info in the database correctly. do i create a function for the insert then add that function inside the loop's a and b and c got 10 quistions. the databse type is text as abc will go in with the 10 answers with a space. <?php if($_POST['a']){foreach($_POST['a'] as $a) { //dont want a database query here what my option's echo "$a <br><br>"; }} if($_POST['b']){foreach($_POST['b'] as $b) { //dont want a database query here what my option's echo "$b <br><br>"; }} if($_POST['c']){foreach($_POST['c'] as $b) { //dont want a database query here what my option's echo "$c <br><br>"; }} ?> //form example only <input type="radio" name="a[]" value="yes" checked>Yes. <br> <input type="radio" name="a[]" value="no">No. <input type="radio" name="b[]" value="yes" checked>Yes. <br> <input type="radio" name="b[]" value="no">No. <input type="radio" name="c[]" value="yes" checked>Yes. <br> <input type="radio" name="c[]" value="no">No. Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769971 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 it depends on the schema,, You need to plan it out! your example only is pointless.. you have A to C and an array or 10 questions! so that doesn't make sense! Planning Planning Planning Planning Planning Planning !!!! Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769973 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 well be tend there 10 questions from the form it was a quick example ever think correct here. Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769974 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 Well thats like me pretending it was design and planned correctly.. See perfect! Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769976 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 here the full code so you can see my point. no insert added yet don't no how to because off all the foreach data every think works just need to get me head around getting the info in the database correctly and safely. The information is echoed out so i no all the array and variables work. i no that i need to add a insert where the echo is , but need to no how with all the foreach there. the way the info comes from the form is good but now need to get rid off the echo and insert to database. <?php session_start(); include("php_podcast_header.php"); require_once("php_podcast_function.php"); if(isset($_POST['submit'])){ $podcast_username=$_POST['podcast_username']; $podcast_password=$_POST['podcast_password']; $podcast_email_address=$_POST['podcast_email_address']; $podcast_programming_name=$_POST['podcast_programming_name']; $podcast_programming_years=$_POST['podcast_programming_years']; $podcast_programming_pay_hour=$_POST['podcast_programming_pay_hour']; echo" username is\n <br>$podcast_username <br><br> password is\n <br>$podcast_password <br><br> email address is\n <br>$podcast_email_address<br><br> profile name is\n <br>$podcast_programming_name<br><br> programming years\n <br>$podcast_programming_years<br><br> programming skils\n <br>"; if($_POST['podcast_programming_exspertise']){foreach($_POST['podcast_programming_exspertise'] as $podcast_programming_exspertise) { echo "$podcast_programming_exspertise <br><br>"; }} echo"podcast contubute\n <br>"; if($_POST['podcast_programming_contubute']){foreach($_POST['podcast_programming_contubute'] as $podcast_programming_contubute) { echo "$podcast_programming_contubute <br><br>"; }} echo"user for hire\n <br>"; if($_POST['podcast_programming_hire']){foreach($_POST['podcast_programming_hire'] as $podcast_programming_hire) { echo "$podcast_programming_hire <br><br>"; }} echo"can user contact you via built in messager\n <br>"; if($_POST['podcast_programming_built_in_email']){foreach($_POST['podcast_programming_built_in_email'] as $podcast_programming_built_in_email) { echo "$podcast_programming_built_in_email <br><br>"; }} echo"can user contact you via your vaild email address\n <br>"; if($_POST['podcast_programming_out_side_email']){foreach($_POST['podcast_programming_out_side_email'] as $podcast_programming_out_side_email) { echo "$podcast_programming_out_side_email <br><br>"; }} echo"show email on profile\n <br>"; if($_POST['podcast_programming_show_email']){foreach($_POST['podcast_programming_show_email'] as $podcast_programming_show_email) { echo "$podcast_programming_show_email <br><br>"; }} echo"teach users for free\n <br>"; if($_POST['podcast_programming_teach_free']){foreach($_POST['podcast_programming_teach_free'] as $podcast_programming_teach_free) { echo "$podcast_programming_teach_free<br><br>"; }} echo"teach users for a price\n <br>"; if($_POST['podcast_programming_teach_pay']){foreach($_POST['podcast_programming_teach_pay'] as $podcast_programming_teach_pay) { echo "$podcast_programming_teach_pay<br><br>"; }} echo" how much you charge per hour\n <br> £$podcast_programming_pay_hour <br>"; echo"do you agree with terms and condition\n <br>"; if($_POST['podcast_programming_agrement']){foreach($_POST['podcast_programming_agrement'] as $podcast_programming_agrement) { echo "$podcast_programming_agrement<br><br>"; }} } ?> <br> <form method="POST" Action="<?php $_SERVER['PHP_SELF']; ?>"> <table STYLE=" width: 900px; height: 50%; border: 0; margin-left: auto; margin-right: auto;"> <tr> <td valign="top" STYLE=" text-align: center; background-color: #3366ff; width: 25%; "> <br><br> <b>Username</b><br> 4 letters & 4 numbers. <br> <input type="text" name="podcast_username"> <br><br> <b>Password</b><br> 3 letters & 3 numbers. <br> <input type="password" name="podcast_password"> <br><br> <b>Email Address</b><br> Valid email address. <br> <input type="text" name="podcast_email_address"> <br><br> <b>Profile Username</b><br> Name shown to others. <br> <input type="text" name="podcast_programming_name"> <br><br> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>Programming Years</b> <br><br> Years Programming. <br> <select name="podcast_programming_years"> <option value=''>Select:</option> <?php for($py=1; $py<51; $py++){ echo"<option value='$py'>$py</option>"; } ?> </select> <br><br> <b>Programming Exspertise</b><br><br> Your programming skills. <br> <div align="left"> <input type="checkbox" name="podcast_programming_exspertise[]" value="Html "> Html. <br> <input type="checkbox" name="podcast_programming_exspertise[]" value="Xhtml "> Xhtml. <br> <input type="checkbox" name="podcast_programming_exspertise[]" value="Css "> Css. <br> <input type="checkbox" name="podcast_programming_exspertise[]" value="Javascript "> Javascript. <br> <input type="checkbox" name="podcast_programming_exspertise[]" value="Php "> Php. <br> <input type="checkbox" name="podcast_programming_exspertise[]" value="Mysql "> mysql. <br> <input type="checkbox" name="podcast_programming_exspertise[]" value="+ Others "> +Others. <br> </div> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>Web site contubute </b> <br><br> Going to contubute. <br> <br> <div align="left"> <input type="checkbox" name="podcast_programming_contubute[]" value="Recorded Pod Cast ">Recorded Pod Cast. <br> <input type="checkbox" name="podcast_programming_contubute[]" value="video Pod Cast ">video Pod Cast. <br> <input type="checkbox" name="podcast_programming_contubute[]" value="Tutoral Pod Cast ">Tutoral Pod Cast. <br> <input type="checkbox" name="podcast_programming_contubute[]" value="Shop">Shop. <br> <input type="checkbox" name="podcast_programming_contubute[]" value="Teach Programming ">Teach Programming. <br> </div> <br> <b>Are you for hire</b> <br> Let users no your for hire. <br><br> <input type="radio" name="podcast_programming_hire[]" value="yes" checked>Yes. <br> <input type="radio" name="podcast_programming_hire[]" value="no">No. <br> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>Can users contact you</b> <br><br> Built in messenger. <br><br> <input type="radio" name="podcast_programming_built_in_email[]" value="yes" checked>Yes. <br> <input type="radio" name="podcast_programming_built_in_email[]" value="no">No. <br> <br> <br><br> <b>Can users email you</b> <br><br> Live mail. <br><br> <input type="radio" name="podcast_programming_out_side_email[]" value="yes" checked>Yes. <br> <input type="radio" name="podcast_programming_out_side_email[]" value="no">No. </td> </tr></table> <br><br> <table STYLE=" width: 900px; height: 50%; border: 0; margin-left: auto; margin-right: auto;"> <tr> <td valign="top" STYLE=" text-align: center; background-color: #3366ff; width: 25%; "> <br><br> <b>Show your email</b><br><br> Show email on profile. <br><br> <input type="radio" name="podcast_programming_show_email[]" value="yes" checked>yes. <br> <input type="radio" name="podcast_programming_show_email[]" value="no">No. <br> <br> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>Teach for free</b><br><br> Teach others for free. <br><br> <input type="radio" name="podcast_programming_teach_free[]" value="yes" checked>yes. <br> <input type="radio" name="podcast_programming_teach_free[]" value="no">No. <br> <br> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>Pay to teach</b><br><br> Pay money to teach. <br><br> <input type="radio" name="podcast_programming_teach_pay[]" value="yes" checked>yes. <br> <input type="radio" name="podcast_programming_teach_pay[]" value="no">No. <br> <br> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>How much you charge</b><br><br> How much money per hour <br> to teach others. <br><br> £ <select name="podcast_programming_pay_hour"> <option value=''>Select:</option> <?php for($py=1; $py<51; $py++){ echo"<option value='$py'>$py</option>"; } ?> </select> <br><br> </td> </tr></table> <br><br> <table STYLE=" width: 900px; height: 50%; border: 0; margin-left: auto; margin-right: auto;"> <tr> <td valign="top" STYLE=" text-align: center; background-color: #3366ff; width: 75%; "> <textarea readonly STYLE="border: 0; text-align: left; background-color: #3366ff;" cols="80" rows="10" name="" ><?php include("php_podcast_terms.php")?></textarea> </td> <td valign="top" STYLE="text-align: center; background-color: #3366ff; width: 25%;"> <br><br> <b>Terms And Conditions</b><br><br> Do you agree to the terms and conditions. <br><br> <input type="radio" name="podcast_programming_agrement[]" value="yes" checked>yes. <br> <input type="radio" name="podcast_programming_agrement[]" value="no">No. <br><br> </td> </tr></table> <br><br> <table STYLE=" width: 900px; height: 50%; border: 0; margin-left: auto; margin-right: auto;"> <tr> <td valign="top" STYLE=" text-align: center; background-color: #3366ff; width: 100%; "> <br><br> <input type="submit" name="submit" value="Register Me!"> <br><br> </tr></table> </form> <?php include("php_podcast_footer.php");?> </body> </html Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769977 Share on other sites More sharing options...
sasa Posted February 24, 2009 Share Posted February 24, 2009 $values= " ('".implode("'), ('", $_POST['podcast_programming_exspertise']). "')"; $sql = "INSERT INTO table_name (field_name) VALUES $values"; Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769986 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 thank you sasa, can you kindly explain the implode bit, i no what implode does but, how it imploding "' <<< don't get. and get the format you have done. need to learn it and understand properly. it much better then foreach. cheers. current result ('Html '), ('Xhtml '), ('Css '), ('Javascript '), ('Php '), ('Mysql '), ('+ Others ') Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769988 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 got it waking up thank you yeeeee haaaaaaaaa <?php $values= implode( ',',$_POST['podcast_programming_exspertise']); echo "<h1>$values</h1>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-769996 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 From the first reply I guess you could use MySQLi and implode the array! I truely didn't think i'll have to give an example! Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-770000 Share on other sites More sharing options...
redarrow Posted February 24, 2009 Author Share Posted February 24, 2009 sorry i forgot the $_POST[''] within the implode . got very confused but cheers mate. i came up with a faster way to show the info works well. <?php if(isset($_POST['submit'])){ $pt[]=$_POST['podcast_username']; $pt[]=$_POST['podcast_password']; $pt[]=$_POST['podcast_email_address']; $pt[]=$_POST['podcast_programming_name']; $pt[]=$_POST['podcast_programming_years']; $pt[]=implode(',',$_POST['podcast_programming_exspertise']); $pt[]=implode(',',$_POST['podcast_programming_contubute']); $pt[]=implode(',',$_POST['podcast_programming_hire']); $pt[]=implode(',',$_POST['podcast_programming_built_in_email']); $pt[]=implode(',',$_POST['podcast_programming_out_side_email']); $pt[]=implode(',',$_POST['podcast_programming_show_email']); $pt[]=implode(',',$_POST['podcast_programming_teach_free']); $pt[]=implode(',',$_POST['podcast_programming_teach_pay']); $pt[]='£'.$_POST['podcast_programming_pay_hour']; $pt[]=implode(',',$_POST['podcast_programming_agrement']); $title=array("Username","Password","Email","Programmming Name","Years Programming", "Your Programming Exspertise","Contubute To Others","Are You For Hire","Can user contact you with the bult in messanger","Can users email you valid email address", "Show your email on your profile","Would you teach users for free","do you charge to tech your skill","If you charge how much per hour","Do you agree to the agreement"); for($i=0; $i<count($title); $pt++, $i++){ echo " <b>{$title[$i]}:</b>\n <br> {$pt[$i]}\n <br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146659-solved-post-help-with-foraech-array/#findComment-770012 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.