PHP MySQL query for insert with Concatenation assignment variable
up vote
0
down vote
favorite
I was facing the problem with php insert multiple image and the only post id to the database.
In database I have image table and post table, when I upload multiple image the image will store in image table with the current post id.
The problem is the insert syntax is wrong, because the
$insertValuesSQL
is from for each with using .=
concatenation assignment to upload the image.
The thing is for I was to add addtional post id easy for me to get the image from which post id, so I have to insert with two thing which is $insertValuesSQL
and the $last_id
from the post id.
Can someone help me to correct the syntax and able to upload the image with the post id? Aprreciate and thank you.
Error at:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
PHP full code:
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['submit-image']['name']))){
foreach($_FILES['submit-image']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['submit-image']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$fileName."'),";
}else{
$errorUpload .= $_FILES['submit-image']['name'][$key].', ';
}
}else{
$errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
}
}
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}else{
$statusMsg = 'Please select a file to upload.';
}
Adding one more
if I using the code below :
$insert = $conn->query("INSERT INTO test (file_name) VALUES $insertValuesSQL");
It successful to upload multiple image but there no post id for the image
Reference from :
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/
php mysql sql
add a comment |
up vote
0
down vote
favorite
I was facing the problem with php insert multiple image and the only post id to the database.
In database I have image table and post table, when I upload multiple image the image will store in image table with the current post id.
The problem is the insert syntax is wrong, because the
$insertValuesSQL
is from for each with using .=
concatenation assignment to upload the image.
The thing is for I was to add addtional post id easy for me to get the image from which post id, so I have to insert with two thing which is $insertValuesSQL
and the $last_id
from the post id.
Can someone help me to correct the syntax and able to upload the image with the post id? Aprreciate and thank you.
Error at:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
PHP full code:
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['submit-image']['name']))){
foreach($_FILES['submit-image']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['submit-image']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$fileName."'),";
}else{
$errorUpload .= $_FILES['submit-image']['name'][$key].', ';
}
}else{
$errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
}
}
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}else{
$statusMsg = 'Please select a file to upload.';
}
Adding one more
if I using the code below :
$insert = $conn->query("INSERT INTO test (file_name) VALUES $insertValuesSQL");
It successful to upload multiple image but there no post id for the image
Reference from :
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/
php mysql sql
@user3783243 Yes, that one is insert post query, so after succesful insert post, i get the last id for upload the image with the id
– Tommy Chong
Nov 17 at 15:19
Output the actual query, it looks like its going to be pretty mangled. I think it'd come out asINSERT INTO test (file_name, asd) VALUES ('filename'),,last_id_value
which isn't going to work. Use themysqli
error reporting function so you get the actual error. This also is open to SQL injections. I'd recommend doing this with parameterized queries.
– user3783243
Nov 17 at 15:22
@user3783243 sorry my bad, I was typed wrongtest (file_name, asd)
– Tommy Chong
Nov 17 at 15:28
This is vulnerable to SQL injection. Don’t ignore the advice to use parameterized queries.
– Ry-♦
Nov 17 at 20:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was facing the problem with php insert multiple image and the only post id to the database.
In database I have image table and post table, when I upload multiple image the image will store in image table with the current post id.
The problem is the insert syntax is wrong, because the
$insertValuesSQL
is from for each with using .=
concatenation assignment to upload the image.
The thing is for I was to add addtional post id easy for me to get the image from which post id, so I have to insert with two thing which is $insertValuesSQL
and the $last_id
from the post id.
Can someone help me to correct the syntax and able to upload the image with the post id? Aprreciate and thank you.
Error at:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
PHP full code:
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['submit-image']['name']))){
foreach($_FILES['submit-image']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['submit-image']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$fileName."'),";
}else{
$errorUpload .= $_FILES['submit-image']['name'][$key].', ';
}
}else{
$errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
}
}
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}else{
$statusMsg = 'Please select a file to upload.';
}
Adding one more
if I using the code below :
$insert = $conn->query("INSERT INTO test (file_name) VALUES $insertValuesSQL");
It successful to upload multiple image but there no post id for the image
Reference from :
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/
php mysql sql
I was facing the problem with php insert multiple image and the only post id to the database.
In database I have image table and post table, when I upload multiple image the image will store in image table with the current post id.
The problem is the insert syntax is wrong, because the
$insertValuesSQL
is from for each with using .=
concatenation assignment to upload the image.
The thing is for I was to add addtional post id easy for me to get the image from which post id, so I have to insert with two thing which is $insertValuesSQL
and the $last_id
from the post id.
Can someone help me to correct the syntax and able to upload the image with the post id? Aprreciate and thank you.
Error at:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
PHP full code:
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['submit-image']['name']))){
foreach($_FILES['submit-image']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['submit-image']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$fileName."'),";
}else{
$errorUpload .= $_FILES['submit-image']['name'][$key].', ';
}
}else{
$errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
}
}
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}else{
$statusMsg = 'Please select a file to upload.';
}
Adding one more
if I using the code below :
$insert = $conn->query("INSERT INTO test (file_name) VALUES $insertValuesSQL");
It successful to upload multiple image but there no post id for the image
Reference from :
https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/
php mysql sql
php mysql sql
edited Nov 17 at 15:26
asked Nov 17 at 15:14
Tommy Chong
1469
1469
@user3783243 Yes, that one is insert post query, so after succesful insert post, i get the last id for upload the image with the id
– Tommy Chong
Nov 17 at 15:19
Output the actual query, it looks like its going to be pretty mangled. I think it'd come out asINSERT INTO test (file_name, asd) VALUES ('filename'),,last_id_value
which isn't going to work. Use themysqli
error reporting function so you get the actual error. This also is open to SQL injections. I'd recommend doing this with parameterized queries.
– user3783243
Nov 17 at 15:22
@user3783243 sorry my bad, I was typed wrongtest (file_name, asd)
– Tommy Chong
Nov 17 at 15:28
This is vulnerable to SQL injection. Don’t ignore the advice to use parameterized queries.
– Ry-♦
Nov 17 at 20:16
add a comment |
@user3783243 Yes, that one is insert post query, so after succesful insert post, i get the last id for upload the image with the id
– Tommy Chong
Nov 17 at 15:19
Output the actual query, it looks like its going to be pretty mangled. I think it'd come out asINSERT INTO test (file_name, asd) VALUES ('filename'),,last_id_value
which isn't going to work. Use themysqli
error reporting function so you get the actual error. This also is open to SQL injections. I'd recommend doing this with parameterized queries.
– user3783243
Nov 17 at 15:22
@user3783243 sorry my bad, I was typed wrongtest (file_name, asd)
– Tommy Chong
Nov 17 at 15:28
This is vulnerable to SQL injection. Don’t ignore the advice to use parameterized queries.
– Ry-♦
Nov 17 at 20:16
@user3783243 Yes, that one is insert post query, so after succesful insert post, i get the last id for upload the image with the id
– Tommy Chong
Nov 17 at 15:19
@user3783243 Yes, that one is insert post query, so after succesful insert post, i get the last id for upload the image with the id
– Tommy Chong
Nov 17 at 15:19
Output the actual query, it looks like its going to be pretty mangled. I think it'd come out as
INSERT INTO test (file_name, asd) VALUES ('filename'),,last_id_value
which isn't going to work. Use the mysqli
error reporting function so you get the actual error. This also is open to SQL injections. I'd recommend doing this with parameterized queries.– user3783243
Nov 17 at 15:22
Output the actual query, it looks like its going to be pretty mangled. I think it'd come out as
INSERT INTO test (file_name, asd) VALUES ('filename'),,last_id_value
which isn't going to work. Use the mysqli
error reporting function so you get the actual error. This also is open to SQL injections. I'd recommend doing this with parameterized queries.– user3783243
Nov 17 at 15:22
@user3783243 sorry my bad, I was typed wrong
test (file_name, asd)
– Tommy Chong
Nov 17 at 15:28
@user3783243 sorry my bad, I was typed wrong
test (file_name, asd)
– Tommy Chong
Nov 17 at 15:28
This is vulnerable to SQL injection. Don’t ignore the advice to use parameterized queries.
– Ry-♦
Nov 17 at 20:16
This is vulnerable to SQL injection. Don’t ignore the advice to use parameterized queries.
– Ry-♦
Nov 17 at 20:16
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You need to put the $last_id
into every list of values being inserted, not as a separate parameter. But you can't do this because you're creating $insertValuesSQL
before you set $last_id
.
You can move the loop that creates $insertValuesSQL
to after you set $last_id
, but another way is to use MySQL's built-in function LAST_INSERT_ID()
:
$insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";
Then you can take $last_id
out of the later INSERT
query:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");
LAST_INSERT_ID()
can create race condition though, can't it?
– user3783243
Nov 17 at 15:46
1
No, it's exactly the same asmysqli_insert_id()
. They both get the last ID inserted by the current connection.
– Barmar
Nov 17 at 15:49
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to put the $last_id
into every list of values being inserted, not as a separate parameter. But you can't do this because you're creating $insertValuesSQL
before you set $last_id
.
You can move the loop that creates $insertValuesSQL
to after you set $last_id
, but another way is to use MySQL's built-in function LAST_INSERT_ID()
:
$insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";
Then you can take $last_id
out of the later INSERT
query:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");
LAST_INSERT_ID()
can create race condition though, can't it?
– user3783243
Nov 17 at 15:46
1
No, it's exactly the same asmysqli_insert_id()
. They both get the last ID inserted by the current connection.
– Barmar
Nov 17 at 15:49
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
add a comment |
up vote
2
down vote
accepted
You need to put the $last_id
into every list of values being inserted, not as a separate parameter. But you can't do this because you're creating $insertValuesSQL
before you set $last_id
.
You can move the loop that creates $insertValuesSQL
to after you set $last_id
, but another way is to use MySQL's built-in function LAST_INSERT_ID()
:
$insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";
Then you can take $last_id
out of the later INSERT
query:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");
LAST_INSERT_ID()
can create race condition though, can't it?
– user3783243
Nov 17 at 15:46
1
No, it's exactly the same asmysqli_insert_id()
. They both get the last ID inserted by the current connection.
– Barmar
Nov 17 at 15:49
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to put the $last_id
into every list of values being inserted, not as a separate parameter. But you can't do this because you're creating $insertValuesSQL
before you set $last_id
.
You can move the loop that creates $insertValuesSQL
to after you set $last_id
, but another way is to use MySQL's built-in function LAST_INSERT_ID()
:
$insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";
Then you can take $last_id
out of the later INSERT
query:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");
You need to put the $last_id
into every list of values being inserted, not as a separate parameter. But you can't do this because you're creating $insertValuesSQL
before you set $last_id
.
You can move the loop that creates $insertValuesSQL
to after you set $last_id
, but another way is to use MySQL's built-in function LAST_INSERT_ID()
:
$insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";
Then you can take $last_id
out of the later INSERT
query:
$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");
answered Nov 17 at 15:44
Barmar
413k34238339
413k34238339
LAST_INSERT_ID()
can create race condition though, can't it?
– user3783243
Nov 17 at 15:46
1
No, it's exactly the same asmysqli_insert_id()
. They both get the last ID inserted by the current connection.
– Barmar
Nov 17 at 15:49
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
add a comment |
LAST_INSERT_ID()
can create race condition though, can't it?
– user3783243
Nov 17 at 15:46
1
No, it's exactly the same asmysqli_insert_id()
. They both get the last ID inserted by the current connection.
– Barmar
Nov 17 at 15:49
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
LAST_INSERT_ID()
can create race condition though, can't it?– user3783243
Nov 17 at 15:46
LAST_INSERT_ID()
can create race condition though, can't it?– user3783243
Nov 17 at 15:46
1
1
No, it's exactly the same as
mysqli_insert_id()
. They both get the last ID inserted by the current connection.– Barmar
Nov 17 at 15:49
No, it's exactly the same as
mysqli_insert_id()
. They both get the last ID inserted by the current connection.– Barmar
Nov 17 at 15:49
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
Thanks I solve with you solution, appreciate :D
– Tommy Chong
Nov 17 at 15:54
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352527%2fphp-mysql-query-for-insert-with-concatenation-assignment-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@user3783243 Yes, that one is insert post query, so after succesful insert post, i get the last id for upload the image with the id
– Tommy Chong
Nov 17 at 15:19
Output the actual query, it looks like its going to be pretty mangled. I think it'd come out as
INSERT INTO test (file_name, asd) VALUES ('filename'),,last_id_value
which isn't going to work. Use themysqli
error reporting function so you get the actual error. This also is open to SQL injections. I'd recommend doing this with parameterized queries.– user3783243
Nov 17 at 15:22
@user3783243 sorry my bad, I was typed wrong
test (file_name, asd)
– Tommy Chong
Nov 17 at 15:28
This is vulnerable to SQL injection. Don’t ignore the advice to use parameterized queries.
– Ry-♦
Nov 17 at 20:16