Good morning everybody
I made a table in html that contains users and the select box contains profiles.
I want to assign every users profile in database according to the profile selected in html.
My table in html contains checkbox next to every user. I want every user that is checked to be assigned.
<form action="PHP/modifier.php" method="POSt">
<table>
<thead>
<tr>
<th><input type="checkbox" id="checker" name="all"></th>
<th>Username</th>
<th>Nom</th>
<th>Prenom</th>
</tr>
</thead>
<?php
$query="SELECT Username,Nom,Prenom from user";
$result = mysql_query($query);
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++){
$row =mysql_fetch_array($result);
$Username = $row['Username'];
$Nom = $row['Nom'];
$Prenom = $row['Prenom'];
echo "<tr id='row'>";
echo "<td><input type='checkbox' id='check' name='check'".$Username."></td>";
echo"<td>".$Username."</td>";
echo "<td>".$Nom."</td>";
echo "<td>".$Prenom."</td>";
echo "<td><div id='MAJ'>";
echo "</div></td>";
echo "</tr>";
}
?>
<h2>Selectionner l'utilisateur et le profile</h2>
<select name="profile" id="profilez" required>
<option>Injecteur</option>
<option>Utilisateur</option>
<option>Administrateur</option>
<option>Utilisateur superieur</option>
<input type="submit" value="Associer">
</form>
In the modifer.php
I want to make update function to update users but how to get information from checked cases in the html table?
I already wrote this
<?php
include "functions.php";
$profile=$_POST['profile'];
$Username=$_POST['Username'];
$query="UPDATE user SET profile = '$profile' WHERE Username = '$Username' " ;
?>
I don't know how to do it.
thank you so much and sorry for my bad english.
there are different ways to do it. but lets go with your way
on you modifer.php page make another query to get all user names
$row =mysql_fetch_array($result);
foreach ($row as $value)
{
if (isset($_POST['check'.$value['username']]))
{
$profile = $_POST['profile'];
$Username = $POST['check'.$value['username'];
$query="UPDATE user SET profile = '$profile' WHERE Username = $value['username'] " ;
}
}
Related
I'm currently trying to generate independent radio buttons for each row read from my server. They are supposed to symbolize ON and OFF Switches. My current issue is that I manage to generate buttons for each row but these are not independent. I can only turn one off or on at a time.
I know that radio buttons become truly "independent" when the name of each is different. But I can't seem to figure out a way to create a new name for each row generated.
Currently my code looks like this
res = $db->query($query);
if ($res->num_rows > 0) {
while ($row = $res->fetch_object()) {
$query = "SELECT * FROM device_status
WHERE device = ($row->id)";
$status = $db->query($query);
$device_status = $status->fetch_object();
$content .= <<<END
<tr>
<td>{$row->devices}</td>
<td>{$row->description}</td>
<td>{$row->room}</td>
<td><input type='radio' name='radiobutton' id='myCheck'></td>
<td><input type='radio' name='radiobutton' id='myCheck' checked='checked'></td>
<!-- <td>{$device_status->status}</td> -->
<td>Delete
Edit</td>
</tr>
END;
}
}
echo $content;
I've been trying different methods such as using <td><input type='radio' name='$row->id' id='myCheck'></td>
But have not gotten it to work. Any help on how to solve this issue is greatly appreciated.
Thank you!
If you want the unique names for all the checkboxes generated you can have them by just incrementing $i for each row.
res = $db->query($query);
if ($res->num_rows > 0) {
$i = 0;
while ($row = $res->fetch_object()) {
$query = "SELECT * FROM device_status
WHERE device = ($row->id)";
$status = $db->query($query);
$device_status = $status->fetch_object();
$content .= <<<END
<tr>
<td>{$row->devices}</td>
<td>{$row->description}</td>
<td>{$row->room}</td>
<td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck'></td>
<td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck' checked='checked'></td>
<!-- <td>{$device_status->status}</td> -->
<td>Delete
Edit</td>
</tr>
END;
$i++;
}
}
echo $content;
If you do not care about the name of the radio button, you could use a "php random number generator", each row you asign a new random number to your radio button name:
<?php $x = rand(5, 100000); ?> <input type='radio' name='<?php echo $x; ?>' id='myCheck'>
I am trying to create a form, basing on information picked from the database using a while loop. I need some guidance on how to go about this. When form is supposed to return multiple values (of students) and then a text field for inputting marks is assigned to the students returned, which should allow the user input the marks and then submit the form.
Here is the code i have sofar
<form method="post">
<?php
//picking student details
$sql = mysql_query("SELECT *
FROM student, class, subject, assesment
WHERE
student.class_id = '$assesment_class'
AND subject.idsubject = '$assesment_subject'
AND subject.subject_option = 'Major'
AND class.idclass = student.class_id
AND class.idclass = subject.class_id
AND assesment.subject_idsubject = subject.idsubject
AND assesment.idassesment = '$ass'
");
$Count = mysql_num_rows($sql); //counting the the selected rows
if($Count > 0){
while($row = mysql_fetch_array($sql)){
//picking database values
$student_id = $row["idstudent"];
$student_names = $row["student_names"];
//Query for checking results
$sql1 = mysql_query("SELECT *
FROM result, student_has_result, grade, student, assesment
WHERE
student_has_result.student_id = '$student_id'
AND student_has_result.student_class_id = '$assesment_class'
AND student_has_result.assesment_id = '$ass'
AND student_has_result.result_id = result.idresult
AND result.grade_idgrade = grade.idgrade
AND student_has_result.assesment_id = assesment.idassesment
AND student_has_result.student_id = student.idstudent
AND student_has_result.result_id = result.idresult");
$Count1 = mysql_num_rows($sql1); //counting the the selected rows
if($Count1 > 0){
while($row = mysql_fetch_array($sql1)){
//picking database values
$mark = $row["mark"];
//echo $mark;
}
}else{
$mark = "";
}
?>
<tr>
<td><?php echo $student_names; ?></td>
<td>
<input type="text" name="result" placeholder="<?php echo $mark; ?>"/>
<input type="hidden" name="student" value="<?php echo $student_id; ?>"/>
<input type="hidden" name="assesment" value="<?php echo $ass; ?>"/>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td><button name="submit-results-button" type="submit" class="btn btn-success">-Submit Students Results Now-</button><br><br></td>
</tr>
</form>
Any guidance will be highly appreciated.
i am very novice to php and mysqli and found a great tutorial but am needing some help.
i am wanting a row to be linkable and send it to another page named single.php?id=ROWID so it will show the single entry
this is what i got so far.
<html>
<head>
<title>MySQLi Tutorial</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}
}
$query = "select * from users";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results ){
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th><a href=\"single.php?id={$id}\">Firstname</></th>";
echo "<th>Lastname</th>";
echo "<th>Username</th>";
echo "<th>Action</th>";
echo "</tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$firstname}</td>";
echo "<td>{$lastname}</td>";
echo "<td>{$username}</td>";
echo "<td>";
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
<script type='text/javascript'>
function delete_user( id ){
//this script helps us to
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'index.php?action=delete&id=' + id;
}
}
</script>
</body>
</html>
i am right in thinking i would be sending the rows id in the url ?
echo "<th><a href=\"single.php?id={$id}\">Firstname</></th>";
but i am having issues with single.php what code would i have to put to show the single entry?
i have been on this a while and got no were near so i deleted the code and swallowed my pride to seek some help :/
thanks in advance
Thank you for the interesting question.
First, let me inform you that, although you are using a moder-looking database access library, the way you are using it is as ancient as a mammoth fossil.
Several things to consider
Never use mysqli as is, but only in the form of some higher level abstraction library.
Never use real_escape_string in the application code but use prepared statements only.
Never mix your database code with HTML output. Get your data first, then start for output.
Never use GET method to modify the data.
Here goes the example based on the above principles. It does ALL basic CRUD operations:
<?
include 'safemysql.class.php'; // a library
$db = new SafeMysql();
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') {
if (isset($_POST['delete'])) {
$db->query("DELETE FROM ?n WHERE id=?i",$table,$_POST['delete']);
} elseif ($_POST['id']) {
$db->query("UPDATE ?n SET name=?s WHERE id=?i",$table,$_POST['name'],$_POST['id']);
} else {
$db->query("INSERT INTO ?n SET name=?s",$table,$_POST['name']);
}
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) {
$LIST = $db->getAll("SELECT * FROM ?n",$table);
include 'list.php';
} else {
if ($_GET['id']) {
$row = $db->getRow("SELECT * FROM ?n WHERE id=?i", $table, $_GET['id']);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
It is using templates to display the data:
list.php
Add item
<? foreach ($LIST as $row): ?>
<li><?=$row['name']?>
<? endforeach ?>
and form.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
Return to the list
</form>
<? if ($row['id']):?>
<div align=right>
<form method="POST">
<input type="hidden" name="delete" value="<?=$row['id']?>">
<input type="submit" value="Удалить"><br>
</form>
</div>
<?endif?>
here goes the part for display.
if ($_GET['id']) {
$row = $db->getRow("SELECT * FROM ?n WHERE id=?i", $table, $_GET['id']);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
if you don't want to show the form - create another template called single.php with whatever markup you wish
Single.php
I Use PDO if u want you can make it with MySQLi too.
<?php
include("db_connect.php"); // database configuration file
if(isset($_GET['id'])
{
$id = (int) $_GET['id'];
$sql = "SELECT * FROM `users` WHERE id=?";
$query = $conn->prepare($sql); // $conn is PDO object yours can be different
$query->bindValue(1,$id);
$query->execute();
if($query){
$row = $query->fetch(); //
}else{
echo "Error with Database";
}
}
else // Error for the Id selection
{
echo("ID is not selected");
}
?>
No while loop because you want just 1 record. $row variable is just for test because i don't know your fields in your DB
<table border="1">
<tr>
<td>ID</td>
<td>Firstname</td>
<td>Lastname</td>
</tr>
<tr>
<td><?php echo $row['id]; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
</tr>
</table>
in your single.php
$id=$_GET['id'];
$query="select * from users where id='".$id."'";
I am trying to delete , edit and add new recodes on the same page but it seems am failing to make it work .And I do not want to do it using ajax jquery or java script but only php .I need some help please below are my code :
<?php
include_once('con.php');
$strSQL = "SELECT film_id, name
from
filmsbox";
$rs = mysql_query($strSQL);
echo "<table border='1' ><tr bgcolor='#eeeeee'><td>Name</td> <td colspan='2'>Action</td></tr>";
while($row = mysql_fetch_assoc($rs))
{
$film_id = $row['film_id'];
$name = $row['name'];
$hometeam= mysql_real_escape_string($name);
echo "<tr bgcolor='#eeeee'><td>$name</td> <td><a href='index.php?film_id=$film_id' name ='edit'>Edit</a></td><td><a href='index.php?film_id=$film_id' name ='delete'>Delete</a></td></tr>";
}
?>
<?php
$strSQL = "SELECT film_id, name
from
filmsbox";
$rs = mysql_query($strSQL);
$row = mysql_fetch_assoc($rs);
$film_id= $row['film_id'];
$name = $row['name'];
$name = mysql_real_escape_string($name);
$film_id= $_GET['film_id'];
?>
<?php
if(isset($_POST['edit'])){
?>
<table>
<form action="index.php" method="post">
<tr>
<td>
Name
</td>
<td>
<input type = "text" name = "name" value="<?php echo $name;?>">
</td>
</tr>
<input name="film_id" type="hidden" id="film_id" value="<?php echo $film_id; ?>">
<tr>
<td>
<input type = "submit" name = "submit" value="update">
</td>
</tr>
<?php
$name = (isset($_POST['name']))? trim($_POST['name']): '';
$film_id = $_POST['film_id'];
$sql = "UPDATE filmsbox SET name='$name'
WHERE film_id ='$film_id'";
$result = mysql_query($sql);
if($result)
{
echo "Success";
}
else
{
echo "Error";
}
}
?>
<?php
/*Delete section*/
if(isset($_POST['delete']))
{
$film_id = $_GET['film_id'];
$delete = "DELETE FROM filmsbox WHERE film_id = '$film_id'";
$result = mysql_query($delete);
if($result)
{
echo "Record deleted successfuly ";
}
else
{
echo "No data deleted";
}
}
?>
Couple of pointers:
You only need to escape values before they go into the database, not when they come out and are used in HTML i.e $hometeam = mysql_real_escape_string($name);
You are pulling the same query from the database twice in quick succession which is not needed. You can remove one of the 2 $strSQL = "SELECT film_id, name
from
filmsbox";
$rs = mysql_query($strSQL); sections from the top of your code
You need to run any update/delete queries on the data before you then do your select query to pull out the records for the page, otherwise your changes will not be shown
You should be escaping the values for your update and delete queries to prevent SQL injection
Edit:
To reload the page in an edit mode, you need to change the link URL in the table to something like
<a href='index.php?film_id=$film_id&edit=1' name ='edit'>Edit</a>
Then your edit block needs to be
if ($_GET['edit']) {
I want to be clear this is not in any way a secure method of editing values, as anyone can put ?edit=1 on the url and get to the form
I have a form that allows the user to add or remove users from a job by using checkboxes. At the moment the engineer's names etc are stored in a table called 'users' and they are identified by their user type, engineers. The checkboxes on the form are created by using a while loop to display a mysql query. This is done so that new users can be added to the database via a webpage at a later date and their name will automatically be added to the list of available engineers. To add an engineer to a job their name is simply ticked and the form submitted.
This works well but I would like the user to be able to add or remove users by checking and unchecking boxes on another form but I can't think for the life of me how to do this.
The users assigned to a job are stored in a many-to-many table called calls_engineers which has 3 fields
id_ce (unique id)
call_ce (id of job, a foreign key)
engineer_ce (id of engineer, a foreign key)
The engineer_ce is the users primary key in the 'users' table which is 'id'.
The code I have so far is...
<? $sql = "SELECT * FROM calls_engineers WHERE call_ce = '$diary_id'";
$result = mysql_query($sql) or die(mysql_error());
$row2 = mysql_fetch_array($result);
$sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ ?>
<div style="width:70px; float:left">
<?
if($row2['engineer_ce'] == $row['id']){
?>
<input type="checkbox" name="engineer[]" checked="checked" value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name'];
} else { ?>
<input type="checkbox" name="engineer[]" value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name'];
}?>
</div>
<?
}
?>
All this does is create checkboxes for each user which is an engineer but it doesn't check them if the user is assigned to the job.
Any help will be greatly appreciated
You never loops the calls_engineers table. Using
$row2 = mysql_fetch_array($result);
Will not actually find all assignments for the engineer. Now check every single engineer if this is included in current job:
<?
$sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ ?>
<div style="width:70px; float:left">
<input type="checkbox" name="engineer[]" <?
$result2 = mysql_query("SELECT * FROM calls_engineers WHERE call_ce = '".mysql_real_escape_string($diary_id)."' AND engineer_ce = ".$row['id']);
if(mysql_num_rows($result2) > 0) echo 'checked="checked"';
?> value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name']; ?>
</div>
<? } ?>