I'm using a form with several checkboxes to make the user to select which rows he want to delete. The code to generate the form is this:
if ($busqueda!=""){
$busca = mysqli_query($con, " SELECT id_user FROM usuarios WHERE
id_user LIKE '%$busqueda%'");
echo "<form action='borrar.php' method='post' enctype='multipart/form-data'>";
while ($array= mysqli_fetch_array($busca)){
$user=makestring($array);
echo"Usuario: <input type='checkbox' name='borrar_usuario' value='$user' />$user<br/>";
}
echo " <input type='submit' name='boton' value='eliminar' />";
echo "</form>";
The code in borrar.php to delete the rows is this:
if(isset($_REQUEST["borrar_usuario"])) {
foreach ($_POST as $valor){
$del_user = $valor;
$q_borrar = "delete from usuarios where id_user in ('$del_user')";
mysqli_query($con, $q_borrar)or die(mysqli_error());
}
//echo "usuario borrado";
header('refresh: 3; url= exito.php');
}
The problem is that only deletes the first row selected and I do not know how to make it to delete all the options selected in the form. Any help, please?
First change the name of your checkbox to include []. Then when you submit the form php will convert it to an array.
<input type='checkbox' name='borrar_usuario[]' value='$user' />
Then in the php code that handles the form submission remove the loop and have this
if(isset($_REQUEST["borrar_usuario"])) {
$q_borrar = "delete from usuarios where id_user in ('" . implode("','", $_POST['borrar_usuario']) . "')";
mysqli_query($con, $q_borrar)or die(mysqli_error());
//echo "usuario borrado";
header('refresh: 3; url= exito.php');
}
I think this will work :
if(isset($_POST['borrar_usuario']))
{
$q_borrar = "delete from usuarios where id_user in ($_POST['borrar_usuario'])";
mysqli_query($con, $q_borrar) or die(mysqli_error());
}
Related
first sorry for my english. I have a problem on my isset Here's the codes:
PHP:
if(isset($_POST['insert'])) {
$insert = $_POST['insert'];
}
{
require('./clanconfig.php');
$cln = $_POST['clanname'];
$cms = $_POST['mesa'];
$checkup = "SELECT id FROM clan WHERE cname='$cln'";
$upsql = mysqli_query($conn, $checkup);
while($srcclan=mysqli_fetch_array($upsql) )
{
$checked = $srcclan['id'];
}
$sql2 = "INSERT INTO clanrequest (clanid, plname, message, playerid) VALUES('$cln', '$uname', '$cms', '$player_id')";
$sql3 = mysqli_query($conn, $sql2) or die();
}
mysqli_close($conn);
Problem is While searching on table1 with a Post value it's okay but while inserting on table 2 with a id of table 1 and on table 2 adding but value is only 0 on id column
Html:
<form class="clan-form-join action="clanjoin.php" method="post">
<?php
require('./clanconfig.php');
$sql = "SELECT * FROM clan";
$sql2 = mysqli_query($conn, $sql);
echo "<html>";
echo "<body>";
echo "<select name='clanname'>";
while($sonuct=mysqli_fetch_array($sql2) )
{
$cnamer = $sonuct['cname'];
echo '<option value=" '.$cnamer.'">'.$cnamer.'</option><br />';
}
echo "</select>";
echo "</body>";
echo "</html><br>";
echo'<b>Message</b><br><textarea name="mesa" rows=3 cols=40></textarea><br/>';
echo'<input type="submit" name="insert" class="clanbutton" value=" Send Application ">';
?>
</form>
Here's the html codes, i select the details of clan in a clan table and when member select the clan name and insert the button the codes sent with a clan name selected like test clan in isset value and in the isset select the id of clan selected on option value clan name on table clan and return to insert clan request with name,id of player and where id of clan the problem is id of clan adding automatically 0 without add real id of clan
All the code for inserting should be inside the if (isset($_POST['insert'])). You only have the variable assignment there (and you never even use that variable), you have the rest of the code in a separate block (there's no purpose to putting it in a block).
if(isset($_POST['insert'])) {
$insert = $_POST['insert'];
require('./clanconfig.php');
$cln = $_POST['clanname'];
$cms = $_POST['mesa'];
$checkup = "SELECT id FROM clan WHERE cname='$cln'";
$upsql = mysqli_query($conn, $checkup);
while($srcclan=mysqli_fetch_array($upsql) )
{
$checked = $srcclan['id'];
}
$sql2 = "INSERT INTO clanrequest (clanid, plname, message, playerid) VALUES('$cln', '$uname', '$cms', '$player_id')";
$sql3 = mysqli_query($conn, $sql2) or die();
mysqli_close($conn);
}
I need to enter scores for all subjects offered by all students in a class simultaneously. So I retrieved the students ID, retrieved the list of subjects being offered by the students, and placed a textbox under each subject.
Now, I want to submit the score in the database as well as the subject name and the student ID, but the score is not storing, just the subject name and the student ID.
Here is my code:
<form method="post">
<?php
include "includes/dbcon.php";
$subject_name ="";
echo "<table border='1'><thead><tr><td>Students Name</td>";
$query_subjects = mysqli_query($link,"SELECT * FROM junior_subjects ORDER BY subject_name ASC");
while ($row_subject=mysqli_fetch_array($query_subjects))
{
$subject_name .= $row_subject['subject_name'];
echo "<td>".$row_subject['subject_name']."</td>";
}
echo "</tr></thead>";
$query_students = mysqli_query($link,"SELECT * FROM students WHERE class_cat='Junior'");
while ($row_students=mysqli_fetch_array($query_students))
{
$student_id = $row_students['student_id'];
echo "<tr><td>".$row_students['student_id']."</td>";
$query_subjects2 = mysqli_query($link,"SELECT * FROM junior_subjects ORDER BY subject_name ASC");
while ($row_subject2 =mysqli_fetch_array($query_subjects2))
{
$subject_name2 =$row_subject2['subject_name'];
echo "<td>
<input type='text' hidden name='$subject_name2'>
<input type='text' size='4' name='$subject_name2'>
</td>";
/////
if (isset($_POST['submit']))
{
$score = $_POST[$subject_name2];
mysqli_query($link,"INSERT INTO score_sheet(student_id,subject_name,score) VALUES('$student_id','$subject_name2','$score') ");
}
}
}
?>
<input type='submit' name='submit'>
</form>
You should have the input score as array and make a foreach loop to insert the query.
You should have the input field like this
And do a foreach loop like this
$score = $_POST['score'];
foreach ($score as $key)
{
$query = "INSERT INTO score_sheet(student_id,subject_name,score) VALUES('$student_id','$subject_name2','$key') ";
$query = mysqli_query($link,$query);
}
Note :
You should not have have two forms inside it.
I have made an eval for you
Warning :
It is not at all a good practise to have such a bulk entry.
I have rows showing products and their stockings.
I can pull out records from the database but somehow I am stuck at the editing part. When I click on edit I don't know how to pass the id so I can use the id to select what is needed to be selected in the table.
I have something like stock.php which shows all item_name and stock
<?php
$sql = "SELECT * FROM inventory";
$result = mysqli_query($mysqli,$sql);
//make sure database queries
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
if ($result->num_rows > 0) {
echo "<table><tr><td>Name</td>
<td>Stock</td>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
echo "<tr>
<td>".$row["item_name"]."</td>
<td>".$row["stock"]."</td>
<td>
<form action='edit.php' method='POST'>
<input type='hidden' name='$id' value='$id'/>
<input type='submit' name='edit' value='edit' />
</form>
</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
in my edit.php I have something like this which is giving me error because the $id is not passed
include_once "init.php";
if ($_SERVER['REQUEST_METHOD']=='POST') {
// query the table by matching up the int id
$sql = "SELECT * FROM inventory WHERE id = '$id'";
}
I want to pull out the data again in a new page and then having input box again to let users update stock # or even the product name and others if necessary.
There is an error in your input declaration. HTML items can't have names that start with special characters (like the $). You should be setting the variable-based names this way:
<input type='hidden' name='".$id."' value='".$id."'/>
Also in your edit.php, your SQL statement has a bug.
$sql = "SELECT * FROM inventory WHERE id = '$id'";
should be:
$sql = "SELECT * FROM inventory WHERE id = ".intval($id);
You need to do the following updates:
stock.php
<input type='hidden' name='id' value='$id'/>
edit.php
$sql = "SELECT * FROM inventory WHERE id = '".$_POST["id"]."'";
you are passing the current id value as the post var name, the var's name should be "id"
I am newbie in PHP and with this less knowledge of PHP i developed a form to update the rows of MySQL table. But the problem is that if i edit one row in index.php file and submit it then that row value comes to all rows of the table.
I want to change the edited inputs and the rest inputs should be unchanged.
Please note that first row budget value will be added when second row budget value and will be inserted in total input and will save to database
This is how it looks
this is the index.php file
<?php
$con=mysqli_connect("127.0.0.1","root","","ji001");
$result = mysqli_query($con,"SELECT * FROM finance")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{ $Budget = $row['Budget'];
$Availed_in_Regions = $row['Availed_in_Regions'];
echo "<form style='width:780px' action='update2.php' method='post' class='form-group'>
id<input type='text' name='Budget' value='".$row['ID']."'>
Budget<input type='text' name='Budget' value='".$row['Budget']."'>
Availed in Regions <input type='text' name='Availed_in_Regions' value='".$row['Availed_in_Regions']."'>
<input type='Submit'>
</form>";
}
?>
This is the update2.php file
<?php
mysql_connect('127.0.0.1', 'root', '') or die(mysql_error());
mysql_select_db("ji001") or die(mysql_error());
$ud_Budget = mysql_real_escape_string($_POST["Budget"]);
$ud_Availed_in_Regions = mysql_real_escape_string($_POST["Availed_in_Regions"]);
$query="UPDATE finance SET Budget = '$ud_Budget', Availed_in_Regions = '$ud_Availed_in_Regions'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>Record Updated<p>";
}else{
echo "<p>Not Updated<p>";
}
?>
Firstly, your form having typo id and name having same name for the name attribute i.e. name='Budget'
index.php
echo "<form style='width:780px' action='update2.php' method='post' class='form-group'>";
while($row = mysqli_fetch_array($result))
{
$Budget = $row['Budget'];
$Availed_in_Regions = $row['Availed_in_Regions'];
echo "id<input type='text' name='id[]' value='".$row['ID']."'>
Budget<input type='text' name='Budget[]' value='".$row['Budget']."'>
Availed in Regions <input type='text' name='Availed_in_Regions[]' value='".$row['Availed_in_Regions']."'>";
}
echo "<input type='Submit' value='Submit'></form>";
and you need to change your update query from
update2.php
$ud_id = $_POST["id"];
$ud_Budget = array_map('mysql_real_escape_string', $_POST['Budget']);
$ud_Availed_in_Regions = array_map('mysql_real_escape_string',$_POST["Availed_in_Regions"]);
foreach($ud_id as $key => $value){
$query = "UPDATE finance SET Budget = '$ud_Budget[$key]', Availed_in_Regions = '$ud_Availed_in_Regions[$key]' where ID = $value";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>Record Updated<p>";
}else{
echo "<p>Not Updated<p>";
}
}
Here I have added where condition which is required to identify which row you need to update and
NOTICE : You were mixing two API within index.php you were using mysqli_ and within update2.php its mysql
You are not using a where clause in the $query query.
it should be something like this
$query="UPDATE finance SET Budget = '$ud_Budget', Availed_in_Regions = '$ud_Availed_in_Regions' WHERE id = '$id'";
You should also pass this id in the form, also change the name of the input field containing the id...
Because you actually SELECT every row in your table.
UPDATE finance SET Budget = '$ud_Budget', Availed_in_Regions = '$ud_Availed_in_Regions';
There is no WHERE clause (link) in your query you have to use it if you wanna update a row in your table.
Pass $id to your update2.php and use this query instead:
UPDATE finance SET Budget = '$ud_Budget', Availed_in_Regions = '$ud_Availed_in_Regions' WHERE id = '$id';
I found a way to delete rows from a table "dynamic", associating the id of each button to be erased, this id is taken from the table.
Associate with each button this line of code:
"<td><form method='post' action='#'><input type='button' value='Elimina' name='delete' id='back'></form></td>";
then run a check to see if it is pressed:
if(isset($_POST['delete'])&& isset($_GET['id']))
{
$connessione = new MySQLi(HOST, USER, PASSWORD, DATABASE);
$query = "DELETE FROM workpaper WHERE id = ?";
$result = mysql_query($query);
if (!$result)
{
die("Errore nella query $query: " . mysql_error());
}
mysql_close();
}
but the page is reloaded, without making any query, how can it?
Your query is looking for something that appears to be something:
$query = "DELETE FROM workpaper WHERE id = ?";
Maybe you should add $_GET['id'] as a value to the clause.
change:
$query = "DELETE FROM workpaper WHERE id = ?";
with:
$query = "DELETE FROM workpaper WHERE id = ".$_POST['id'];
"<td><form method='post' action='".$_SERVER['PHP_SELF']."'><input type='hidden' name='id' value='".$attivitaID."\" /><input type='button' value='Elimina' name='delete' id='back'></form></td>";