I want to show a list of all entries where the column "approved" is "no" and then place a button next to it that when clicked will change the "approved" to a "yes". I ran this through code checker and fixed a few issues with brackets and such. It now tells me there are no errors so something else is just not working with this. It is probably something small that I am missing (I hope). Can anyone help me find/understand what part of this is incorrect... and/or if there is a better way to achieve what I'm wanting?
<?php
//select the database to write to
$unapprovedsires = mysql_query("SELECT * FROM nominatedsires WHERE approved = 'no'");
//While loop to cycle through the rows
while($row = mysql_fetch_assoc($unapprovedsires)){
$sirename = $row['sirename'];
echo $sirename;}
?>
<ul class="admin-fields">
<?php
foreach($row as $field){
if(empty($field)){
echo "....";
}
print '<li>' .$field.' </li>';
}//End For Each Loop
//print $sirename;
?>
</ul>
<p>
<?php
if(isset($_POST['approve'])){
mysql_query("UPDATE nominatedsires SET approved = 'yes' WHERE sirename = '.$sirename.'") or die ("Something went wrong");
}
?>
<ul>
<li>
<form method="post">
<input type="hidden" name="sirename" value="$sirename" />
<button name="approve" id="approve" type="submit">Approve Sire</button>
</form>
</li>
</ul>
As I mentioned as a comment, you need to wrap $sirename in PHP tags with an echo statement. You are also not passing $_POST['sirename'] into your script. It otherwise defaults to the original $sirename from your mysql_fetch_assoc().
Warning: the way you have your script set up, you're vulnerable to injection attacks. This is just an example to show you how to pass in variables. See: How can I prevent SQL injection in PHP?
<?php
//select the database to write to
$unapprovedsires = mysql_query("SELECT * FROM nominatedsires WHERE approved = 'no'");
//While loop to cycle through the rows
while($row = mysql_fetch_assoc($unapprovedsires)){
$sirename = $row['sirename'];
echo $sirename;}
?>
<ul class="admin-fields">
<?php
while($row = mysql_fetch_assoc($unapprovedsires)){
if(empty($row['sirename']))
echo "....";
else
print '<li>' .$row['sirename'].' </li>';
}
//print $sirename;
?>
</ul>
<p>
<?php
if(isset($_POST['approve'])){
$sirename = mysql_real_escape_string($_POST['sirename']);
mysql_query("UPDATE nominatedsires SET approved = 'yes' WHERE sirename = '$sirename'") or die ("Something went wrong");
}
?>
<ul>
<li>
<form method="post" method="<?php echo $_SERVER[PHP_SELF]; ?>">
<input type="hidden" name="sirename" value="<?php echo $sirename; ?>" />
<input type="submit" name="approve" id="approve" value="Approve Sire" />
</form>
</li>
</ul>
Related
<?php
include("connection.php");
// Collect services.
$serviceid = "select * from service";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$services = [];
while ($row = $servicevalue->fetch_assoc()) {
$services[] = $row;
}
// Collect activities.
$activityid = "select * from activity";
$activityvalue = $conn->query($activityid) or die ($conn->error.__LINE__);
$activities = [];
while ($row = $activityvalue->fetch_assoc()) {
$activities[] = $row;
}
// Collect something else?
$perid = "select * from periodicity";
$pervalue = $conn->query($perid) or die ($conn->error.__LINE__);
$pers = [];
while ($row = $pervalue->fetch_assoc()) {
$pers[] = $row;
}
foreach ($services as $service):
?>
<form method="post" action="doalert.php" name="test">
<ul>
<li>
<input name='arr[1][service]' type='checkbox' data-id='Incometax'value="<?php echo $service['service_id']?>"/><?php echo $service['servicename']?>
<ol>
<li>
<?php foreach ($activities as $activity) : ?>
<input type='checkbox' name='arr[1][activity][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
</li>
<br>
<?php endforeach;?>
</ol>
</li>
</ul>
<br>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit"/>
</form>
The value of services and activity come from the database, I want to store all the services and related activity in DB serviceactivitymap table, here is my php code but when I store the data then last checked service value only stored with all the activity checked
incometax
filling
return
gst
filling
return
If incometax,gst,filling,return is checkboxes then when we check :
incometax->filling,return and gst->filling,return
Then the code only store
gst->filling,return and gst->filling,return
Instead of :
incometax->filling,return and gst->filling,return
You need to pass the service_id to the activity name like :
<input type='checkbox' name='activity[<?php echo $service['service_id']?>][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
Full form will be like :
<form method="post" action="doalert.php" name="test">
<?php foreach ($services as $service): ?>
<ul>
<li>
<input name='service[]' type='checkbox' data-id='Incometax' value="<?php echo $service['service_id']?>"/><?php echo $service['servicename']?>
<ol>
<?php foreach ($activities as $activity): ?>
<li>
<input type='checkbox' name='activity[<?php echo $service['service_id']?>][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
</li>
<?php endforeach;?>
</ol>
</li>
</ul>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit"/>
</form>
In your code, the form start tag is created every time a loop runs as it is inside the service foreach. Try taking it outside the loop
Also, the name you used for service input tag as arr[1]["service"] will be overwritten everytime the loop runs. Instead I would suggest if you can use a variable increment in each loop like
$i=0;
foreach($services as $service)
{
.....
<input name='arr[$i][service]' ......
$i++;
}
This will differentiate the input tag name and saving may happen properly.
I'm creating a comment system in my project. I want each posted question to have a comment. I was able to post the comment, but I am having a problem displaying all comment to it's respective answers. I was able to display only one row but not the row of the comment. I try to use a while loop nested inside the while that echo's each question, but it hangs. When I use if it only displays the first row of the comment of each question.
So my question is how can I display them all?
<div class="answer">
<?php
include 'db.php';
$sql = "select * from answers where questionrid IN(select id from question where id='$qid')";
$result = mysqli_query($con,$sql);
if($result){
while($ro = mysqli_fetch_assoc($result)){
?>
<div class="a_view">
<pre>
<?php
echo $ro["answer"];
?>
</pre>
</div>
<div class="ans_comment">
<?php
if($ro["id"]){
$id = $ro["id"];
$sqli = "SELECT * FROM comments WHERE answerid='$id'";
$query = mysqli_query($con,$sqli);
$row = mysqli_fetch_assoc($query);
$num = mysqli_num_rows($query);
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
}
?>
</div>
<div class="add"><div class="coment">add a comment</div> <div id="coment">
<form class="cform" method="post" action="acomment.php">
<textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is
required to give correction or more information about the problem"></textarea><br><br>
<input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid">
<input type="submit" value="Post your comment">
</form>
</div></div>
<?php
}
}else{
echo "no record";
}
?>
<?php
$con->close();
?>
This is the section that made it hang
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
when I use if, it only echos one row.
Instead of
while($row){
do it just like you're doing in the while loop above
while($row = mysqli_fetch_assoc($query)){
The way you have it now, $row is never changing, and therefore always evaluates to true, leaving you stuck in your loop.
//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php') ?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
<input type="checkbox" name="list[]" value="<?php echo "$f6";?>" /><?php echo "$f6","<br>"; ?>//display result in htmlpage
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
print.php
//after submitting in php page
<?php include('connection.php') ?>
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected){
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem
I want to display content of checkbox from html form to php page.I extracted the checkbox contents from database.problem is how I display the content in php page selecting multiple checkbox.
First of all add error_reporting in your code:
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
You have few issues in your HTML/PHP combination:
Issues:
You are using <input type="checkbox" .. inside the PHP.
your concatenation is wrong "$f6","<br>"
Also need to add name for like name="submit" in button.
Modified Code:
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?><br/>
<?php
$i++;
$d = $f6;
}
?>
Side note:
Suggestion of error_reporting is only for development and staging not for production.
Please use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.
<?php echo "$f6","<br>"; ?>
wrong concatenation
<?php echo "$f6"."<br>"; ?>
use this
Your code should be:-
<?php
// This line should be first.
// You have missed semicolon here.
include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<form action="print.php" method="post">
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<!-- You should write below line as -->
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?> </br>
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
there are couple of errors in your codes. please find the modified codes below with my comment started with //seeyouu:
//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_num_rows($sql);//seeyouu: used wrong function>>mysql_numrows
$i=0; //seeyouu: after my amendment, probably can remove this
while ($row = mysql_fetch_array($sql)) {
//seeyouu: no such way of doing, your $sql already a result >> $f6=mysql_result($sql,$i,"item_name");
//seeyouu: forgot to close your php tag here
?>
<input type="checkbox" name="list[]" value="<?php echo $row["item_name"]; ?>" />
<?php //seeyouu: wrong>>echo "$f6","<br>";
//correct way:
echo $row["item_name"]."<br />";
//$d=$f6;i don't know what is this line for, so i leave it to you.
}
?>
<input type="submit" name="submit" value="submit"/> <!-- seeyouu: forgot to set name: submit -->
</form>
print.php
//after submitting in php page
<?php include('connection.php');
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected)
{
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem
So I have a form that users fill out with some radio buttons. The values from the radio buttons get passed to MySQL. I now want to pull those values from the database, display them in a table on a different page, and apply different styles to them with span tags.
Here's the code from the form:
<input class="radio_style" type="radio" name="job_type" value="fulltime"/>Full-time<br/>
<input class="radio_style" type="radio" name="job_type" value="parttime"/>Part-time<br />
Here's the code for the page where I want to display it:
<div class='job_type_div'>
<?php if($job_type=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php if($job_type=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
<?php } ?>
</div>
So ideally, the "fulltime" value will have one style and the "parttime" value will have another style. But when I try running this code, nothing happens. I'm definitely connecting to the database correctly. And the row name is properly labelled "job_type". Any ideas on where I might be going wrong? Any help would be greatly appreciated :)
First of all, your form should be something like so:
<form action="page_you_want_to_display.php" method="POST">
<label for="type">Job Type:</label>
<label for="fulltime">
<input class="radio_style" id="fulltime" name="job_type" type="radio" value="fulltime">
Fulltime
</label>
<label for="parttime">
<input class="radio_style" id="parttime" name="job_type" type="radio" value="parttime">
Part Time
</label>
<input name="submitted" type="submit" value="Submit">
</form>
The page you want to display on should look something like this:
if(isset($_POST["submitted"])){
$job_type = $_POST['job_type'];
echo '<div class="job_type_div">';
if($job_type=='fulltime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='fulltime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="fulltime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
} elseif ($job_type=='parttime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='parttime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="parttime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
}
echo '</div>';
}
and CSS:
.fulltime {
margin:0px;
padding:5px;
width:300px;
background:#9C0;
color:#fff;
}
.parttime {
margin:0px;
padding:5px;
width:300px;
background:#069;
color:#fff;
}
Tested:
Hope this helps
may be problem in your php. Is there some logic?
$job_type=null;
if($job_type=='fulltime'){
...
if($job_type=='parttime'){
...
}
}
did you set $job_type variable? May be you need something like this:
<div class='job_type_div'>
<?php if($row['job_type']=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php } elseif($row['job_type']=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
</div>
I don't believe that the conditions will work the way you implemented it, try doing it like this:
<?php
echo "<div class='job_type_div'>";
if($job_type=='fulltime') {
echo "<span class='job_type_style'>"
//etc...
While you fetching your array from Data Base you need to use MYSQL_BOTH, to fetch columns by Name.
mysql_fetch_array($res, MYSQL_BOTH)
So you should have something like this:
$job_type_query = "SELECT * FROM `job`; ";
$res = mysql_query($job_type_query) or die(mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_BOTH))
{
echo $row['job_type'];
}
form.php
if you initially set one 'selected' in your form, dont need to check if its set, simple set db like so:
...
mysql_query("UPDATE X SET (job_type='{$_GET['job_type']}') WHERE Y");
...
display.php
As you will probably be making a stylesheet, reference the selectors with the job_type labels, which you put in your database
while($row = mysql_fetch_assoc($resultsource))
echo "<div class='job_type_div'> <span class='{$row['job_type']}_style'>{$row['job_type']}</span>";
thanks now I have a error code HY093 Invalid parameter number after using the renameit SUBMIT button. Any idea why? thanks
Any help would be appreciated. Thanks a lot.
<?php
// init
include("db_con1.php");
require("menu.php");
// modify distribution list name
if(is_numeric($_GET['gid'])) {
$g_id=$_GET['gid'];
$one = $pdo->prepare('SELECT * FROM contactgroups WHERE id=:gid');
$one->bindParam(':gid', $g_id, PDO::PARAM_INT);
if( $one->execute(array(':gid' => $_GET['gid'])) ) {
$result = $one->fetch();
}
}
// distribution list query
$queryl = $pdo->prepare('SELECT id, gr_name FROM contactgroups WHERE status=1 ORDER BY gr_name ASC');
$queryl->execute();
// Members list query
if (isset($_GET['gid'])) {
$g_id=$_GET['gid'];
$querym = $pdo->prepare('SELECT STRAIGHT_JOIN gm.linktype, if( gm.linktype = "group", cg.gr_name, cm.contact_sur ) mname FROM groupmembers gm LEFT JOIN contactgroups cg ON gm.link_id = cg.id LEFT JOIN contactmain cm ON gm.link_id = cm.id WHERE gm.group_id =:gid ORDER BY mname ASC');
$querym->bindParam(':gid', $g_id, PDO::PARAM_INT);
$querym->execute();
}
// distribution list query
$queryr = $pdo->prepare('SELECT * FROM contactmain WHERE status=1 ORDER BY contact_sur ASC');
$queryr->execute();
This is what should but does not work...
if (isset($_POST['renameit'])) {
$ren = htmlspecialchars($_POST['rename']);
$g_id = $_GET['gid'];
if ($g_id !== '' && is_numeric($g_id)) { // Change that first to == if gid != 0 as well
$sqlren = "UPDATE contactgroups SET gr_name = :rename WHERE id = :gid";
$sqlren = $pdo->prepare($sqlren);
$sqlren->bindValue(':rname', $ren); // <<< Is this supposed to be :ren?
$sqlren->bindValue(':gid', $g_id);
if ($sqlren->execute()) {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=groups.php\">";
} else {
//Query failed.
$errorcode = $sqlren->errorCode();
echo $errorcode;
}
} else {
echo 'gid not provided'; // Or something
}
}
?>
and this is the HTML bit:
<form id="group-in" method="post" action="groups.php">
Add new Distribution group: <input type="text" name="newgroup" placeholder="name..."> <input type="submit" name="createit" value="Create new">
Rename groupname: <input type="text" name="rename" value="<?php echo $result['gr_name']; ?>"> <input type="submit" name="renameit" value="Rename">
</form>
<!-- Distribution list -->
<div id="left"><label class="header">Distribution list</label>
<ul>
<?php foreach ($queryl as $i => $rowl) { ?>
<li >
<?php if ($i)?>
<input name="checkbox1_add[]" id="dist_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>" />
<label for="groups_<?php echo $i ?>">
<a href="groups.php?gid=<?php echo $rowl['id']; ?>" <?php $g_id=$_GET['gid']; if ($g_id==$rowl['id']) echo 'class="bold"'; ?> >
<?php echo $rowl['gr_name']; ?>
</a></label>
</li>
<?php } ?>
</ul>
</div>
sorry for the long code but I think I loose this $_GET somehow after selecting the distribution item.
Shouldn't you be doing something like this?
if (isset($_POST['renameit'])) {
$ren = htmlspecialchars($_POST['rename']);
$g_id = $_GET['gid'];
if ($g_id !== '' && is_numeric($g_id)) { // Change that first to == if gid != 0 as well
$sqlren = "UPDATE contactgroups SET gr_name = :ren WHERE id = :gid";
$sqlren = $pdo->prepare($sqlren);
$sqlren->bindValue(':rname', $ren); // <<< Is this supposed to be :ren?
$sqlren->bindValue(':gid', $g_id);
if ($sqlren->execute()) {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=groups.php\">";
} else {
//Query failed.
$errorcode = $sqlren->errorCode();
echo $errorcode;
}
} else {
echo 'gid not provided'; // Or something
}
}
Also, if it's always going to be an integer, you could use:
$g_id = (int)$_GET['gid'];
But you would need to be careful with things that evaluate to 0 and check for it in your if statement:
if ($g_id > 0) {
Assuming 0 is not a valid gid value.
EDIT
Looking at your markup and form, this all seems confused.
For instance, your PHP code is using GET, but your code here is not including the gid in the ACTION attribute. (Also, you really should use two different forms for this, IMO.)
<form id="group-in" method="post" action="groups.php?gid=<?php echo $g_id;?>">
Add new Distribution group:
<input type="text" name="newgroup" placeholder="name...">
<input type="submit" name="createit" value="Create new">
</form>
<form id="group-in" method="post" action="groups.php?gid=<?php echo $g_id;?>">
Rename groupname:
<input type="text" name="rename" value="<?php echo $result['gr_name']; ?>">
<input type="submit" name="renameit" value="Rename">
</form>
However, your comment seems to suggest that multiple checkboxes can be checked to rename a group? But then you don't have a FORM tag around it:
<!-- Distribution list -->
<div id="left"><label class="header">Distribution list</label>
<form id="group-in" method="post" action="groups.php">
<ul>
<?php foreach ($queryl as $i => $rowl) { ?>
<li >
<?php if ($i)?>
<input name="checkbox1_add[]" id="dist_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>" />
<label for="groups_<?php echo $i ?>">
<a href="groups.php?gid=<?php echo $rowl['id']; ?>" <?php $g_id=$_GET['gid']; if ($g_id==$rowl['id']) echo 'class="bold"'; ?> >
<?php echo $rowl['gr_name']; ?>
</a></label>
</li>
<?php } ?>
</ul>
</form>
</div>
The challenge that you're going to have here is that you need to loop through that $_POST['gid'] array, whereas the first single group rename you key off of the gid in the GET. I would suggest organizing your code into a Group/Groups object(s) and use a Model/View/Controller (MVC) pattern to organize your code.
Are you saying that you can't get $_GET['gid'] after you submit the group-in form?
Because if that's the case, what you have to do is create a hidden input with your gid value so it can be available in $_POST.
Without putting much thought to what you're trying to do, I can tell you that you simply can't have code using $_GET and $_POST simultaneously.
Update: I don't think you understood what I meant. But Jared is already doing a better job explaining what is wrong with your code, so I guess I won't repeat it.