Trouble controlling the loops - php

If pressed delet , All images are deleted ,I do not want that.
I just want to delete one image.
$get_img = "SELECT * FROM img";
$run_img = mysqli_query($db,$get_img);
while ($row_img= mysqli_fetch_array($run_img)){
if (isset($_POST['delete'])){
$delete= "DELETE FROM img WHERE id = {$row_img['id']} ";
$query = mysqli_query ($db , $delete);
}
echo '
<div style="width:200px; height:200px; border: 5px solid red; margin:10px; float: right;">
<img src="images/'.$row_pro['c_img'].'" width="200" height="200" /><br />
<form action="get.php" method="POST">
<input type="submit" name="delet" value="delet" />
</form>
</div>
';
}

Just a little explain, we start talking Arabic because Mostafa is not good in english, so i wrote a little explain in English and Arabic below to help to help both languages users.
while($row_img = mysqli_fetch_array($run_img)){
//explain [1]
echo '
<div style="width:200px; height:200px; border: 5px solid red; margin:10px; float: right;">
<img src="images/'.$row_img['c_img'].'" width="200" height="200" /><br />
<form action="?" method="POST">
<input name="id" value="'.$row_img['id'].'" hidden><!-- //explain [2] -->
<input type="submit" name="delet" value="delet" />
</form>
</div>
';
}
if(isset($_POST['delet'])){
$id = intval($_POST['id']);//explain [3]
$delete= "DELETE FROM img WHERE id = '$id'";
$query = mysqli_query($db, $delete);
if($query){
echo 'Deleted Successfully!';
}
}
explain [1] Remove the query from inside while its bad to put it there
explain [2] Then i made a hidden input called id to get the id from it
explain [3] Make the hidden sent id to $id variable and intval it for more security, you can read about it
.
explain [1] اول حاجة شلت الكويري إلي انت كاتبها جوا ال while
explain [2] بعدين سويت زر مخفي مع كل صورة إسمه أي دي يعطينا الاي دي الخاص فيها
explain [3] بعدين بستقبل الاي دي إلى بالزر المخفي على متغير جديد وبسويله شوية حماية.
I hope this helps you :)

Grab your isset and delete query out of while
Something like that. I did not checked all details but maybe it's good for inspiration.
$get_img = "SELECT * FROM img";
$run_img = mysqli_query($db, $get_img);
while ($row_img = mysqli_fetch_array($run_img))
{
echo '<div>
<img src="images/' . $row_pro['c_img'] . '" /><br />
</div>';
}
echo '<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" name="delete" value="delete" />
</form>';
if (isset($_POST['delete']))
{
$delete= "DELETE FROM img WHERE id = :id", ['id'=>$row_pro['id']];
$query = mysqli_query($db, $delete);
}
Hope you'll figured out.

Related

How to make my PHP code more efficient for a search database?

Main objective for this post is to open up a can of worms. I would like to get some advise on what is the best way/ most efficient way to search a phpmyadmin database.
I am just starting to learn PHP. Main goal is to be able to write my own search databases as that has always interested me. I played around with some code to build a basic search database and have it working now.
Below is the code I used. I would guess as it is my first attempt at this I am going about it the wrong way. Assuming this was a large database, how could I make this more efficient? Am I going about it the wrong way using PHP? Is there a better way to do this?
The goal is to have searchable database where the user has multiple options to query to help refine the results.
<html>
<head>
<title> test Search </title>
<style type="text/css">
table {
background-color: #ffffff;
}
th {
width: 200px;
text-align: left;
}
</style>
</head>
<body>
<h1> test Search</h1>
<form method="post" action="index.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<label>State: <input type="text" name="criteria5" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')
LIMIT 0,10";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>

PHP Dropdown Menu from DB column values

