this codes below came from my friend. i want to make a search under this codes . from this codes , it will display all the users to delete. what i want is i want to put search box and search button . so when i insert user id and press search only then it display which user that i want . where should i put this search text , button and php ? i want to put it under this else if codes . help me. im new :(
else if($_SESSION['jawatan'] == 'ADMIN') { ?>
<li> VIEW PROFILE </li>
<li> VIEW REQUEST </li>
<li>LOG OUT</li>
<li><img src="msc.jpg" width = "240" height ="80"></li>
<form method="get">
<input type="text" name="userid" placeholder="search" />
<inpu type="submit" value="Search" />
</form>
<h3>View User</h3>
<table border='1'>
<tr>
<td><b>#</b></td>
<td><b>Nama</b></td>
<td><b>Email</b></td>
<td><b>Division</b></td>
<td><b>Department</b></td>
<td><b>Delete</b></td>
</tr>
<?php
$i = 1;
$whr="";
if(isset($_GET['userid'])){
$whr.= " and mem_id='".$_GET['userid']."'"; // which field you want. If you want to do search name use LIKE instead =(equal to)
}
$result = mysql_query("SELECT * FROM members WHERE mem_role='USER' ".$whr);
if(mysql_num_rows($result)>0) {
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $row['mem_name']; ?>
</td>
<td>
<?php echo $row['mem_email']; ?>
</td>
<td>
<?php echo $row['mem_division']; ?>
</td>
<td>
<?php echo $row['mem_department']; ?>
</td>
<td>Delete</td>
</tr>
<?php
$i++;
}
} else {
echo '<tr><td>No results found</td></tr>';
}
?>
</table>
<?php
}
HTML
<form method="get">
<input type="text" name="userid" placeholder="search" />
<inpu type="submit" value="Search" />
</form>
PHP
$whr="";
if(isset($_GET['userid'])){
$whr.= " and mem_id='".$_GET['userid']."'"; // which field you want. If you want to do search name use LIKE instead =(equal to)
}
MYSQL
$result = $result=mysql_query("SELECT * FROM members WHERE mem_role='USER' ".$whr);// add whr variable here
I can tell you the steps:
Create search text field and search button ( wrap in form if you want to do with php only)
on click of button ( that would be submit ), fetch that value from search text box and run query based on that
when results appear , fill the table data with that results using foreach.
hope it helps. Ask me for any issue.
Related
My code is running a cycle that adds textareas where the user can input queries, which the div it is in also contains a dropdown with a list of servers available to run those queries in. At first it was working just fine for only the first iteration and not showing the list at all in the next dropdowns, but while I was messing with the code and pasted the query inside the cycle all dropdowns were filled but the server ID of the first textarea stopped being posted correctly, instead sending the value that's currently in the database. By other words it won't update properly.
<?php
if(mysqli_num_rows($result_query) > 0){
while($rowq = mysqli_fetch_assoc($result_query)){
$sql_servers = "SELECT id, name, address FROM servers ORDER BY id ASC";
$result_servers = mysqli_query($link, $sql_servers);
?>
<table>
<form name="formStep" method="post" action="">
<br>
<tr>
<textarea class="form-control scrollabletextbox" id="query<?php echo $rowq['step']?>" name="query<?php echo $rowq['step']?>"><?php echo $rowq['query'];?></textarea>
</tr>
<tr>
<td width="25%" style="vertical-align:middle;"><select id="server" name="server" class="form-control input-md">
<?php
if (mysqli_num_rows($result_servers) > 0) {
while($rows = mysqli_fetch_assoc($result_servers)){ ?>
<option value="<?php echo $rows["id"];?>" <?php if($rows['id']==$row_query2['id_server']) echo 'selected=\"selected\"' ?> ><?php echo $rows["name"];?></option>
<?php
}
}?>
</select>
</td>
<td style="padding:10px;"><input type="submit" name="submit" formaction="save.php?i=4&id=<?php echo $id; ?>&s=<?php echo $rowq['step'];?>" class="btn btn-block btn-primary" value="<?php echo $lableSave; ?>"></td>
</tr>
<?php
}
}?>
</form>
</table>
If I echo the server variable in the save.php it will not update for the new selected value in the dropdown, but it will work for all the subsequent iterations of the cycle.
Any way to solve this problem or the previous one before i started trying to hammer the code until it worked would be greatly appreciated.
I am learning basic php since September 2015, and I have to create a website with a fully functioning php update code.
What I have right now is a news page with 3 rows that have to be updated in an administration panel with a submit button for every field (date, title and content), so there are 3 forms for 3 news articles.
[http://i.imgur.com/zFhiDR8.png] picture showing the fields.
So everytime a new row gets created (even though there will always be 3), a new form appears in the administration panel.
Now, the problem that I have is that only the last field gets updated.
For example, I type information in all 3 forms, but only the last form gets the updated information shown on the news page when pressing the submit button according to the corresponding form. The updated information is also not getting added in the database.
If I type info in the first content field (inhoud) and press update, it will only check if the last form is updated.
Thinking about it, this would mean that my code only checks if the last id of the database, right?
For testing, I added a new row in the database with an auto-incrementing id of 4. And the result is that only the fourth id is checked for updating.
It also says in my error checker:
"NO RECORDS UPDATED ... ... ... WHERE ID=9"
This is all the information I could think of to put in here.
This is my php code:
<?php
session_start();
if(!isset($_SESSION['gebruiker'])){
header ("Location: admin_login.php");
}
else{
require_once 'db_config.php';
$query = "
SELECT
*
FROM
gip_home_nieuws";
$result = mysql_query($query);
if(!$res = mysql_query($sql))
{
trigger_error(mysql_error().'<br />In query: '.$sql);
}
elseif(mysql_num_rows($res) == 0)
{
echo 'Geen resultaten gevonden';
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pagina's updaten</title>
<link href="admin_page1_css.css" rel="stylesheet" type="text/css">
</style>
</head>
<body>
<div id="container">
<div id="cont_header">
<div id="header">
<div id="header_logo"><img src="images/logo.png" width="200" height="110"></div>
<div id="header_slogan">We secure your future, the easy way</div>
<div id="knop_cont">
<div class="navigatie_knop" id="knop_nieuws">Nieuws</div>
<div class="navigatie_knop" id="knop_overons">Over ons</div>
<div class="navigatie_knop" id="knop_producten">Producten</div>
<div class="navigatie_knop" id="knop_winkel">Winkel</div>
<div class="navigatie_knop" id="knop_contact">Contact</div>
<div class="navigatie_knop" id="knop_uitloggen">Uitloggen</div>
</div>
</div>
</div>
<div id="divider1"></div>
<div id="cont_inhoud">
<div id="contact1">
<div class="contact_titel id="contact1_t>Administratie</div>
<div id="contact1_c">
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<?php
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$nieuws_date = $row['nieuws_date'];
$nieuws_titel = $row['nieuws_titel'];
$nieuws_content = $row['nieuws_content'];
?>
<td width="100"></td>
</tr>
<tr>
<td width="100">id</td>
<td><input class="input1" name="form_id" type="text" value="<?=$id?>"></td>
</tr>
<tr>
<td width="100">Datum</td>
<td><input class="input1" name="form_date" type="text" value="<?=$nieuws_date?>"></td>
</tr>
<tr>
<td width="100">Titel</td>
<td><input class="input2" name="form_titel" type="text" value="<?=$nieuws_titel?>"></td>
</tr>
<tr>
<td width="100">Inhoud</td>
<td><textarea class="input3" name="form_content" type="text"><?=$nieuws_content?></textarea></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<p><input type="submit" name="updaten" value="gegevens updaten"/>
<?php } ?></p>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST[updaten]))
{
$id2 = $_POST['form_id'];
$nieuws_date2 = $_POST['form_date'];
$nieuws_titel2 = $_POST['form_titel'];
$nieuws_content2 = $_POST['form_content'];
$sql = "
UPDATE
gip_home_nieuws
SET
nieuws_date = '".$nieuws_date2."',
nieuws_titel = '".$nieuws_titel2."',
nieuws_content = '".$nieuws_content2."'
WHERE
id=$id2
";
}
if(!$res = mysql_query($sql)) {
trigger_error(mysql_error().'<br />In query: '.$sql);
}
elseif(mysql_affected_rows() == 0) {
echo 'Geen records gegwijzigd. <br />Query: '.$sql;
echo"<a href=\"index.php\">Terug</a";
}
else
{
echo 'Update was succesvol!';
echo"<a href=\"index.php\"<br><br>>Terug</a";
}
?>
</div>
</div>
</div>
<div id="divider2"></div>
<div id="footer">
<div id="cont_footer">
<div id="footer_info">© Belgian Space Industries 2016</div>
</div>
</div>
</div>
<?php } ?>
</body>
</html>
I would really appreciate some help!
EDIT: seeing some posts with comments saying I shouldn't use mysql_* functions, I was teached to use MYSQL_* functions.
So sorry if there is confusion about this, but I can't really do something about this!
If you run this code and fill in each text input what happens when you submit the form?
<?php
var_dump($_POST)
?>
<form method="POST">
<input type="text" name="foo">
<input type="text" name="foo">
<input type="text" name="foo">
<input type="submit">
</form>
As all three of those foo fields above share the same name, when you POST, only one of the foo input's value will be available (it will clobber the other two).
You need to create three separate forms by creating each form inside your loop. Currently you have one form (your form html tags surround the loop).
You want something more akin to this:
<?php for($i=0; $i<3; $i++ ) { ?>
<form method="POST">
<table>
<tr>
<td>
Foo:
</td>
<td>
<input type="text" name="foo">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit">
</td>
</tr>
</table>
</form>
<?php } ?>
Swap out the for for your while.
You should at least escape submitted values within your queries to reduce the chances of sql injection.
$sql = "
UPDATE
foo_table
SET
bar_field = '".mysql_real_escape_string($bar_value)."',
WHERE
baz_field = $baz_value
";
As others have pointed out the mysql functions are deprecated in PHP 5.5.0, and removed in PHP 7.0.0. Mysqli is a similar an alternative extension.
Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>
So i've created an administration page that creates X-number of forms based on how many users we have in our database, each of which have a submit button next to them to submit changes to the dates we have in our DBs. My problem is that when I want to get the value of what gets posted I can't extract exactly what I need from what gets posted. It gets saved into an array called and when I print_r the array I get exactly what I want, which is:
[1] => "whatever date they typed in"
(obviously the 1 changes depending on which item they changed the date of)
I need be able to query my datebase by:
UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number"
I know exactly what I need to do, I'm just not as familiar with SQL as i'd like to be, so any help would be greatly appreciated. Thanks in advance.
Code for reference:
<div class="form-section grid12" id="changedates">
<h1>Change Dates</h1>
<?php
$query = mysql_query("SELECT * FROM users WHERE admin='y'");
?>
<table>
<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td>
<h5><?php echo $row['displayname'];?></h5>
</td>
<td>
<form action="" method="POST">
<input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>">
<input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>">
</td>
<td>
<input type="submit" value="Set Date" name="setdate">
</form>
</td>
<?php
}
?>
</table>
</div>
You could use foreach...
foreach ($_POST[nextupdate] as $rowId => $time)
{
// do db update
}
Edit: Just realised you have more than one input per form.
Why not name each input with an array name:
<input type="text" name="form_data[<?= $row_id ?>][subdate]">
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]">
In PHP:
foreach ($_POST[form_data] as $rowId => $values)
{
$subdate = $values[subdate];
$nextupdate = $values[nextupdate];
// do SQL stuff
}
I have a php page where user can select one category via dropdown list and after clicking 'go' button the relative images for that category will display on page below the dropdown list from database. This works fine. Now I want to display the images on page load for the 1st dropdown list category everytime then after based on user choice. I think my point is clear to all. for example, 1st category in the dropdown is Featured art, on loading the php page images related to Featured art always display on page. After that user can change it by changing the category from dropdown. Help me please.
Here is my complete code :
<body>
<?php
include ('connect-db.php');
?>
<form name="product" method="post" action="">
<table align="right" width="10%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Category</td>
<td>
<select name="category">
<?php
$sql = "SELECT id, art_name FROM category;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
<option value="<?= $row['id']; ?>"><?= $row['art_name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input name="go" type="submit" value="Go" /></td>
</tr>
</table>
</form>
<div align="center">
<ul class="display">
<?php
$id = (int)$_POST['category'];
$sql_search = "SELECT id, categoryid, path FROM list WHERE categoryid = $id";
$search = mysql_query($sql_search);
if (isset($_POST['go'])) {
while ($row = mysql_fetch_assoc($search)) {
?>
<li><img src="<?= $row['path']; ?>" border="0"></li>
<?php }
}
else {
}
?>
</ul>
</div>
</body>
Thanks in advance!:)
You may try to use GET request instead of POST. Using this you may check like:
<?php
$get_id = $_GET['category'];
if($get_id == null){
$get_id = <default-value-say-1>;
}
$id = (int)$get_id;
?>
Also, in the dropdown, set the default / first category selected.
When the user selects another option, redirect to url?category=<selected-id>
This is not a copy-paste solution, please take this as a starting
point and follow the best-practices for development in PHP or any PHP
framework of your choice.
Hope this helps!
Vivek