PrasannaNehru Posted August 5, 2008 Share Posted August 5, 2008 file----insert.php doubt---how to insert randomly records may string or int <?php class insert { function display() { $database=$_POST['data1']; $table=$_POST['data2']; $radio1=$_POST['radio1']; $radio2=$_POST['radio2']; $textarea=$_POST['textarea']; $records=$_POST['t3']; // database connectivity include("DataBase.php"); $sql = "Select * from $table"; $qry = mysql_query($sql); $count = ""; $num = mysql_num_fields($qry); // take field name for($i=0;$i<$num;$i++) { //echo $f[] = $_POST['field'.$i]; $count.= $f[] = $_POST['field'.$i].','; } $del=substr($count,0,-1); //echo "output=".$count; /*for($i=0;$i<$num;$i++) { //echo $f[] = $_POST['field'.$i]; $fieldvalue.= $fv[] = $_POST['field'.$i].','; }*/ //take field type for($i=0;$i<$num;$i++) { //echo $f1[] = $_POST['field'.$i]; $count1.= $f1[] = $_POST['fieldtype'.$i].','; } $del1=substr($count1,0,-1); //take random values as text and integer $valrand1=""; $valrand2=""; for($i=0;$i<$num;$i++) { if(substr($f1[$i],0,7)=='varchar') { $valrand1.=" 'pras' ".','; } else { $valrand1.=rand(1,.','; } echo $valrand1."<br>"; } $a=substr($valrand1,0,-1); // echo $a; //insert the records in the data base for($i=1;$i<=$records;$i++) { $sql="INSERT INTO $table ($del) VALUES ($a)" ; $res = mysql_query($sql)or die(mysql_error()); //echo $res; } $res = true?print("Inserted"):print("failed"); } } $obj= new insert(); $obj->display(); ?> explanation ----------- $a=5,'pras'5 //it dynamically change as like number of field may int or varchar $del=id,'name',salary //dynamically change like select the table $records=10 //10 records to inserted but randomly not same as previous recoreds.....plz..... Link to comment https://forums.phpfreaks.com/topic/118195-doubts/ Share on other sites More sharing options...
chronister Posted August 5, 2008 Share Posted August 5, 2008 post -- yours doubt -- what the hell are you talking about? Your question is as clear as mud. please for the love of vishnu clarify your issue so that we may assist. All joking aside, I have no idea what your asking. Maybe I am just retarded tonight? *Please don't take offense to the vishnu comment. Simpsons reference * Link to comment https://forums.phpfreaks.com/topic/118195-doubts/#findComment-608354 Share on other sites More sharing options...
rajesh Posted August 5, 2008 Share Posted August 5, 2008 You can use this code and try it . But i don't know how many bugs but this is the logic. using inner for loop is the result for inserting different records ////////////////////////////// <?php class insert { function display() { $database=$_POST['data1']; $table=$_POST['data2']; $radio1=$_POST['radio1']; $radio2=$_POST['radio2']; $textarea=$_POST['textarea']; $records=$_POST['t3']; // database connectivity include("DataBase.php"); $sql = "Select * from $table"; $qry = mysql_query($sql); $count = ""; $num = mysql_num_fields($qry); // take field name for($i=0;$i<$num;$i++) { $fieldName[$i] = $_POST['field'.$i]; } $fieldName=implode(",",$fieldName); //take field type for($i=0;$i<$num;$i++) { //echo $f1[] = $_POST['field'.$i]; $fieldType[$i] = $_POST['fieldtype'.$i]; } //insert the records in the data base for($i=1;$i<=$records;$i++) { $sql="INSERT INTO $table ($del) VALUES (" ; $values = ""; for($c=0;$c<$num;$c++){ if(substr($fieldType[$c],0,7)=='varchar'){ $values. = "'".$this->random_letters(5)."',"; } else{ $values. = rand(500, 15000).","; } } $values = substr($values,0,-1); echo $sql.= "$values)"; $res = mysql_query($sql)or die(mysql_error()); //echo $res; } $res = true?print("Inserted"):print("failed"); } function random_letters ($numofletters) { if (!isset($numofletters)) $numofletters = 10; // if $numofletters is not specified sets to 10 letters $literki = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'W'); $ilosc_literek = count($literki); for ($licz = 0; $licz < $numofletters; $licz++) { $rand = rand(0, $ilosc_literek-1); $vercode = $vercode.$literki[$rand]; } return $vercode; } } $obj= new insert(); $obj->display(); ?> Link to comment https://forums.phpfreaks.com/topic/118195-doubts/#findComment-608382 Share on other sites More sharing options...
PrasannaNehru Posted August 12, 2008 Author Share Posted August 12, 2008 // in my pgm there is an error in my data base programm plz clarify the doubt.... // column count doesnt match the value at row 1 ---this is my doubt plz help.... my project is run on my system but it may not run on other system..... Link to comment https://forums.phpfreaks.com/topic/118195-doubts/#findComment-614260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.