WHAT I STARTED WITH
My php
<?php
mysql_select_db("$db") or die(mysql_error());
$sql = "SELECT kpi FROM pin_kpi_types";
$query = mysql_query($sql);
echo '<select name="KPI" style="width: 400px">';
while ($row = mysql_fetch_assoc($query)) {
echo '<option>'.$row['KPI'].'</option>';
}
echo '</select>';
?>
My current label/input method that requires me to manually type.
What I am trying to do is pull a query from a table to select the only available KPI types.
This is working now.
<p>
<label for="KPIType" id="preinput">Choose KPI Type: </label>
<input type="text" name="kpi_type" required placeholder="Lead" id="inputid" />
</p>
WHERE I WAS AT PREVIOUS
Edit; Unfortunately the answer posted by Lucky didn't quiet help me, but it steered me in the right way.
I am now have duplicate fields? It isn't posting the value into the DB. either.
Here is a screenshot...
http://s30.postimg.org/o1c2l9yr5/Untitled.png
Can someone steer me in right direction of where I went wrong? :)
Thank you in advance for any help.
<?php
include('db.php');
$select=mysql_query("SELECT * FROM pin_kpi_types");
$i=1;
while( $userrow=mysql_fetch_array($select) )
{
$kpi_id =$userrow['KPI_ID'];
$kpi =$userrow['KPI'];
?>
<style>
#CreateStatusTypeFORM label {display: inline-block;width: 10em; text-align: right;padding-right: 0.5em;}
</style>
<div id="CreateStatusTypeFORM">
<form action="insert.php" method="post" name="insertform">
<p>
<div align='center'><label for="StatusColor" id="preinput">Choose A Color: </label>
<ui-colorpicker ng-model="targetColor"></ui-colorpicker></div>
<input type="hidden" name="pinstatus_color" value="{{targetColor}}" id="inputid" />
</p>
<p>
<label for="StatusName" id="preinput">Set Status Name: </label>
<input type="text" name="pinstatus_type" required placeholder="Come Back" id="inputid"/>
</p>
<p>
<label for="StatusName" id="preinput">Available KPI Types: </label>
<select name="<?php echo $kpi; ?>" style="width: 237px">
<option name="KPI" required id="inputid" value="<?php echo $kpi; ?>"><?php echo $kpi; ?></option>
</select>
</p>
<?php } ?>
<input type="submit" name="send" value="Submit" id="inputid1" />
</p>
</form>
WHERE I AM AT NOW
This is where I am at. I now can get the dropdown correct without duping it over and over. However, now I cannot get the value to post the actual selected KPI to the DB. everything else submits data to the db, but KPI_type option.
<style>
.StatusForm {padding-left:75px;}
.CreateStatusTypeFORM {padding:25px;border-style:solid; border-color:solid black; width: 500px;background-color:#C4C4C4;}
#CreateStatusTypeFORM label {display: inline-block;width: 100em;}
#CreateStatusTypeFORM input {border-color:solid black;}
#CreateStatusTypeFORM input[type=text] {padding:5px; border:1px solid #666; -webkit-border-radius: 5px; border-radius: 5px;}
</style>
<div class='StatusForm'>
<div class="CreateStatusTypeFORM">
<H3> ADD New Status </H3>
<form action="insert.php" method="post" name="insertform">
<p>
<label for="StatusColor" id="preinput">Choose A Color: </label>
<ui-colorpicker ng-model="targetColor"></ui-colorpicker>
<input type="hidden" name="pinstatus_color" value="{{targetColor}}" id="inputid" required placeholder=""/>
</p>
<p>
<label for="StatusName" id="preinput">Set Status Name: </label>
<input type="text" name="pinstatus_type" required placeholder="" id="inputid"/>
</p>
<p>
<label for="KPI" id="preinput">Available KPI Types: </label>
<select>
<?php
include('db.php');
$select2=mysql_query("SELECT * FROM pin_kpi_types ORDER BY KPI_ID DESC");
while( $userrow=mysql_fetch_array($select2) )
{
$kpi =$userrow['KPI'];
$kpi_id =$userrow['KPI_ID'];
?>
<option name="kpi_type" required id="inputid" value="<?php echo $kpi; ?>"><?php echo $kpi; ?></option><?php } ?>
</select>
</p>
<input type="submit" name="send" value="Submit" id="inputid1" />
</p>
</form>
</div>
</div>
This is the insert function.
<?php
ob_start();
include("db.php");
if(isset($_POST['send'])!="")
{
$pinstatus_type =mysql_real_escape_string($_POST['pinstatus_type']);
$kpi_type =mysql_real_escape_string($_POST['kpi_type']);
$pinstatus_color =mysql_real_escape_string($_POST['pinstatus_color']);
$update =mysql_query("
INSERT INTO pin_status_types(
pinstatus_type,
kpi_type,
pinstatus_color,
created
)
VALUES(
'$pinstatus_type',
'$kpi_type',
'$pinstatus_color',
now()
)
");
if($update)
{
$msg="Successfully Updated!!";
echo "<script type='text/javascript'>alert('$msg');</script>";
header('Location:index.php');
}
else
{
$errormsg="Something went wrong, Try again";
echo "<script type='text/javascript'>alert('$errormsg');</script>";
}
}
ob_end_flush();
?>
mysql_select_db("$db") or die(mysql_error());
$sql = "SELECT kpi FROM pin_kpi_types";
$query = mysql_query($sql);
echo '<label for="KPIType" id="preinput">Choose KPI Type: </label>';
echo '<select name="KPI" style="width: 400px" required id="inputid">';
echo '<option value=''>Lead</option>';
while ($row = mysql_fetch_assoc($query)) {
echo '<option value='.$row['KPI'].'>'.$row['KPI'].'</option>';
}
echo '</select>';
<?php
include 'config.php';
$sql_states=mysqli_query($conn,"SELECT * FROM master_states");
?>
<section Name="state_name">
<option value="">section1</option>
<option value="">section2</option>
</section>

Radio button and search text box in php

I have my input form:
<form style="font-size: 10pt; font-family: 'Courier New'; color:black;font-weight:600;margin-left:15px;line-height:25px" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="radio" id="a" name="search" value="EntryId" checked>EntryId <font color="blue">(get Vocab information)</font>
<br />
<input type="radio" id="b" name="search" value="EntryId1">EntryId <font color="blue">(get disease name, protein name)</font>
<br />
<input type="radio" id="c" name="search" value="UniprotId">UniprotId <font color="blue">(get Gene name, Dna Seq)</font>
<br />
<input type="radio" id="d" name="search" value="Genename">Genename <font color="blue">(get Gene information)</font>
<br />
<input type="radio" id="e" name="search" value="EntryId3">EntryId <font color="blue">(get HTML, PubMed information)</font>
<br /><br /><br />
<input style="height:30px; width: 300px; border:black 1px solid" type="text" name="Search" >
<input style="height:30px" type="submit" value="Go">
</form>
In this form I am using six radio buttons and each one is representing a stored procedure which is stored in MySQL. If I select a radio button and enter a value in text box (which will be the parameter for my selected procedure) I want output for that procedure.
PHP code:
<?php
$id='';
$query='';
if (isset($_POST['Search']))
{
$id=$_POST['Search'];
}
echo "<table>";
//connect to database
$connection = mysqli_connect("localhost", "root", "", "mohammad_prostatecancer");
//run the store proc
if (isset($_POST['EntryId1']))
{
$query= "call DiseaseVocab(".'"'.$id.'")';
}
if (isset($_POST['EntryId2']))
{
$query= "call DiseaseProtein(".'"'.$id.'")';
}
if (isset($_POST['UniprotId']))
{
$query= "call getGene(".'"'.$id.'")';
}
if (isset($_POST['Genename']))
{
$query= "call Getname(".'"'.$id.'")';
}
if (isset($_POST['EntryId3']))
{
$query= "call Getdata(".'"'.$id.'")';
}
$result = mysqli_query($connection, $query) or die("Query fail: " . mysqli_error());
//loop the result set
echo "<tbody>";
// point to the beginning of the array
$check = mysqli_data_seek($result, 0);
?>
<tr>
<td><b>EntryId</b></td>
<td><b>Vocabid</b></td>
<td><b>VocabSourceName</b></td>
</tr>
<?php
while ($rownew = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach($rownew as $k => $v)
{
echo "<td>".$v."</td>";
}
echo "</tr>"."<br>";
}
echo "</tbody></table>";
?>
I am not getting any result. I get these warnings:
Warning: mysqli_query(): Empty query
Warning: mysqli_error() expects exactly 1 parameter, 0 given
You need the connection in the error, do it like this and then you can see the error.
mysqli_error($connection)
query is empty because you are checking
`if (isset($_POST['EntryId1']))` instead of `if (isset($_POST['search']) && $_POST['search']=='EntryId1')`
and switch case would be b`enter code here`etter in this scenario.

Need assistance with disabling checkboxes

I currently have a form as you will see below. For simplicity sake the form is condensed to the checkboxes that I'm working with. What I want to have happen is for a user to optionally click one or the other. Depending on which one is clicked, the other one should be disabled. I noticed that user "dinah" posted a similar question before and that this js fiddle was provided http://jsfiddle.net/A5TGf/19/ . However, for some reason when I applied my form id and switched the blacklist names around it wasn't working with my code. Any suggestions? Thanks
HTML/PHP
<form name="insertTicket" id="insertTicket" action="index.php" method="POST" >
<div id="ticket_hidden" style="text-align: center; clear:both;">
<div id="visible_div" style="float: left;">
<input type="checkbox" name="escalated" id ="escalated" value="Yes" onclick="doInput(this);" tabindex="18">Escalate
</div>
<div id="hidden_div" style="float: left; display:none;">
<?php
$dept = $_SESSION['dept_id'];
$get_email = mysql_query("Select email_name, dept_id, copy_user from emails where escalated = 'yes'");
while(($email = mysql_fetch_assoc($get_email)))
{
$department = explode(",", $email['dept_id']);
if(in_array($dept, $department, TRUE))
{
echo '
<input type="checkbox" name="escalated_to[]" id="escalated_to[]" value="'.$email['email_name'].'" />'.$email['email_name'].'
';
if($email['copy_user'] == 'YES')
{
echo '
<input type="text" name="emails[]" style="width: 175px; height: 20px;" placeholder="To:">
';
}
}
}
?>
</div>
<div id="visible_divX" style="float: left;">
<input type="checkbox" name="email" id="email" value="Yes" onclick="doInputs(this);" tabindex="20">Send Email </div></td></tr>
<div id="hidden_divX" style="float: left; display:none; ">
<?php
$dept = $_SESSION['dept_id'];
$get_email = mysql_query("Select email_name, dept_id, copy_user from emails where escalated != 'yes'");
while(($email = mysql_fetch_assoc($get_email)))
{
$department = explode(",", $email['dept_id']);
if(in_array($dept, $department, TRUE))
{
echo '
<input type="checkbox" name="emailed_to[]" id="emailed_to[]" value="'.$email['email_name'].'" />'.$email['email_name'].'
';
if($email['copy_user'] == 'YES')
{
echo '
<input type="text" name="emails[]" style="width: 175px; height: 20px;" placeholder="To:">
';
}
}
}
?>
</div>
</form>
If you need to disable a checkbox by Javascript, you just need one line:
document.getElementById("foo").setAttribute("disabled","disabled")
and to enable it again,
document.getElementById("foo").removeAttribute("disabled")
I don't think you need to use jQuery for such task...
Good luck!

How to make search results appear on another page?

I have a big search on my home-page and when the user types in the text fields and clicks submit I want the results from my database to appear on another site in this case 'searchresults.php.' In this case 'really.html' is my homepage.
Here is my code:
What am I doing wrong?
Really.html
<center>
<form action="searchresults.php" method="post">
<input type="text" size="35" value="Job Title e.g. Assistant Manager"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Job Title e.g. Assistant Manager'){this.value =
'';this.style.color='#000'}" />
<input type="text" size="35" value="Location e.g. Manchester"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Location e.g. Manchester'){this.value =
'';this.style.color='#000'}" />
<input type="image" src="but.tiff" alt="Submit" width="60">
</form>
Searchresults.php
<html>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0) {
//all of your php code for the search
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '$search' AND
location LIKE '$searchterm'";
$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) {
echo "<br>$row[0]<br>";
echo "$row[1]<br>";
echo "$row[2]<br>";
}
}
}
?>
</body>
</html>
Thanks!
You should add attr name to your input.
For example,
<input type="text" name="search" ... />
<input type="text" name="searchterm" ... />
Also don't forget about escaping input data using mysqL_escape_string function
The query is probably returning 0 results. Instead of
if ($result) {
Try
if (mysql_num_rows($result) >= 1) {
And in your query try...
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'";
This will be less strict and return a better result set.

Categories