finto Posted December 6, 2007 Share Posted December 6, 2007 Hi All, I have a survey website with forms that I have linked to Mysql Database. It has over 300 questions true or false. I am about to write a php file with the "insert into" command to write to the database. My question is, Can i simplify the php command so that I do not have to type in over 300 database links. ie. INSERT INTO results (idno, name, course, q1, q2, q3......) and so on. Is there a command like q1 -> q300 Thanks for the help Fint Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 6, 2007 Share Posted December 6, 2007 <?php $sql = "INSERT INTO `results` (`idno`, `name`, `course`,"; for ( $x = 1 ; $x < 301 ; $x++ ) { $sql .= " `q$x`"; if ( $x < 300 ) { $sql .= ","; } else { $sql .= ")"; } } That will build you the field map. Using the same technique, you can dynamically create the VALUES. anyways, the answer is yes, you can do this... SQL querys can be dynamically built, and are especially easy to do so if your field names are numbered and your values are in a matching numeric array. You have think ahead to do something like this, but it's quite simple once setup. PhREEEk Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 6, 2007 Share Posted December 6, 2007 you can also try something like this <?php $sql = "INSERT INTO `results` (`idno`, `name`, `course`,`q".implode("`, `q",range(1,300))."`)"; print $sql; ?> Quote Link to comment Share on other sites More sharing options...
Ads Posted December 6, 2007 Share Posted December 6, 2007 you can also try something like this <?php $sql = "INSERT INTO `results` (`idno`, `name`, `course`,`q".implode("`, `q",range(1,300))."`)"; print $sql; ?> would You Query $sql Not print it? Quote Link to comment Share on other sites More sharing options...
finto Posted December 6, 2007 Author Share Posted December 6, 2007 <?php $sql = "INSERT INTO `results` (`idno`, `name`, `course`,"; for ( $x = 1 ; $x < 301 ; $x++ ) { $sql .= " `q$x`"; if ( $x < 300 ) { $sql .= ","; } else { $sql .= ")"; } } That will build you the field map. Using the same technique, you can dynamically create the VALUES. anyways, the answer is yes, you can do this... SQL querys can be dynamically built, and are especially easy to do so if your field names are numbered and your values are in a matching numeric array. You have think ahead to do something like this, but it's quite simple once setup. PhREEEk Great thanks. So if I want to put the values tag in after this where would I start it? After the last } is ? Thanks Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 6, 2007 Share Posted December 6, 2007 The .= keeps adding to the end of $sql, so you could go... <?php $sql = "INSERT INTO `results` (`idno`, `name`, `course`,"; for ( $x = 1 ; $x < 301 ; $x++ ) { $sql .= " `q$x`"; if ( $x < 300 ) { $sql .= ","; } else { $sql .= ")"; } } $sql .= " VALUES ('$idno', '$name', '$course',"; for ( $x = 1 ; $x < 301 ; $x++ ) { $sql .= " '$a[$x]'"; if ( $x < 300 ) { $sql .= ","; } else { $sql .= ")"; } } Where the array $a[] contains the data to be inserted. Here's an example: <?php // Set simulated first values $idno = '26'; $name = 'Yes or No'; $course = 'General Questions'; // Simulate a 300 question form submission of Yes or No answers for ( $x = 1 ; $x < 301 ; $x++ ) { $rand_choice = mt_rand(0, 1); if ( $rand_choice ) { $choice = 'Yes'; } else { $choice = 'No'; } $a[$x] = $choice; } // Dynamically build the field map $sql = "INSERT INTO `results` (`idno`, `name`, `course`,"; for ( $x = 1 ; $x < 301 ; $x++ ) { $sql .= " `q$x`"; if ( $x < 300 ) { $sql .= ","; } else { $sql .= ")"; } } // Dynamically build the values map $sql .= " VALUES ('$idno', '$name', '$course',"; for ( $x = 1 ; $x < 301 ; $x++ ) { $sql .= " '$a[$x]'"; if ( $x < 300 ) { $sql .= ","; } else { $sql .= ")"; } } // You would INSERT here, but we'll echo it echo $sql; /* Output: INSERT INTO `results` (`idno`, `name`, `course`, `q1`, `q2`, `q3`, `q4`, `q5`, `q6`, `q7`, `q8`, `q9`, `q10`, `q11`, `q12`, `q13`, `q14`, `q15`, `q16`, `q17`, `q18`, `q19`, `q20`, `q21`, `q22`, `q23`, `q24`, `q25`, `q26`, `q27`, `q28`, `q29`, `q30`, `q31`, `q32`, `q33`, `q34`, `q35`, `q36`, `q37`, `q38`, `q39`, `q40`, `q41`, `q42`, `q43`, `q44`, `q45`, `q46`, `q47`, `q48`, `q49`, `q50`, `q51`, `q52`, `q53`, `q54`, `q55`, `q56`, `q57`, `q58`, `q59`, `q60`, `q61`, `q62`, `q63`, `q64`, `q65`, `q66`, `q67`, `q68`, `q69`, `q70`, `q71`, `q72`, `q73`, `q74`, `q75`, `q76`, `q77`, `q78`, `q79`, `q80`, `q81`, `q82`, `q83`, `q84`, `q85`, `q86`, `q87`, `q88`, `q89`, `q90`, `q91`, `q92`, `q93`, `q94`, `q95`, `q96`, `q97`, `q98`, `q99`, `q100`, `q101`, `q102`, `q103`, `q104`, `q105`, `q106`, `q107`, `q108`, `q109`, `q110`, `q111`, `q112`, `q113`, `q114`, `q115`, `q116`, `q117`, `q118`, `q119`, `q120`, `q121`, `q122`, `q123`, `q124`, `q125`, `q126`, `q127`, `q128`, `q129`, `q130`, `q131`, `q132`, `q133`, `q134`, `q135`, `q136`, `q137`, `q138`, `q139`, `q140`, `q141`, `q142`, `q143`, `q144`, `q145`, `q146`, `q147`, `q148`, `q149`, `q150`, `q151`, `q152`, `q153`, `q154`, `q155`, `q156`, `q157`, `q158`, `q159`, `q160`, `q161`, `q162`, `q163`, `q164`, `q165`, `q166`, `q167`, `q168`, `q169`, `q170`, `q171`, `q172`, `q173`, `q174`, `q175`, `q176`, `q177`, `q178`, `q179`, `q180`, `q181`, `q182`, `q183`, `q184`, `q185`, `q186`, `q187`, `q188`, `q189`, `q190`, `q191`, `q192`, `q193`, `q194`, `q195`, `q196`, `q197`, `q198`, `q199`, `q200`, `q201`, `q202`, `q203`, `q204`, `q205`, `q206`, `q207`, `q208`, `q209`, `q210`, `q211`, `q212`, `q213`, `q214`, `q215`, `q216`, `q217`, `q218`, `q219`, `q220`, `q221`, `q222`, `q223`, `q224`, `q225`, `q226`, `q227`, `q228`, `q229`, `q230`, `q231`, `q232`, `q233`, `q234`, `q235`, `q236`, `q237`, `q238`, `q239`, `q240`, `q241`, `q242`, `q243`, `q244`, `q245`, `q246`, `q247`, `q248`, `q249`, `q250`, `q251`, `q252`, `q253`, `q254`, `q255`, `q256`, `q257`, `q258`, `q259`, `q260`, `q261`, `q262`, `q263`, `q264`, `q265`, `q266`, `q267`, `q268`, `q269`, `q270`, `q271`, `q272`, `q273`, `q274`, `q275`, `q276`, `q277`, `q278`, `q279`, `q280`, `q281`, `q282`, `q283`, `q284`, `q285`, `q286`, `q287`, `q288`, `q289`, `q290`, `q291`, `q292`, `q293`, `q294`, `q295`, `q296`, `q297`, `q298`, `q299`, `q300`) VALUES ('26', 'Yes or No', 'General Questions', 'No', 'Yes', 'No', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'No', 'No', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes') */ PhREEEk Quote Link to comment Share on other sites More sharing options...
finto Posted December 6, 2007 Author Share Posted December 6, 2007 You are the man Phrreeek. Thats some great coding and it worked perfect. Thanks alot Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 6, 2007 Share Posted December 6, 2007 You're quite welcome finto... glad it worked for what you need! PhREEEk 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.