Language: PHP / MySQL
I have a form on a page, with hidden inputs. I forward the data from these inputs on to another page, then I insert them into my database.
The inputs inside the form are:
These are set-up to be arrays because there are times that a user will have more than one file attached.
<input type="hidden" id="copied_filename" name="copied_filename[]" value="<?php echo $img->filename; ?>" />
<input type="hidden" id="copied_url" name="copied_url[]" value="<?php echo $img->url; ?>" />
<input type="hidden" id="copied_userid" name="copied_userid[]" value="<?php echo $current_user->id; ?>" />
Now on the 2nd page where the data is received, this is how I handle it:
if (empty($_POST["copied_filename"])) {
WHAT IT DOES WHEN THERE ARE NO ATTACHED FILES
}
else {
$copied_filename = $_POST["copied_filename"];
$copied_url = $_POST["copied_url"];
$new_sessionid = $_POST['session_id'];
foreach ($_POST["copied_filename"] as $copied_file) {
$sql = "INSERT INTO ".$wpdb->prefix."estimate_images (code, url, filename, session_id, user_id) VALUES ('".$code."', '".$copied_url."', '".$copied_file."','".$new_sessionid."', '".$current_user->id."')";
$wpdb->query($sql);
}
It works fine for the filename, but the URL being inserted in my database is the word " Array "...
I am sure it is the foreach format, but I am stumped & don't know how to fix it.
Thank you so much for your time, any assistance would be greatly appreciated!
if (empty($_POST["copied_filename"])) {
//WHAT IT DOES WHEN THERE ARE NO ATTACHED FILES
}
else {
$new_sessionid = $_POST['session_id'];
foreach ($_POST["copied_filename"] as $k => $copied_file) {
$copied_url = $_POST["copied_url"][$k];
$sql = "INSERT INTO ".$wpdb->prefix."estimate_images (code, url, filename, session_id, user_id) VALUES ('".$code."', '".$copied_url."', '".$copied_file."','".$new_sessionid."', '".$current_user->id."')";
$wpdb->query($sql);
}
}
Because they are arrays. echo
cannot correctly display an array. Use print_r
or var_dump
to see the correct structure.
If you want to access the first element, you need to echo $_POST["copied_filename"][0]
.
If you want to access all of the elements, iterate it with a foreach
loop.
Your foreach is working on copied_filename only. For your situation try in this way:
foreach ($_POST["copied_filename"] as $key => $val)
{
$sql = "INSERT INTO ".$wpdb->prefix."estimate_images (code, url, filename,
session_id, user_id) VALUES ('".$code."', '".$copied_url[$key]."',
'".$val."','".$new_sessionid."', '".$current_user->id."')";
$wpdb->query($sql);
}
$copied_url is an array. To get the URL you have to do something like eg $copied_url[0]. In your case maybe simething like this could work:
for($i=0; $i< count($_POST["copied_filename"]); $i++) {
$sql = "INSERT INTO ".$wpdb->prefix."estimate_images (code, url, filename, session_id, user_id) VALUES ('".$code."', '".$copied_url[$i]."', '".$copied_filename[$i]."','".$new_sessionid."', '".$current_user->id."')";
$wpdb->query($sql);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.