php - INSERT INTO mysql only if input text is not empty - Codeigniter
Get the solution ↓↓↓Solution:
I would use following syntax in your situation:
INSERT INTO todo
SET
one='val1',
two='val2',
three='val3'
You can set field=value pairs in an array easier and add to the statement by imploding.
Added sample below:
if( !empty($one_input)){
$query_data[]= "one='$one_input'";
}
if( !empty($two_input)){
$query_data[]= "two='$two_input'";
}
if( !empty($three_input)){
$query_data[]= "three='$three_input'";
}
$query = "INSERT INTO todo SET " . implode(",", $query_data);
Answer
Solution:
If this is your query:
INSERT INTO todo(one, two, three) VALUES ('val1, val2, val3);'
Then you have a problem with single quotes. You probably want:
INSERT INTO todo(one, two, three) VALUES ('val1', 'val2', 'val3');
or
INSERT INTO todo(one, two, three) VALUES (val1, val2, val3);
Answer
Solution:
Can you please elaborate more what you want for your output? And input?
if(empty($one_input) || empty($two_input) || empty($three_input)){
/* Do Nothing */
}
else {
query str="INSERT INTO todo('','','') VALUES ('','','')";
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object of class stdclass could not be converted to string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.