PHP & MySQL - Array to string conversion error problem
I keep getting an the following error of Array to string conversion error
on this line listed below. How can I fix this problem?
$skill = explode('', $_POST['skill']);
Here is the PHP & MySQL code.
$skill = explode('', $_POST['skill']);
$experience = explode('', $_POST['experience']);
$years = explode('', $_POST['years']);
for ($s = 0; $s < count($skill); $s++){
for ($x = 0; $x < count($experience); $x++){
for ($g = 0; $g < count($years); $g++){
if (mysqli_num_rows($dbc) == 0) {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO learned_skills (user_id, skill, experience, years, date_created)
VALUES ('" . $user_id . "', '" . $skill[$s] . "', '" . $experience[$x] . "', '" . $years[$g] . "', NOW())");
}
if ($dbc == TRUE) {
$dbc = mysqli_query($mysqli,"UPDATE learned_skills
SET skill = '$skill', experience = '$experience', years = '$years', date_created = NOW()
WHERE user_id = '$user_id'");
echo '<p class="changes-saved">Your changes have been saved!</p>';
}
if (!$dbc) {
print mysqli_error($mysqli);
return;
}
}
}
}
Here is the HTML code.
<li><label for="skill">Skills: </label><input type="text" name="skill[]" id="skill[]" size="25" maxlength="255" />
<label for="experience">Experience: </label>
<?php
echo '<select id="e开发者_JAVA技巧xperience[]" name="experience[]">' . "\n";
foreach($experience_options as $option) {
if ($option == $experience) {
echo '<option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected">' . stripslashes(htmlentities(strip_tags($option))) . '</option>' . "\n";
} else {
echo '<option value="'. stripslashes(htmlentities(strip_tags($option))) . '">' . stripslashes(htmlentities(strip_tags($option))) . '</option>'."\n";
}
}
echo '</select>';
?>
<label for="years">Years: </label>
<?php
echo '<select id="years[]" name="years[]">' . "\n";
foreach($year_options as $option) {
if ($option == $years) {
echo '<option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected">' . stripslashes(htmlentities(strip_tags($option))) . '</option>' . "\n";
} else {
echo '<option value="'. stripslashes(htmlentities(strip_tags($option))) . '">' . stripslashes(htmlentities(strip_tags($option))) . '</option>'."\n";
}
}
echo '</select></li>';
As noted by the docs, explode
with an empty delimiter always returns FALSE. What are you trying to do. What does $_POST['skill']
look like?
I'd say that in
$skill = explode('', $_POST['skill']);
$_POST['skill']
is an array. explode()
's 2nd argument expects a string, or something it can convert to a string I think. It will probably also give you headaches if you attempt to split with an empty string variable.
I also know this because your name of the input field is skill[]
.
PHP will turn any name with a []
at the end into an array. You can even specify the key you like between the brackets.
So you can probably skip that line, as the variable is already an array. If you don't want it to be an array, drop the []
off the name attribute.
In this text form element:
<label for="skill">Skills: </label>
<input type="text" name="skill[]" id="skill[]" size="25" maxlength="255" />
you've asked PHP to store the text value provided by the user in an array called "skill". To store the field as a simple string (which you can explode later), remove the square brackets:
<label for="skill">Skills: </label>
<input type="text" name="skill" id="skill" size="25" maxlength="255" />
This should fix the array error. On another note, as pointed out by others, the empty parameter to explode in this line is probably not correct:
$skill = explode('', $_POST['skill']);
Should the skills provided by the user be comma separated? If so, perhaps this is what you wanted?
$skill = explode(',', $_POST['skill']);
Hope this helps!
精彩评论