johnrb87 Posted October 5, 2010 Share Posted October 5, 2010 Hi everyone I have the following PHP code $sql.= "INSERT INTO `data` (`info`, `write`, `date`) VALUES"; $sql.= "("; for ($c=0; $c < $num; $c++) { $sql.= '"'.str_replace('"', "", $data[$c]).'",'; } $sql.= "'".date('Y-m-d')."');"; If I print $sql; I get INSERT INTO `data` (`info`, `write`, `date`) VALUES("data99","n",'2010-10-05'); INSERT INTO `data` (`info`, `write`, `date`) VALUES("data101","y",'2010-10-05'); INSERT INTO `data` (`info`, `write`, `date`) VALUES("data876","n",'2010-10-05'); what would I need to do to my PHP code in order for it to process each QUERY either all at once or one at a time Whatever I try, it either inserts nothing, or just does the first INSERT Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/ Share on other sites More sharing options...
johnsmith153 Posted October 5, 2010 Share Posted October 5, 2010 Put all the lines you have shown inside the for loop and then execute the query inside the for loop at the end. Change .= to just = in the first line. Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119446 Share on other sites More sharing options...
johnrb87 Posted October 5, 2010 Author Share Posted October 5, 2010 Thanks, I tried for ($c=0; $c < $num; $c++) { $sql= "INSERT INTO `data` (`info`, `write`, `date`) VALUES"; $sql.= "("; $sql.= '"'.str_replace('"', "", $data[$c]).'",'; $sql.= "'".date('Y-m-d')."'); $query = mysql_query($sql); } but that doesn't seem to do anything Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119450 Share on other sites More sharing options...
johnrb87 Posted October 6, 2010 Author Share Posted October 6, 2010 any ideas? Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119457 Share on other sites More sharing options...
johnrb87 Posted October 6, 2010 Author Share Posted October 6, 2010 Can anyone help? Been trying different things for last 6 hours and no luck yet Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119508 Share on other sites More sharing options...
BlueSkyIS Posted October 6, 2010 Share Posted October 6, 2010 your code is invalid and should not compile, certainly not run properly. do you have error_reporting turned off? If so, turn it on at least partially to get compile errors. secondly, I suggest that you check for errors whenever you perform mysql_query, for instance: $sql = "INSERT INTO table_name SET some_col = 'some_val'"; mysql_query($sql) or die(mysql_error() . " IN $sql "); // Add mysql_error() to see if the query fails (or not) Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119607 Share on other sites More sharing options...
mikosiko Posted October 6, 2010 Share Posted October 6, 2010 What you are trying to produce as the final value for your $sql variable is this: $sql = INSERT INTO `data` (`info`, `write`, `date`) VALUES ('data99','n','2010-10-05') , ('data101','y','2010-10-05') , ('data876','n','2010-10-05')"; therefore using concatenation from the beginning in your $sql variable is not going to work in the first code that you showed. Now that you now what is the final goal just adjust your code to produce the sentence in the right way. Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119619 Share on other sites More sharing options...
chintansshah Posted October 7, 2010 Share Posted October 7, 2010 try below code, $sql = "INSERT INTO `data` (`info`, `write`, `date`) VALUES"; for ($c=0; $c < $num; $c++) { $sql.= "("; $sql.= '"'.str_replace('"', "", $data[$c]).'",'; $sql.= "'".date('Y-m-d')."');"; }echo $sql;mysql_query($sql); your code will generate the query which you can execute once only. Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119695 Share on other sites More sharing options...
anups Posted October 7, 2010 Share Posted October 7, 2010 $link = new mysqli("localhost", "root", "root", "phpform"); /* turn autocommit on */ $link->autocommit(TRUE); $query = "INSERT INTO `test` (`info`, `write`, `date`) VALUES('data99','n','2010-10-05');"; $query .= "INSERT INTO `test` (`info`, `write`, `date`) VALUES('data101','y','2010-10-05');"; $query .= "INSERT INTO `test` (`info`, `write`, `date`) VALUES('data876','n','2010-10-05')"; echo $query; /* execute multi query */ $link->multi_query($query); /* close connection */ $link->close(); Link to comment https://forums.phpfreaks.com/topic/215246-multiple-queries/#findComment-1119712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.