I need to retrieve the hobbies name from mysql and display it in check boxes. I done the below given code. But it displays just check box and not any hobby names. Please help.
$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result))
{
$hobby=$r["hobby_name"];?>
<input type='checkbox' name='check[]' value='$hobby'>
}
You need to add simple text $hobby next to every checkbox.
Corrected code:
$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result)) {
$hobby=$r["hobby_name"];
?>
<input type='checkbox' name='check[]' value='<?php echo $hobby;?>'> <?php echo $hobby;?>
<?php
}
Checkbox input itself does not display any text. You need to show both checkbox and text separately like this:
<input type='checkbox' name='check[]' value='$hobby'><label>$hobby</label>
Try this:
<?php
$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result))
{
$hobby=$r["hobby_name"];
echo "<input type='checkbox' name='check[]' value='".$hobby."'><label>".$hobby."</label>";
}
?>
Related
Hi I have tried to select mysqli results from a table row for phone numbers but it produces many textboxes instead of only one. Please I need your help, I'll be grateful. My code is as below:
<form method="post">
<?php
$connect = mysqli_connect("localhost", "root", "", "hrm");
$query = "SELECT * FROM tbl_employee";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
?>
<input type='text' name='phone' class='form-control' placeholder='Enter recipients' value='<?php echo $row['phone']; ?>' />
<?php
}
?>
</form>
try like this in php alone i used while inside value attr.
printing ',' will produce extra one in the last which will help to type new number
<form method="post">
<?php
$connect = mysqli_connect("localhost", "root", "", "hrm");
$query = "SELECT * FROM tbl_employee";
$result = mysqli_query($connect, $query); ?>
<input type='text' name='phone' class='form-control' placeholder='Enter recipients'
value='<?php
while($row = mysqli_fetch_array($result)){
echo $row['phone'].",";
}
?>'
/>
</form>
I have a simple table with staff names stored in the column f_operator_name.
I have a drop down menu in a php form with these staff names available for selection. Here is a snippet of the relevant the code:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
When someone selects the staff name I want it to be stored in a variable. I'm testing the submit form at the bottom which is intended to print out the name that has been selected ( in the variable operator), but it's not printing anything out. Can anyone see any issues?
EDIT *** Here's the updated code after some advice from Barmar with the variable information also, for some reason the echo statement still isn't working:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
You can't put <select> inside <h2> and then put the <option>s and </select> outside it. HTML elements have to be nested properly, and <option> has to be inside <select>.
Change it to:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
And the index in $_POST has to match the name of the <select>, so $_POST['operator'] should be $_POST['f_operator_id'].
I have a table in MySQL where there is a row with this data.
id = 187
friendly name = i don't like mustard
filetype = exe
This first block of code below works perfectly, and echos text i don't like mustard into an HTML form. Similarly, if I change $row['friendlyname'] to $row['filetype'], text exe is echoed. All good, no issues yet.
<?php
$con = mysqli_connect('domain','user','pass','db');
$sql = "select * from installers where id=187";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$con->close();
?>
<input type='text' value='<?php echo $friendlyname; ?>'>
The problem I'm having is if I try to echo both $row['friendlyname'] and $row['filetype'], only the variable that is listed first will be echoed. For example, in the below code, $row['friendlyname'] is listed before $row['filetype']. In this example, only $row['friendlyname'] (i don't like mustard) will be echoed. Similarly, if $row['filetype'] is listed before $row['friendlyname'], then only $row['filetype'] (exe) is echoed, and the second other HTML input form is empty.
<?php
$con = mysqli_connect('domain','user','pass','db');
$sql = "select * from installers where id=187";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$filetype= htmlspecialchars(" ".$row['filetype']." ",ENT_QUOTES);
$con->close();
?>
<input type='text' value='<?php echo $friendlyname; ?>'>
<input type='text' value='<?php echo $filetype; ?>'>
Note 1: It doesn't matter the order of the input type forms. I ruled that out as the issue.
Note 2: If I were to replace $row['friendlyname'] and $row['filetype'] with the text I'm trying to echo, then it work (the below code). So, this definitely appears to be something with these $row variables.
<?php
$con = mysqli_connect('domain','user','pass','db');
$sql = "select * from installers where id=187";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
$friendlyname = i don't like mustard;
$filetype= exe;
$con->close();
?>
<input type='text' value='<?php echo $friendlyname; ?>'>
<input type='text' value='<?php echo $filetype; ?>'>
You have not added brackets into while loop so only first record is populated.
This block:
while($row=mysqli_fetch_array($result))
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$filetype= htmlspecialchars(" ".$row['filetype']." ",ENT_QUOTES);
Should be:
while($row=mysqli_fetch_array($result)){
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$filetype= htmlspecialchars(" ".$row['filetype']." ",ENT_QUOTES);
}
I am trying to create a form which allows the user to search for an event using the Venue and category fields which are scripted as dropdown boxes and the Price and finally by event title, as shown via the code if a keyword is entered which matches the fields on the database it should output all the related information for that event if any matches have been made on either search fields, but it seems to output every single event from the database no matter what I type in the search field.
DATABASE: http://i.imgur.com/d4uoXtE.jpg
HTML FORM
<form name="searchform" action ="PHP/searchfunction.php" method = "post" >
<h2>Event Search:</h2>
Use the Check Boxes to indicate which fields you watch to search with
<br /><br />
<h2>Search by Venue:</h2>
<?php
echo "<select name = 'venueName'>";
$queryresult2 = mysql_query($sql2) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult2)) {
echo "\n";
$venueID = $row['venueID'];
$venueName = $row['venueName'];
echo "<option value = '$venueID'";
echo ">$venueName</option>";
}# when the option selected matches the queryresult it will echo this
echo "</select>";
mysql_free_result($queryresult2);
mysql_close($conn);
?>
<input type="checkbox" name="S_venueName">
<br /><br />
<h2>Search by Category:</h2>
<?php
include 'PHP/database_conn.php';
$sql3 ="SELECT catID, catDesc
FROM te_category";
echo "<select name = 'catdesc'>";
$queryresult3 = mysql_query($sql3) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult3)) {
echo "\n";
$catID = $row['catID'];
$catDesc = $row['catDesc'];
echo "<option value = '$catID'";
echo ">$catDesc </option>";
}
echo "</select>";
mysql_free_result($queryresult3);
mysql_close($conn);
?>
<input type="checkbox" name="S_catDes">
<br /><br />
<h2>Search By Price</h2>
<input type="text" name="S_price" />
<input type="checkbox" name="S_CheckPrice">
<br /><br />
<h2>Search By Event title</h2>
<input type="text" name="S_EventT" />
<input type="checkbox" name="S_EventTitle">
<br /><br />
<input name="update" type="submit" id="update" value="Search">
searchfunction.php file
<?php
$count = 0;
include 'database_conn.php';
$venuename = $_REQUEST['venueName']; //this is an integer
$catdesc = $_REQUEST['catdesc']; //this is a string
$Price = $_REQUEST['S_price'];
$EventT = $_REQUEST['S_EventT'];
$sql = "select * FROM te_events WHERE venueID LIKE '%$venuename%' OR catID LIKE '%$catdesc%' OR eventPrice LIKE '%Price%' OR eventTitle LIKE '%$EventT%'";
$queryresult = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult))
{
echo $row['eventTitle'];
echo $row['eventDescription'];
echo $row['venueID'];
echo $row['catID'];
echo $row['eventStartDate'];
echo $row['eventEndDate'];
echo $row['eventPrice'];
}
mysql_free_result($queryresult);
mysql_close($conn);
?>
The query should be
$sql = "select * FROM te_events
WHERE (venueID LIKE '%$venuename%'
OR catID LIKE '%$catdesc%'
OR eventPrice LIKE '%$Price%'
OR eventTitle LIKE '%$EventT%')
;
To get values from the form submitted with method POST we use $_POST to access form data and not $_REQUEST:
$venuename = $_POST['venueName']; //this is an integer
$catdesc = $_POST['catdesc']; //this is a string
$Price = $_POST['S_price'];
$EventT = $_POST['S_EventT'];
That was about your problem - now some important notes:
Do not use mysql extension as it's deprecated. Read this official documentation.
Use mysqli and prevent SQL injections by using prepared queries and parameters like in official documentation again.
Since you are matching on any fields surrounded by wildcards, if any of the fields are blank, then the MySQL query will match all rows.
Also, you need to prevent MySQL injection. Otherwise, your MySQL table will eventually be hacked.
By the way, the code eventPrice LIKE '%Price%' is invalid and is missing a dollar sign.
Lastly, the mysql extension has been deprecated. I would recommend using mysqli instead as it is fairly similar.
echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name='$product_naam' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo "check: ", $check;
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
I want to have a list of product in my webshop administrator page. Every product has a checkbox. I want to be able to delete all of the product of the checkboxes are checked. But I don't know how.. The above is what I have so far. The added picture is how it looks on the page. But the page is a list of product selected from the database with a foreach loop as can be seen in the code. The checkbox also is in the loop. I don't know how to assign every product which is check to a variable and then delete them from the database. Can anyone help me?
Name all the checkboxes a same, like delete[] , and and put the name of product in the value of each checkbox.
Example :
<form action="..." method='post' >
user1<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user2<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user3<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user4<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
<input type='submit' value='delete' />
</form>
Delete query :
<?php
if(isset($_POST['delete'])){
$ids = $_POST['delete'];
$sql = "DELETE FROM `producten` WHERE product_naam
IN('".implode("','", $ids)."')";
//execute query
}
?>
You're looking for $_POST['checkbox'] but that's not what you have in your form. Name the checkboxes all checkbox[] and use $product_naam as the value.
<input type='checkbox' name='checkbox[]' value='$product_naam'>
Now you can loop over it and delete with your foreach loop.
you should not name the checkbox to the product name, just call it delete or something. Then you can use the foreach method to delete them from the database, like this:
<?php
echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name=delete' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(isset($_POST['delete'])) {
foreach($_POST['delete'] as $delete){ {
$sql = "DELETE FROM producten WHERE product_naam= $delete";
$query = $dbh->prepare( $sql );
$result = $query->execute();
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
Also, it seems to me you're mistaking "value" with "name", read up on it. And it would be good to read something about PDO, mysql isn't safe. http://www.php.net/manual/en/book.pdo.php