/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] mysql multi-line INSERT from array???

This is Sooooo CLOSE! And yet…

follow comments:

[code=php]<?php # Script test_canvas_list.php
if (isset($_POST[‘submit’])) { // Handle the form.
require_once (‘mysql_connect.php’); // Connect to the db.
mysql_select_db (‘crm’) OR DIE (“Could Not select database’crm’:” . MYSQL_ERROR());

$canvas_list_contacts =” select id from contacts where agency_name=’FSBO’ and first_name=’tj'”;
$result = @mysql_query ($canvas_list_contacts)or die (“Query failed”); // Run the query.

// narrow query paramaters for single result and convert result set into array
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row[id];
}
$targets = array();
foreach ($array as $value)
{
$targets[] = “(”,’4c1c90ac-e29d-b079-8c24-4550f759e92c’,’$value’,’Contacts’,’null’,’null’)”;
}
echo ‘<select name=”endDate”>’;
foreach ($targets as $key => $value) {
echo “<option value=”$key”>
$value</option>n”; // echos infinite $targets array string values in dropdown as expected.
}
echo ‘</select><br><br>’;

$populate_canvas_list = “INSERT INTO prospect_lists_prospects (id,prospect_list_id,related_id,related_type,date_modified,deleted)
VALUES (”, ‘4c1c90ac-e29d-b079-8c24-4550f759e92c’,”,’Contacts’,”,”),
(‘1’, ‘4c1c90ac-e29d-b079-8c24-4550f759e92c’,”,’Contacts’,”,”),
(‘2’, ‘4c1c90ac-e29d-b079-8c24-4550f759e92c’,”,’Contacts’,”,”)”; //Everything works to this point – BUT…
//(‘2’, ‘4c1c90ac-e29d-b079-8c24-4550f759e92c’,’$value’,’Contacts’,”,”)”; //Query fails using ‘$value’
//VALUES .implode(‘, ‘, $targets)”; // Closing double_quote causes error, unexpected ‘”‘
VALUES .implode(‘, ‘, $targets); // Remove closing double_quote returns: ‘Query failed:’ ‘”‘
mysql_query($populate_canvas_list) or die(‘Query failed: $populate_canvas_list – . mysql_error()’);
}
exit(); // Quit the script.
mysql_close(); // Close the database connection.
?>[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisNov 17.2006 — The SQL [B]VALUES[/B] clause requires parentheses around the list of values. What is this?

//VALUES .implode(', ', $targets)"; // Closing double_quote causes error, unexpected '"'

VALUES .implode(', ', $targets); // Remove closing double_quote returns: 'Query failed:' '"'
Copy linkTweet thisAlerts:
@so_is_thisNov 17.2006 — Maybe you want this:

...VALUES(" .implode(', ', $targets) .");";

String values in that list will also require single quotes around them.
Copy linkTweet thisAlerts:
@tjmcdauthorNov 17.2006 — Maybe you want this:

...VALUES(" .implode(', ', $targets) .");";

String values in that list will also require single quotes around them.[/QUOTE]
the following is echoed from $value, so if I understand you correctly, I'm covered on the single quotes?[CODE]('','4c1c90ac-e29d-b079-8c24-4550f759e92c','e7f459e6-2d8c-6393-d57e-454e129f7114','Contacts','null','null')[/CODE]as for ... VALUES(" .implode(', ', $targets) .");"; MUCH to my surprise inserting this DOES NOT get t-string or unexpected'"' errors which I have been figting right along. Sadly however the inser query still fails ?
Copy linkTweet thisAlerts:
@so_is_thisNov 17.2006 — If each $value in $targets echoes that string, then you will not be able to use implode() for the entire array. The reason is that string shows the parentheses are already present. That would seem to be too many parentheses for the SQL statement.
Copy linkTweet thisAlerts:
@tjmcdauthorNov 17.2006 — If each $value in $targets echoes that string, then you will not be able to use implode() for the entire array. The reason is that string shows the parentheses are already present. That would seem to be too many parentheses for the SQL statement.[/QUOTE] Right on the money! I discovered ths by simply echoing the query, and was able to back my way into the right combination of parentheses, single and double qoutes. That said, the following now works, BUT... I'm struggling to assign a unique id value to $key in the second array. Unfortunately my tables 'id' field is not auto_increment. Stumped. ?[code=php] <?php # Script test_canvas_list.php

//create unique ID
$c = uniqid (rand (),true);
echo $c;

if (isset($_POST['submit'])) { // Handle the form.
require_once ('mysql_connect.php'); // Connect to the db.
mysql_select_db ('crm') OR DIE ("Could Not select database'crm':" . MYSQL_ERROR());

$canvas_list_contacts =" select id from contacts where agency_name='FSBO'";
$result = @mysql_query ($canvas_list_contacts)or die ("Query failed"); // Run the query.

// narrow query paramaters for single result and convert result set into array
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row[id];
}
$targets = array();
foreach ($array as $key=>$value)
{
$targets[] = "('$key','4c1c90ac-e29d-b079-8c24-4550f759e92c','$value','Contacts','null','null'";
}

foreach ($targets as $key => $value) {
echo $populate_canvas_list = "INSERT INTO prospect_lists_prospects (id,prospect_list_id,related_id,related_type,date_modified,deleted)
VALUES $value);";
mysql_query($populate_canvas_list) or die('Query failed: $populate_canvas_list - . mysql_error()');

}
}

exit(); // Quit the script.
mysql_close(); // Close the database connection.
?>[/code]
Copy linkTweet thisAlerts:
@NogDogNov 17.2006 — [EDIT: Looks like I was still typing when you made your last reply, so this may be unnecessary now.]

Let's simplify things a bit by only specifying the fields we're setting. (If you want/need to explicitly set fields to a null value, do not quote the word NULL in the value list.)
[code=php]
foreach ($array as $value)
{
$targets[] = "('4c1c90ac-e29d-b079-8c24-4550f759e92c','$value','Contacts')";
}
[/code]

...and...
[code=php]
$populate_canvas_list = "INSERT INTO prospect_lists_prospects (prospect_list_id,related_id,related_type)
VALUES " . implode(', ', $targets);
[/code]

If that doesn't work (or something else you try doesn't work), can you show us the exact query string and MySQL error message that the die() returns, so that we can see what it's actually trying to do?
Copy linkTweet thisAlerts:
@tjmcdauthorNov 17.2006 — Thanks Again NogDog!

Finally I got this sucker![code=php]<?php # Script test_canvas_list.php

//create unique ID

if (isset($_POST['submit'])) { // Handle the form.
require_once ('mysql_connect.php'); // Connect to the db.
mysql_select_db ('crm') OR DIE ("Could Not select database'crm':" . MYSQL_ERROR());

$canvas_list_contacts =" select id from contacts where agency_name='FSBO'";
$result = @mysql_query ($canvas_list_contacts)or die ("Query failed"); // Run the query.

// narrow query paramaters for single result and convert result set into array
while ($row = mysql_fetch_assoc($result)) {
$key = uniqid(rand(), true);
$targets[] = "('$key','4c1c90ac-e29d-b079-8c24-4550f759e92c','$row[id]','Contacts','null','null'";
}

foreach ($targets as $key => $value) {
$populate_canvas_list = "INSERT INTO prospect_lists_prospects (id,prospect_list_id,related_id,related_type,date_modified,deleted)
VALUES $value);";
mysql_query($populate_canvas_list) or die('Query failed: $populate_canvas_list - . mysql_error()');

}
}

exit(); // Quit the script.
mysql_close(); // Close the database connection.
?>[/code]
×

Success!

Help @tjmcd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...