having this problem for more than three days to fix.
I'm creating a form that lets the user search with some conditions.
Why the values of company and category are not passing to the URL when using the get method?
<form class="main-searchbox-container" role="search" method="get" action="search-results.php">
<div class="form-group" id="main-searchbox">
<input id="target" type="text" class="form-control main-search-input" placeholder="Search..." name="target" value="<?php if (isset($_POST['target'])) echo $_POST['target']; ?>" />
</div>
<div class="selection-container">
<select name="company" class="header-selection" form="main-searchbox">
<option value="company" name="company">Company</option>
</select>
</div>
<div class="selection-container">
<select name="category" class="header-selection" form="main-searchbox">
<?php
// Make the query:
$sql = "select Business_Category from business_categories order by Business_Category ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['Business_Category'].'" name="'.$row['Business_Category'].'">'.$row['Business_Category'].'</option>';
}
} else { echo "----";} // End
?>
</select>
</div>
<input id="submit" class="btn btn-default main-search-button" type="submit" name="submit" value="Search">
</form>
It must be quite straight forward to fix this and with your feedback can fix that easier.
Thank you for your time.
It worked by removing form="main-searchbox" present in both <select> elements (inside of <div class="selection-container">) as there is not a matching id with that name on the <form>, as RiggsFolly said in the comments.
See the working code:
<form class="main-searchbox-container" role="search" method="get" action="search-results.php">
<div class="form-group" id="main-searchbox">
<input id="target" type="text" class="form-control main-search-input" placeholder="Search..." name="target" value="<?php if (isset($_POST['target'])) echo $_POST['target']; ?>" />
</div>
<div class="selection-container">
<select name="company" class="header-selection">
<option value="company" name="company">Company</option>
</select>
</div>
<div class="selection-container">
<select name="category" class="header-selection">
<?php
// Make the query:
$sql = "select Business_Category from business_categories order by Business_Category ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['Business_Category'].'" name="'.$row['Business_Category'].'">'.$row['Business_Category'].'</option>';
}
} else { echo "----";} // End
?>
</select>
</div>
<input id="submit" class="btn btn-default main-search-button" type="submit" name="submit" value="Search">
</form>
Related
I have designed a form and a dropdown that fetches its option from MYSQL Database. But after getting the options the rest of the code like the button and other parts disappeared not showing. The rest of the code works fine every time I comment out the PHP Query.
<form action="" method="POST">
<label><b>Add Student to this User ID</b></label>
<select name="student_id" class="form-control">
<option value="0">--Select Student--</option>
<?php
$query = mysqli_query($con, "SELECT * FROM students ORDER BY first_name, last_name, Other_name ASC");
while ($row = mysqli_fetch_assoc($query)) { ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['first_name'] . ' ' . $row['last_name'] . ' ' . $row['Other_name'] ?></option>;
<?php } ?>
</select>
<br />
<input type="hidden" name="page_id" value="<?php echo $user_id; ?>">
<div class="row">
<div class="col-sm-3 col-sm-offset-4">
<p><input type="submit" class="btn btn-xs btn-success" name="submit" value="Add Student"></p>
</div>
</div>
</form>
There's a semicolon missing after $row['Other_name']
EDIT: My edit button refuse to work.
I'm trying to edit this page, but it seems to be breaking when I click the edit button. it is retrieving data from the database and displaying on faculty.php successfully while if I try to edit it shows a blank page. what is wrong with my code, please.
faculty.php: a separate page where i have my edit button
<td>
<form action="facultyedit.php" method="POST">
<input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>" >
<button type="submit" name="edit_data_btn" class="btn btn-success">EDIT</button>
</form>
</td>
facultyedit page: where i receive the edit button
<?php
if (isset($_POST['edit_data_btn']))
{
$id = $_POST['edit_id'];
$query = " SELECT * FROM register WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row ) {
?>
<form action="" method="POST" >
<input type="text" name="edit_id" value="<?php echo $row['id'] ?>" >
<div class="form-group">
<label>Name:</label>
<input type="text" name="edit_name" value="<?php echo $row['name']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Designation:</label>
<input type="text" name="edit_designation" value="<?php echo $row['design']; ?>" class="form-control" >
</div>
<div>
<label>Description:</label>
<input type="text" name="edit_description" value="<?php echo $row['descrip']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Upload Image:</label>
<input type="file" name="edit_faculty_image" id="faculty_image" value="<?php echo $row['image']; ?>" class="form-control" >
</div>
<div class="">
Cancel
<button type="update" name="update_aboutusbtn" class="btn btn-primary">Update</button>
</div>
</form>
<?php
}
}
?
I have fixed the issue:
The problem was that I was selecting a register table from the database instead of the faculty table.
I have a page which is showing around 3000 entries. what i am trying to do is when i click on filter button the data of only that entry will change. but currently, when i click on filter button the data of all the entries are changing.
here is my code:-
<?php
include("config.php");
$sql="SELECT * FROM inventory_details where status ='0' limit 0,100";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_assoc($query)){
$id = $row['id'];
$inventorybackground = $row['inventorybackground'];
$inventorycolor = $row['inventorycolor'];
$firm_name = $row['firm_name'];
$position = $row['position'];
$city = $row['city'];
$catagory_name = $row['catagory_name'];
$supplier = $row['supplier'];
if (isset($_POST['update'])){
$position=$_POST['position'];
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
$run_update=mysqli_query($conn, $update_query);
}
?>
<div class="container">
<div class="row" style="background-color: <?php echo $inventorybackground; ?> !important; color: <?php echo $inventorycolor; ?>; padding-top: 10px;">
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="id" name="id" value="<?php echo $id;?>" disabled>
</div>
<div class="col-md-3 inventory-data">
<input type="text" class="form-control" id="firm_name" name="firm_name" value="<?php echo $firm_name;?>" disabled>
</div>
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="position" name="position" value="<?php echo $position;?>">
</div>
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="city" name="city" value="<?php echo $city;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<input type="text" class="form-control" id="catagory_name" name="catagory_name" value="<?php echo $catagory_name;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<input type="text" class="form-control" id="supplier" name="supplier" value="<?php echo $supplier;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<button type="submit" name="update" value="update" class="btn btn-primary">Filter</button>
</div>
</form>
</div>
</div>
<?php } ?>
Currently, i am only trying to change the position and I think, this problem is because of loop.
Please help me out, Thanks in advance.
When you click the button, the entire list is updated on your loop to list the rows.
Add this code at your page to detect the form POST to do the update:
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// perform update here
$id = $_POST['id'];
$position = $_POST['position'];
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
$run_update=mysqli_query($conn, $update_query);
}
Edit:
Your input can't be disabled, try to use input hidden to pass id to POST:
<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
If you need to show the id on input text, change the name to different name istead of id, because it will be used from input hidden
while($row=mysqli_fetch_assoc($query)){
This has ending bracket on the end. So you are basically looping through all the data and updating it all. Do the update query outside of loop.
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
You have SQL injection vulnerability here, please use prepared statements.
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
If id is integer, you dont have to put quotation marks around it
I need to display only the id row of the database on a textbox, my problem was the data is displaying all id's rows and columns.
I was fetching all data in the database, but what I need is only the id on the database.
Here is my code in forms, input text fields and submit.
<form action="" method="POST">
<div class="row col-md-4">
<label>Amount</label>
<input type="text" name="id" class="form-control validate">
<br>
<input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input><br>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection, 'qrproject');
if(isset($_POST['search']))
{
$id = $_POST['id'];
$query = "SELECT * FROM scratch_cards WHERE amount='$id' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="" method="POST">
<input type="text" name="code" value="<?php echo $row['code'] ?>" class="form-control validate" id="mapo">
<input type="text" name="pin" value="<?php echo $row['pin'] ?>" class="form-control validate" id="mact">
<input type="text" name="status" value="<?php echo $row['status'] ?>" class="validate form-control" id="soluong">
<input type="date" name="card_expiration" value="<?php echo $row['card_expiration'] ?>" class="validate form-control" id="cardex">
<input type="number" name="card_validity" value="<?php echo $row['card_validity'] ?>" class="validate form-control" id="cardval">
</form>
<?php
}
}
?>
</form>
From your comments, it seems what you want to do is output a single, random row which matches the $_POST['id'] value against the amount column in your table. You can do this by changing your query to this:
SELECT * FROM scratch_cards WHERE amount='$id' ORDER BY RAND() LIMIT 1
You can also change
while($row = mysqli_fetch_array($query_run))
to
if($row = mysqli_fetch_array($query_run))
although with the change to the query to limit the output to one row this is no longer absolutely necessary.
As has been pointed out, you are vulnerable to SQL injection, you should read this question to see how to resolve that.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
Im trying to nest HTML code in php while loop. It gives error with that block. "Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\cms\admin\categories.php on line 119"
<form action="categories.php" method="post">
<div class="form-group">
<label for="cat-title">Update</label>
<?php //EDIT & UPDATE //
if (isset($_GET['edit'])){
$edit_row_title = $_GET['edit'];
$query = "SELECT * FROM category WHERE cat_id = {$edit_row_title}";
$edit_query = mysqli_query($connect,$query);
while($row = mysqli_fetch_assoc($edit_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
?>
<input class="form-control" name="cat_title" type="text" value="<?php if(isset($cat_title)){echo $cat_title;} ?>">
<?php } ?> }
<input type="text" name="cat-title" class="form-control" >
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Edit Category" >
</div>
</form>
But when i put curly brackets before html code it works. Expect while loop doesnt work properly in this way.
<form action="categories.php" method="post">
<div class="form-group">
<label for="cat-title">Update</label>
<?php //EDIT & UPDATE //
if (isset($_GET['edit'])){
$edit_row_title = $_GET['edit'];
$query = "SELECT * FROM category WHERE cat_id = {$edit_row_title}";
$edit_query = mysqli_query($connect,$query);
while($row = mysqli_fetch_assoc($edit_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
} ?>
<input class="form-control" name="cat_title" type="text" value="<?php if(isset($cat_title)){echo $cat_title;} ?>">
<?php } ?>
<input type="text" name="cat-title" class="form-control" >
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Edit Category" >
</div>
</form>
Change
<?php } ?> }
To
<?php } }?>
Updated Code
<form action="categories.php" method="post">
<div class="form-group">
<label for="cat-title">Update</label>
<?php //EDIT & UPDATE //
if (isset($_GET['edit'])){
$edit_row_title = $_GET['edit'];
$query = "SELECT * FROM category WHERE cat_id = {$edit_row_title}";
$edit_query = mysqli_query($connect,$query);
while($row = mysqli_fetch_assoc($edit_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
?>
<input class="form-control" name="cat_title" type="text" value="<?php if(isset($cat_title)){echo $cat_title;} ?>">
<?php } }?> //Changes
<input type="text" name="cat-title" class="form-control" >
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Edit Category" >
</div>
</form>
while($row = mysqli_fetch_assoc($edit_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
?>
<input class="form-control" name="cat_title" type="text" value="<?php if(isset($cat_title)){echo $cat_title;} ?>">
<?php } ?> }
In the last line, you add a closing curly bracket after the closing PHP tag.
I assume it's there to close the if but PHP won't interpret it. Therefore, at the end of the file, PHP will still be expecting that block to be closed, causing a parse error.
Please Update below code
<form action="categories.php" method="post">
<div class="form-group">
<label for="cat-title">Update</label>
<?php //EDIT & UPDATE //
if (isset($_GET['edit'])){
$edit_row_title = $_GET['edit'];
$query = "SELECT * FROM category WHERE cat_id = {$edit_row_title}";
$edit_query = mysqli_query($connect,$query);
while($row = mysqli_fetch_assoc($edit_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
?>
<input class="form-control" name="cat_title" type="text" value="<?php if(isset($cat_title)){echo $cat_title;} ?>">
<?php } }?> //closing if loop and while loop
<input type="text" name="cat-title" class="form-control" >
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Edit Category" >
</div>
</form>
You have to Change <?php } ?> } into <?php } }?> and your problem will solve.
Following is updated code according to your given code snippet :
<form action="categories.php" method="post">
<div class="form-group">
<label for="cat-title">Update</label>
<?php //EDIT & UPDATE //
if (isset($_GET['edit'])){
$edit_row_title = $_GET['edit'];
$query = "SELECT * FROM category WHERE cat_id = {$edit_row_title}";
$edit_query = mysqli_query($connect,$query);
while($row = mysqli_fetch_assoc($edit_query)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
?>
<input class="form-control" name="cat_title" type="text" value="<?php if(isset($cat_title)){echo $cat_title;} ?>">
<?php } }?> //Changes => This changes requred on your code.
<input type="text" name="cat-title" class="form-control" >
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Edit Category" >
</div>
</form>