PDO MySQL update statement does not effect - php

I use two PHP files, one a_ndryshoje_produktin.php for posting data to be updated and one another produkti_ndryshuar.php that executes the posted data for update.
The problem for my question is that the file produkti_ndryshuar.php does execute without errors but there isn't any update of only the produkti_id with the produkti_i_ri_id on MySQL produktet table.
Below is my code for both files.
Thank you
echo "<form action='produkti_ndryshuar.php' method='post'>";
echo "<table class='tbl_manevro'>";
echo "<tr><td class='titujt'>";
echo "ID";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_i_ri_id' maxlength='60' value='".$row['produkti_id']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Kategoria";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='kategori_id' maxlength='60' value='".$row['kategori_id']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Emri";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_emri' maxlength='60' value='".$row['produkti_emri']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Çmimi";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_cmimi' maxlength='60' value='".$row['produkti_cmimi']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Fotografia";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_fotografia' maxlength='60' value='".$row['produkti_fotografia']."' />";
echo "</td></tr>";
echo "<input type='hidden' name='produkti_id' value=".$row['produkti_id']." />";
echo "<tr><td>";
echo "<input class='shtoje_btn' type='submit' value='Ndryshoje produktin' />";
echo "</td></tr>";
echo "</table>";
echo "</form>";
and the other file produkti_ndryshuar.php :
$produkti_i_ri_id=strip_tags($_POST['produkti_i_ri_id']);
$kategori_id=strip_tags($_POST['kategori_id']);
$produkti_emri=strip_tags($_POST['produkti_emri']);
$produkti_cmimi=strip_tags($_POST['produkti_cmimi']);
$produkti_fotografia=strip_tags($_POST['produkti_fotografia']);
$produkti_id=strip_tags($_POST['produkti_id']);
if($produkti_i_ri_id && $kategori_id && $produkti_emri && $produkti_cmimi && $produkti_fotografia ){
$db=include('dbconnect.php');
$query_2=$db->prepare("update produktet set
produkti_id=:produkti_i_ri_id,
kategori_id=:kategori_id,
produkti_emri=:produkti_emri,
produkti_cmimi=:produkti_cmimi,
produkti_fotografia=:produkti_fotografia
where produkti_id=:produkti_id");
$query_2->execute(array(':produkti_i_ri_id'=>$produkti_i_ri_id,
':kategori_id'=>$kategori_id,
':produkti_emri'=>$produkti_emri,
':produkti_cmimi'=>$produkti_cmimi,
':produkti_fotografia'=>$produkti_fotografia,
':produkti_id'=>$produkti_id));
echo "<span class='lajmi_sakte'>Produkti i caktuar u ndryshua me sukses.</span>";
}

You are giving values to your if statement, not booleans.
Try isset() :
$produkti_i_ri_id=strip_tags($_POST['produkti_i_ri_id']);
$kategori_id=strip_tags($_POST['kategori_id']);
$produkti_emri=strip_tags($_POST['produkti_emri']);
$produkti_cmimi=strip_tags($_POST['produkti_cmimi']);
$produkti_fotografia=strip_tags($_POST['produkti_fotografia']);
$produkti_id=strip_tags($_POST['produkti_id']);
if(isset($produkti_i_ri_id) && isset($kategori_id) && isset($produkti_emri) && isset($produkti_cmimi) && isset($produkti_fotografia) ){
$db=include('dbconnect.php');
$query_2=$db->prepare("update produktet set
produkti_id=:produkti_i_ri_id,
kategori_id=:kategori_id,
produkti_emri=:produkti_emri,
produkti_cmimi=:produkti_cmimi,
produkti_fotografia=:produkti_fotografia
where produkti_id=:produkti_id");
$query_2->execute(array(':produkti_i_ri_id'=>$produkti_i_ri_id,
':kategori_id'=>$kategori_id,
':produkti_emri'=>$produkti_emri,
':produkti_cmimi'=>$produkti_cmimi,
':produkti_fotografia'=>$produkti_fotografia,
':produkti_id'=>$produkti_id));
echo "<span class='lajmi_sakte'>Produkti i caktuar u ndryshua me sukses.</span>";
}
else{
echo "<span class='lajmi_mink'>Ju lutemi plotësoni të gjitha fushat!!!</span>";
}
And why are you using strip_tags() function?

Related

Stop button to repet in loop

Is there any way to stop a button from submit button to stop looping in while loop?
it just looks terrible to have like 15 submit buttons insted of 1. whitout moving it from the from
$sql = "SELECT * FROM `bestalning` WHERE lev=0
ORDER BY tid";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<div class='continer bg-vit table-rsponsive-sm '>";
echo "<table class='table'>";
echo "<thead class='thead-dark'>";
echo "<tr><th>Lev</th>";
echo "<th> </th>";
echo "<th>Artikelnr</th>";
echo "<th>Antal</th>";
echo "<th>Singnatur</th>";
echo "<th>Önskad ankomst</th>";
echo "<th>Skickad</th>";
echo "</tr></thead><tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form method='POST' action='ID_change.php'>";
echo "<td><input style='margin-right:5px;' name='levJA' type='checkbox' value='1'></td>";
echo "<td><input style='margin-right:5px;' name='RowID' type='hidden' value='".$row['ID']."'></td>";
echo "<td>".$row['artikelnr']."</td>";
echo "<td>".$row['antal']."</td>";
echo "<td>".$row['ovrigt']."</td>";
echo "<td>".$row['Tid']."</td>";
echo "<td>".$row['date']."</td>";
echo "<input class='btn btn-dark m-1' type='submit' value='submit'>;
echo "</form>";
echo "</tr>";
}
echo "</tbody></table>";
echo "</div>";
mysqli_close($conn);

Displaying data from database to select tag

I want to update the data from database using select tag but the problem is I don't know how to import data to select so that when I click the Edit button, the data will be imported. So far, I imported the data to the textbox. The only problem is the select tag.
PHP CODE:
echo "<div class='col-3'>";
echo "<div class='card'>";
echo "<div class='row'>";
echo "<div class='col-2'>";
echo "<h6 class='card-title'>".$row['project_name']."</h6>";
echo "</div>";
echo "<div class='col-2'>";
echo "<div class='card-setting'><i class='fa fa-gear'></i></div>";
echo "<div id='card-setting-dropdown' class='card-dropdown-content'>";
echo "<button class='card-dropdown-menu' id='btn-edit'>Edit Project</button>";
echo "<button class='card-dropdown-menu'>Delete Project</button>";
echo "<button class='card-dropdown-menu'>Add Task</button>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='row'>";
echo "<div class='col-2'>";
echo "<label class='project-details'>".$row['department']."</label>";
echo "</div>";
echo "<div class='col-2'>";
echo "<label class='project-details' style='float:right;'>Priority: <span style='color:".$color."'>".$row['priority']."</span></label>";
echo "</div>";
echo "</div>";
echo "<div class='pr-task-data'>";
echo "<div class='pr-task-summary-l'>";
echo "<label class='pr-task-title'>Tasks</label>";
echo "<p class='pr-task-details'>".$count_2."</p>";
echo "</div>";
echo "<div class='pr-task-summary-l'>";
echo "<label class='pr-task-title'>Completed</label>";
echo "<p class='pr-task-details'>".$count_3."</p>";
echo "</div>";
echo "<div class='pr-task-summary-l'>";
echo "<label class='pr-task-title'>In-Progress</label>";
echo "<p class='pr-task-details'>".$count_4."</p>";
echo "</div>";
echo "<div class='pr-task-summary-r'>";
echo "<label class='pr-task-title'>Not Completed</label>";
echo "<p class='pr-task-details'>".$count_5."</p>";
echo "</div>";
echo "</div>";
echo "<div class='progress'>";
echo "<div class='progress-bar' style='width:".$percent."%;'>";
echo "<label class='progress-bar-percent'>".$percent."%</label>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div id='modal_2' class='modal fade'>";
echo "<div class='modal-dialog'>";
echo "<div class='modal-content'>";
echo "<div class='modal-header'>";
echo "<h6 class='modal-title'>Add Project</h6>";
echo "<button type='button' class='close_2'>x</button>";
echo "</div>";
echo "<div class='modal-body'>";
echo "<form autocomplete='off' method='POST'>";
echo "<input type='hidden' id='".$row['project_id']."'>";
echo "<div class='form-group'>";
echo "<label class='form-control-label'>Program</label>";
echo "<input type='text' placeholder='Program' name='program' id='program_2' class='form-control'>";
echo "</div>";
echo "<div class='form-group'>";
echo "<label class='form-control-label'>Project Name</label>";
echo "<input type='text' placeholder='Project Name' name='pname' id='pname' class='form-control'>";
echo "</div>";
echo "<div class='form-group'>";
echo "<label class='form-control-label'>Project Description</label>";
echo "<input type='text' placeholder='Description' name='description' id='description' class='form-control'>";
echo "</div>";
echo "<div class='form-group'>";
echo "<label class='form-control-label'>Department</label>";
echo "<select class='form-control' id='department' name='department'>";
echo "<option value=''>Department</option>";
echo "<option value='Executive Department'>Executive Department</option>";
echo "<option value='CCA Department'>CCA Department</option>";
echo "</select>";
echo "</div>";
echo "<div class='form-group'>";
echo "<label class='form-control-label'>Priority</label>";
echo "<select class='form-control' id='priority' name='priority'>";
echo "<option value=''>Priority</option>";
echo "<option value='Low'>Low</option>";
echo "<option value='Medium'>Medium</option>";
echo "<option valie='High'>High</option>";
echo "<option valie='High'>High</option>";
echo "</select>";
echo "</div>";
echo "</div>";
echo "<div class='modal-footer'>";
echo "<button type='submit' class='btn btn-update' name='update'>Update</button>";
echo "<button type='button' class='btn btn-secondary'>Close</button>";
echo "</div>";
echo "</form>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<option value='Low' ".($row['priority']=='Low' ? 'selected' : '').">Low</option>";
add "selected" if database value is exactly same with option's value.

Update sql table of only rows where checkbox is checked php

I have a table of values that I am trying to update only the date of. I have a checkbox that I want only the select rows to updated when its checked and user clicks the submit button. The issue I am having is let's say I have 4 records and I only want record 1,2 and 4 updated. What is happening is records 1,2 and 3 are being updated. I know it has something to do with the array in the foreach loop. Side note I know the sql code allows for injection I am trying to get this to work first before worrying about that. Thank you for any and all help and time.
Table Code
$sql = "SELECT * FROM tblUnitMaster WHERE JobNumber = '" . $JobNumber . "' AND `EngReleaseDate` IS NULL OR `EngReleaseDate` = '' ORDER BY UnitID";
$result=$conn->query($sql);
echo "<style>";
echo "th {";
echo "background-color: #6EB4FF;";
echo "color: white;";
echo "}";
echo "</style>";
echo "<form action=pmReleaseEngineering3.php method=post>";
echo "<input type=hidden name=JobNumber value=$JobNumber />";
echo "<table border=2 cellpadding=4 cellspacing=10>";
echo "<tr>";
echo "<th><b><font size=4></font></b></th>";
echo "<th><b><font size=4>"."Job"."</font></b></th>";
echo "<th><b><font size=4>"."Code"."</font></b></th>";
echo "<th><b><font size=4>"."Model Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Serial Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Scope"."</font></b></th>";
echo "<th><b><font size=4>"."Date"."</font></b></th>";
echo "</tr>";
while ($row=$result->fetch_array())
{
echo "<tr>";
//echo "<td><font color=silver size=4>".$row[0]."</font></td>"; // Unit Number
echo "<td><input type=checkbox name=check[] value=".$row[0]." checked></td>";
echo "<td><input name=UnitID type=text font color=silver size=3 value=".$row[0]." readonly/></td>"; // Unit Number
echo "<td><font color=silver size=4>".$row[5]."</font></td>"; // Job Code
echo "<td><font color=silver size=4>".$row[2]."</font></td>"; // Model Number
echo "<td><font color=silver size=4>".$row[3]."</font></td>"; // Serial Number
echo "<td><font color=silver size=4>".$row[4]."</font></td>"; // Scope
echo "<td><input name=EngReleaseDate size=6 type=datetime value=" . date('Y-m-d'). " /></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<button class=button style=vertical-align:middle><span>Submit</span></button>";
echo "</form>";
echo "<br>";
PHP SQL Update TBL Code
include('pmconnect.php');
$JobNumber=$_REQUEST['JobNumber']; //array
$UnitID=$_POST['UnitID']; // array
$EngReleaseDate=$_POST['EngReleaseDate']; // array
if(isset($_POST['check'])){
//if(!empty($_POST['check'])) {
foreach($_POST['check'] as $a => $B){
$sql = "UPDATE tblUnitMaster SET " .
"EngReleaseDate='" . $EngReleaseDate . "'" .
"WHERE UnitID='" . $UnitID . "'";
//$result=$conn->query($sql);
if ($conn->multi_query($sql) === TRUE)
echo "<table border=1 cellpadding=4 cellspacing=10>";
echo "<tbody>";
echo "<tr>";
echo "<td><b><font size=4>Unit ID</font></b></td>";
echo "<td><b><font size=4>Eng. Release</font></b></td>";
echo "</tr>";
echo "<tr>";
echo "<td><font size=4> $UnitID </font></td>";
echo "<td><font size=4> $EngReleaseDate </font></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo"<br>";
}
}

Php empty array checking

I've a following html table which name contain array. How can i check this array if it's value is empty ?
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' /> ";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";
Currently i'm checking it with following code, it's not working but i know the name is array and it's must be compare with any array function. Can you guys give me a idea ?
if(isset($_POST['ch']))
{
$ch = $_POST['ch'];
if(empty($ch))
echo "<div class='error>Select attendence field. </div>";
}
Regards.
Update: (Full Code)
$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'>
Present All <input type= 'checkbox'
onclick='checkAll(this)'</td>";
echo "</tr>";
//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$id = (int) $res2['id'];
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";
//echo each radio with the counter for this row
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' /> ";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";
echo "</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
//add one to the counter
$counter++;
}
echo "<tr>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit'
class='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
if(isset($_POST['Submit']) && $_POST['Submit'] == "Record")
{
if(isset($_POST['ch']))
{
$ch = array_filter($_POST['ch']);
if (empty($ch))
{
echo "<div class='error>Select attendence field. </div>";
}
}
if(count($ch) == 0)
{
echo "<div class='error>Select attendence field. </div>";
}
else
{
foreach ($_POST['ch'] as $roll => $arr1)
{
$roll;
foreach ($arr1 as $name => $arr2)
{
$name;
foreach ($arr2 as $class => $value)
{
$class;
$value;
$sql = mysql_query("INSERT INTO e_attendence VALUES('', '$name', '$roll', '$class',
'$value', '$current_date')");
}
}
}
}
if($sql)
echo "<div class='success'>Succesfully recorded.
</div>";
}
empty() method work for finding whether the array is empty or not.
But for Your solution use below:-
if(isset($_POST['ch']) && $_POST['ch']=='1')
{
$ch = $_POST['ch'];
if(empty($ch))
echo "<div class='error>Select attendence field. </div>";
}
if You want to show a error message when no radio is selected then use below code:-
if(!isset($_POST['ch']) && $_POST['ch']=='')
{
echo "You have not selected any radio button";
}
use empty() instead of isset()
check the comparison table available at the below link
http://php.net/manual/en/types.comparisons.php
try this
print_r($_POST['ch']);
Try this
if(isset($_POST['ch'])){
$ch = array_filter($_POST['ch']);
if (empty($ch)) {
echo "<div class='error>Select attendence field. </div>";
}
}

PHP postback on submit

I have a question on php postback
My code is :
<?php
if(isset($_POST["Delete"]))
{
echo "DELETE";
}
if(isset($_POST["Modifier"]))
{
echo "Modifier";
}
if(!empty($_SESSION["Status"]))
{
if($_SESSION["Status"] == "u")
{
header("Location: Index.php?Action=Acceuil");
}
if($_SESSION["Status"] == "a")
{
$Connection = mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("tpw34") or die("Nope.");
$query = "Select * From Products";
$result = mysql_query($query);
While($ligne = mysql_fetch_assoc($result))
{
//Index.php?Action=AdminDeleteProduct&Delete=".$ligne["ProductID"]."
echo "<form method='POST' Action='#'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='2'><center><img width='250' height='250' src='".$ligne["Image"]."'/></center></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Nom du produit :</th>";
echo "<td>".$ligne["ProductName"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Prix :</th>";
echo "<td>".$ligne["Prix"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Description :</th>";
echo "<td>".$ligne["Description"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'h><input type='Submit' value='Delete' name='Delete'/><input type='Submit' value='Modifier' name='Modifier'/></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "</form>";
}
}
}
?>
My Question is : I want to get the ProductID of the item ( in the table ) to be in the $_POST["Delete"] and $_POST["Modifier"] but i dont wanna change the text on the button. I want to keep DELETE and MODIFIER. I have read many things on the web but i dont find the correct answer.
Include a hidden form value for ProductID. Then you can retrieve the value in $_POST['ProductID']
echo "<input type=hidden name='ProductID' value='" . $ligne["ProductID"] . "'>";
You could work with sessions, where you can temporarily save your information.
Sessions
Or like Tim Dearborn suggested, use a hidden input to send it with the next form submit.

Categories