transform array in string php - php

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!

Related

PHP - Delete multiple rows via Checkbox

I have the following code, which works to a certain degree:
if(isset($_POST['remove'])) {
$user = $_POST['user'];
$passcode = $_POST['passcode'];
$sql = "SELECT *
FROM admin
WHERE username = '" . $user . "'
and passcode ='".md5($passcode)."'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count==0) {
echo "Invalid Credentials";
} else {
include 'config.php';
$cnt=array();
$listCheck = "'" .implode("','", $_POST['checkbox']) . "'";
$sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
$delete = mysqli_query ($conn,$sql6);
}
Issue is, it only deletes 1 of the selected rows. How do I modify the code so it deletes ALL of the selected rows?
<input type="submit" name="remove" class="btn2" value="Delete Selected">
<div>
<?php
$query1 = "SELECT `name` FROM `customer` ORDER BY `name` ASC ";
$result = mysqli_query($conn, $query1);
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value=" . $row['name'] . "></td>";
echo"</tr>";
}
echo "</table>";
echo "<br>";
?>
</div>
You can simply use one query to delete all records without the pain of for loop
include 'config.php';
$array = $_POST['checkbox'];
$listCheck = "'" . implode("','", $array) . "'";
echo $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
$delete = mysqli_query ($conn,$sql6);
Also there is issue in this code. Replace the below line with your code
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='" . $row['name'] . "'></td>";
better go with foreach( $_POST['checkbox'] as $k => $v), it may not always be numbers and even if this way you do not have a loop for each possible in range but only for each selected checkbox.
Also have a look on prepared statements for SQL queries, you do not want to have possible injections.
Besides LIKE better be replaced by the exact comparison =.
Try the following code:
include 'config.php';
$array = $_POST['checkbox'];
$listCheck = "";
foreach($array as $arr) {
$listCheck .= "'" . urldecode($arr) . "',";
}
$listCheck = substr(trim($listCheck), 0, -1);
echo $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
$delete = mysqli_query ($conn,$sql6);
This is something that caught my eye
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value" . $row['name'] . "></td>";
Should be
echo '<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$row['name'].'" ></td>';
The value of the checkbox is missing an =

Inserting dynamic multiple values in a single column in a table

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>";

How to insert different selected value from drop down list into database

Scenario : The administrator is to assign different companies to different student.
Problem : All student is given the same company of the last student in the form.
How do i make my hidden input work so that the correct selected drop down values of companies for each estudent to reflect correctly on the database?
$result = mysqli_query($con,"SELECT student_id, admin_no, name, GPA, gender FROM student_details WHERE jobscope1= 'Information Technology' ORDER BY `GPA` DESC; ");
$result2 = mysqli_query($con,"SELECT job_title FROM job_details WHERE jobscope='Information Technology' ORDER BY `job_title` ASC;");
/*options sections start*/
$options= '';
while ($row2 = mysqli_fetch_assoc($result2))
{
$options .='<option value="'. $row2['job_title'] .'"> '. $row2['job_title'] .'</option>';
}
/*options sections end*/
//return the array and loop through each row
while($row = mysqli_fetch_assoc($result))
{
$adminno = $row['admin_no'];
$name = $row['name'];
$gpa = $row['GPA'];
$gender = $row['gender'];
echo "<tr>";
echo '<input type=hidden name=admin_no value='. $adminno . '/>';
echo "<td>" . $adminno . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $gpa . "</td>";
echo "<td>" . $gender . "</td>";
echo "<td><select name='ddl' { myform.submit('') }'>".$options."</select></td>";
}
echo "</tr>";
The php form action :
$query = mysqli_query($con, "SELECT * FROM student_details WHERE jobscope1 = 'Information Technology';");
while ($row = mysqli_fetch_assoc($query))
$complist = $_POST['ddl'];
$result4 = mysqli_query($con, "UPDATE `student_details` SET `company`= '" . $complist . "' WHERE `jobscope1` = 'Information Technology';");
Try this:
//return the array and loop through each row
while($row = mysqli_fetch_assoc($result))
{
$adminno = $row['admin_no'];
$name = $row['name'];
$gpa = $row['GPA'];
$gender = $row['gender'];
echo "<tr>";
//changed here (this is called an input array which makes it hold multiple
//values with same name)
echo "<input type='hidden' name='admin_no[]' value='". $adminno ."'/>"; //edited
echo "<td>" . $adminno . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $gpa . "</td>";
echo "<td>" . $gender . "</td>";
//changed here too
echo "<td><select name='ddl[]' >".$options."</select></td>"; //edited
}
In your PHP:
if(isset($_POST['ddl'])){
foreach( $_POST['ddl'] as $index => $val ) //edited extra { here
{
$result4 = mysqli_query($con, "UPDATE `student_details`
SET `company`= '" . $val . "'
WHERE `jobscope1` = 'Information Technology'
AND `admin_no` = '".$_POST['admin_no'][$index]."';");
}
}
In this all your values will be updated (whether you changed any dropdown or not). If you want to limit only the changed dropdowns to be updated then you can have a hidden input variable which changes its value on change of dropdown and update your query only if the hidden input matches.

id of the input

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.

Get the total from foreach loop + mysql while loop

I am getting the $_post array and running a query on each iteration, then trying to get the total points accummulated, it seems to be overwriting the total with the last point iteration. How do I fix this?
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$total_donations = $row['points'];
}
}
}
$full_total += $total_donations;
echo $full_total;
You have to insert the $full_total in the foreach loop like this
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$full_total += $row['points'];
}
}
}
echo $full_total;

Categories