I'm sending muliple inputs with form, How can I get the id of the actuall input, so i can update mysql content?
my code:
if (isset($_POST['save']))
{
foreach ($_POST as $key => $value)
{
${$key} = $value;
mysql_query("UPDATE table SET img='$image_[id]', title='$title_[id]' where id='$id'");
}
}
while ($row = mysql_fetch_array($result))
{
echo "<b>Title: </b><input type='text' name='title_" . $row['id'] . "' value='" . $row['title'] . "'><br />";
echo "<b>Image: </b><input type='text' name='image_" . $row['id'] . "' value='";
}
echo '<input type="submit" name="save" value="save"><br /><hr>';
echo '</form>';
Yours naming is wrong. Use this way.
echo "... name='title[" . $row['id'] . "]'...";
And after it your $_POST['title'] will contain array of titles.
And read manual How do I create arrays in a HTML ?
But if you are still what to go that way. You can use something like this.
foreach ($_POST as $key => $value)
{
if (preg_match('^image_(\d*)$', $key, $matches))
{
$id = $matches[1];
if (isset($_POST['title_' . $id]))
{
mysql_query("UPDATE table SET title='" . $_POST['title_' . $id] . "' where id='$id'");
}
}
}
But sign here that you understand all vulnerabilities of this code.
Related
I've a very simple database, a list of 8 languages and the values they can have is Y or N.
What I need is to output a checkbox for each languages and, if the value is Y, the checkbox must be checked, otherwise it must be empty.
This is the code I'm using for each single language, but I would like to know if there's a better way to obtain the same result.
if ($language=='N'){
echo "<input type='checkbox' name='" . $rowlang["english"] . "' value='" . $rowlang["english"] . "'> English<br>";
} else {
echo "<input type='checkbox' name='" . $rowlang["english"] . "' value='" . $rowlang["english"] . " checked'> English<br>";}
//What I've tried to do is to build an array of the languages and use a foreach
$languages = array($rowlang["czech"],$rowlang["english"],$rowlang["german"],$rowlang["slovak"],$rowlang["russian"],$rowlang["french"],$rowlang["spanish"],$rowlang["italian"]);
foreach($languages as $language)
if ($language=='N'){
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "'> " . $rowlang . "<br>";
}else{
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "' checked> " . $rowlang . "<br>";
}
which is almost working, the problem is that I can't echo the single language, because with this code I'm getting "array".
The problem is that trying to use $rowlang as part of the echo is a problem as it is an array.
Instead you can create an array of the languages you want to output, this also gives the name to be displayed as the value. So use a foreach() over this array and check in the $rowlang array to see if it is set. Rather than repeat the whole HTML, this just sets the checked attribute.
// Need to expand this array for all the countries you need
$languages = array("german" => "Germany","english" => "English");
foreach($languages as $language => $label) {
if ($rowlang[$language]=='N'){
$checked = '';
}else{
$checked = ' checked';
}
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "'$checked>" . $label . "<br>";
}
This will output something like
<input type='checkbox' name='german' value='german'>Germany<br>
<input type='checkbox' name='english' value='english' checked>English<br>
First create an associative array and store the languages and their values in it:
$languages = array('czech' => 'N','english' => 'Y','german' => 'N','slovak' => 'N','russian' => 'N','french' => 'N','spanish' => 'Y','italian' => 'N' );
then:
foreach($languages as $key => $value) {
if ($value=='N'){
echo "<input type='checkbox' name='" . $key . "' value='" . $value . "'> " . $key . "<br>";
}
else{
echo "<input type='checkbox' name='" . $key . "' value='" . $value . "' checked> " . $key . "<br>";
}
}
the result will be:
if it is array you should use $language->language_name or $langage['language_name'] as of your array type
This question already has an answer here:
Multiple hidden input with same name, always retrieve the last input [duplicate]
(1 answer)
Closed 4 years ago.
index.php
<form method="POST" action="add_to_db.php">
<?php
foreach ($response->getGraphEdge() as $graphNode) :
echo
"<div class='form-check mb-3'>" .
"<input type='radio' name='fb_name' class='form-check-input mt-3' value='".$graphNode['name']."'>" .
"<img class='mx-2' src='" . $graphNode['picture']['url'] . "'>" .
"<label class='form-check-label' for='fb_name'>" . $graphNode['name'] . '</label>' .
"<input type='hidden' name='fb_id' value='" . $graphNode['id'] . "'>" .
"<input type='hidden' name='fb_access_token' value='" . $graphNode['access_token'] . "'>" .
"</div>";
endforeach; ?>
</form>
add_to_db.php
if(isset($_POST['submit'])) {
$query = new db;
$b = $query->Query('SELECT * FROM user WHERE user_ID = 1');
$fb_id = $_POST['fb_id'];
$fb_name = $_POST['fb_name'];
$fb_access_token = $_POST['fb_access_token'];
$update = $query->Query("UPDATE user
SET user_fb_page_id = '$fb_id',
user_fb_page_name = '$fb_name',
user_fb_page_access_token = '$fb_access_token'
WHERE user_ID = 1 ");
if(!$update) {
echo '<div class="alert alert-success" role="alert">Updated Successful!</div>';
} else {
echo '<div class="alert alert-danger" role="alert">Oh no! Something went wrong ☹️</div>';
}
}
Problem:
When a user clicks on the Facebook Page that they want, the value to their name & access token is successfully updated to the database however it'll update the last fb_id to the db which is clearly not what I want! I am not sure at all why the ID does this yet none of the other attributes do it
The form input fields in one iteration of your foreach loop have the same names as the input fields in the other iterations.
This produces a form where all of the input fields that hold an id have the exact same name. (The same goes for the input fields that hold an access token and the radio buttons.) Because the fields have the same name, only the last value of each set is submitted to the server, with exception of the radio button which just submits the selected value.
To fix this, you need to give all of the fields in your form an unique name. I've done so here by adding a $key (alternatively you can use the fb_id, if that's unique).
When the form is submitted, only one value should exist in the POST array for $_POST['key'], this is the key of the selected radio button. You can then use this key to find the id, name and access token that go with it:
<form method="POST" action="add_to_db.php">
<?php
foreach ($response->getGraphEdge() as $key => $graphNode) :
echo
"<div class='form-check mb-3'>" .
"<input type='radio' name='key' class='form-check-input mt-3' value='".$key."'>" .
"<img class='mx-2' src='" . $graphNode['picture']['url'] . "'>" .
"<label class='form-check-label'>" . $graphNode['name'] . '</label>' .
"<input type='hidden' name='fb_name[".$key."]' value='" . $graphNode['name'] . "'>" .
"<input type='hidden' name='fb_id[".$key."]' value='" . $graphNode['id'] . "'>" .
"<input type='hidden' name='fb_access_token[".$key."]' value='" . $graphNode['access_token'] . "'>" .
"</div>";
endforeach; ?>
</form>
and
if(isset($_POST['submit'])) {
$query = new db;
$b = $query->Query('SELECT * FROM user WHERE user_ID = 1');
$key = $_POST['key'];
$fb_id = $_POST['fb_id'][$key];
$fb_name = $_POST['fb_name'][$key];
$fb_access_token = $_POST['fb_access_token'][$key];
$update = $query->Query("UPDATE user
SET user_fb_page_id = '$fb_id',
user_fb_page_name = '$fb_name',
user_fb_page_access_token = '$fb_access_token'
WHERE user_ID = 1 ");
if(!$update) {
echo '<div class="alert alert-success" role="alert">Updated Successful!</div>';
} else {
echo '<div class="alert alert-danger" role="alert">Oh no! Something went wrong ☹️</div>';
}
}
(I've removed the "label for" because it wasn't working. To get it to work you can add id='$key' to the radio input, and label for='$key' to the label.)
I have these textarea generated by while loop:
<form id='form_tbl' action='include/value.inc.php' method="POST"><input type="hidden" name="intrebare" value="1">
<?php
$sql = "SELECT NUME, PRENUME, TIP, ID
FROM personal
WHERE TIP <> 'Inactiv'
ORDER BY NUME ASC";
$result = $conn->query($sql);
echo "<table><tr><th>NUME</th><th>NOTA</th><th>SUGESTII</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td><input type='hidden' name ='id_personal[". $row['ID'] ."]' value='". $row["ID"]."'>" . $row["NUME"]. ' '. $row["PRENUME"]. "</td>";
echo "<td><select name='nota_pers[". $row['ID'] ."]' autocomplete='off'><option disabled selected>nota</option>";
for($i=1; $i<=10; $i++){
echo "<option value='$i'>$i</option>\n";
};
echo "</select></td>";
echo "<td><textarea name='sugestie' form='form_tbl' maxlength='200'></textarea></td></tr>";
}
echo '</table><button>NEXT ></button>';
?>
</form>
And value.inc.php:
<?php
include "bd_cnx.inc.php";
$insert_str = null;
$nota_pers = $_POST ['nota_pers'];
$intrebare = $_POST ['intrebare'];
$sugestie = $_POST ['sugestie'];
foreach ($nota_pers as $key => $value){
$insert_str [] = '(' . $key . ', ' . $value . ', ' . $intrebare . ', ' . $sugestie .')';
}
$var = implode(', ', $insert_str);
$sql = "INSERT INTO chestionar (ID_PERSONAL, NOTA, INTREBAREA, SUGESTII) VALUES " . $var;
?>
When I test with echo '<pre>'.print_r($insert_str,true).'</pre><br>'; the browser generated an array as: [0] => (55, 5, 1, Array). How can I repalce the array with the text from each textarea.
Thank you!
i wish to insert multiple entries in a single column..
inserting requires taking the values from the form in text field..
help me with retrieval..
here is my code for page1..
$result = mysqli_query($con, "SELECT * FROM cse");
while ($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row["name"] . "</td>" . "<td>" . $row["bupin"] . "</td>";
echo "<td>" . "<input type='text' name='1quiz1[".$row["bupin"]."]'>". "</td></tr>";
}
echo "</table>";
here is the code for final11.php
if (isset($_POST["1quiz1"]))
{
foreach ($presence as $key => $val)
{
mysqli_query($con,"Insert into cse ");
}
}
echo "Entered successfully..";
if (isset($_POST["1quiz1"])) {
$keys = "";
$values = "";
foreach ($presence as $key => $val) {
$keys .= $key+",";
$values .= $val+",";
}
// remove last commas
$keys = substr($keys, 0,-1);
$values = substr($values, 0,-1);
//insert
mysqli_query($con,"Insert into cse (".$keys.") values (".$values.")");
// you can here check the errors then if you need
}
echo "Entered successfully..";
You don't need to insert anything inside your input name attribute brackets. in other words, you have to make it something like the following:
echo "<td>" . "<input type='text' name='1quiz1[]'>". "</td></tr>";
I'm trying to Update a SQL Field so what i got is a Select and
$row = $stmt->fetch(PDO::FETCH_ASSOC);
....
$country_ausgabe = $row['country'];
So country_ausgabe as for example a value = de for german.
Also i have a array with all country codes in it.
So with this code i get all in a Select. $countries_de = the array with all country codes.
<select>
foreach ($countries_de as $key=>$country) {
echo "<option value='" . $key . "'>" . $country . "</option>";}
</select>
So but how can i use my $country_ausgabe to set this value in the select to selected?
use this code
<select>
foreach ($countries_de as $key=>$country) {
$selected = ($country_ausgabe == $key)?"SELECTED":"";
echo "<option value='" . $key . "' ".$selected." >" . $country . "</option>";}
</select>
try this
echo "<select>";
foreach ($countries_de as $key=>$country)
{
$selected="";
if($key==$country_ausgabe)
{
$selected = "selected";
}
echo "<option value='" . $key . "' " . $selected . " >" . $country . "</option>";
}
echo "</select>";