Jump to content

Recommended Posts

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

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

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

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

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

$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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.