I am having issues getting information out of mysql into multiple checkboxes.
The query im using is this.
<?php
$usergroupid = $_SESSION['UserGroupID'];
$sql="SELECT * FROM sites WHERE UserGroupID='{$usergroupid}' ORDER BY sites.Description";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
$description=$row["sites.Description"];
{
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $description; ?>">
<?php
}
?>
but this only inputs 1 checkbox and has no text after it when there are multiple rows in the table.
Well for starters, never use the same id twice in HTML (you go through a for loop and make each element have the same id, not a good thing). Fix that issue first (make the HTML input element's id include some kind of id from the row)
Then, the real problem comes from the fact that you put the
$description=$row["sites.Description"];
line before your opening brace for the while statement. It should be
while ($row=mysql_fetch_array($result))
{
$description=$row["sites.Description"];
instead.
I would change the code to:
<?php
$usergroupid = $_SESSION['UserGroupID'];
$sql="SELECT * FROM sites WHERE UserGroupID='{$usergroupid}' ORDER BY sites.Description";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
$description=$row["sites.Description"];
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $description; ?>">
<?php
}
?>
The problem was that the "{" should've been written directly after "while ($row=mysql_fetch_array($result))".
Also, I would strongly recommend stop using mysql_query if possible, since mysql_query is now deprecated (read more about it here: http://php.net/manual/en/function.mysql-query.php).
It's not the only issue your code has. For example the formatting, I mean you can't really read it. Learn to properly format your code, sort it up. That will actually help you to prevent other errors.
And as commented, it will also help you to read and understand your code.
And now as the third tip: If you ask a question with properly formatted code you will also get better answers here on the website. So please keep your issues important and do all the best you can do to get help here on site.
<?php
$usergroupid = $_SESSION['UserGroupID'];
$sql = sprintf(
"SELECT * FROM sites WHERE UserGroupID = %d ORDER BY sites.Description",
(int)$usergroupid
);
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$description = $row["sites.Description"];
echo '<input name="checkbox[]" type="checkbox" id="checkbox[]" value="',
$description, '">';
}
You should use mysqli extension instead mysql(is deprecated)
$sql="SELECT * FROM sites WHERE UserGroupID='{$usergroupid}' ORDER BY sites.Description";
$i=0;
$result=mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result)){
$description=$row["sites.Description"];
$xxx= "<input name='checkbox[]' type='checkbox' id='checkbox_$i' value='$description'>";
$i++;
}
echo $xxx;
?>
$description have to be inside the while.
id="checkbox_$i Add autoincrement to make diferent the ids
Related
i have a problem:
if(isset($_POST['send'])){
$id = mysql_real_escape_string($_POST['id']);
$query = mysql_query("select * from somewhere where id='$id'");
$row = mysql_fetch_array( $query );
if(!mysql_num_rows($query)==1){
echo('error');
}
}
After this i have this echo from db:
<input type="text" name="up_name" value="<?php echo $row['name'];?>" id="up_name"/>
and this echo:
<select>
<?php
$up_id=$_POST['up_id'];
$sqlDateUser=mysql_query("SELECT `something` from `somewhere` where id='".$id."'");
$res=mysql_fetch_assoc($sqlDateUser);
$somethig_selected=$res['something'];
$something=mysql_query("SELECT `den` FROM `jud`");
while($row = mysql_fetch_row( $something)){
$selected=($row[0]==$somethig_selected)?'selected':'';
echo "<option value='".$row[0]."' ".$selected.">".$row[0]."</option>";
}
?>
</select>
In this order all it is ok but if i change it, first echo doesn't work. I need to display and others rows after these and i don't know what is the problem with the second echo. Can someone tell me what is the problem?
It may be a naming issue. For example, you have your second query assigning your values for mysql_fetch_row into a variable named $row.
That also is the same name you've given your first query, where you are assigning your mysql_fetch_array.
I would probably try naming them something different like $row_get_user_info and $row_get_den to differentiate them. Also it would make it easier if, when you moved your first echo line, that you also move the query that goes along with it. (Keep them together.)
So as said in title I'm trying to use the query variable given from the page which directs to this one and the form data from THIS page to manipulate the database. I can't seem to get it right and I have no idea what I'm doing wrong. The code snippet looks like this:
<?php
$ware_number = $_GET['id'];
Echo "<form action='usernamecheck.php' method='post'>";
Echo 'Username:<br>';
Echo '<input type="text" name="usernamecheck" size="14"><br>';
Echo 'Password:<br>';
Echo '<input type="password" name="passwordcheck" size="14"><br>';
Echo '<input type="submit" value="Send">';
Echo '</form>';
if (isset($_POST['usernamecheck'])) {
$sql2 = "SELECT * FROM `storedata`.`users` WHERE `username` LIKE '$_POST[usernamecheck]'";
$found_user_id = mysql_query($sql2, $conn);
print $found_user_id;
}
if (isset($_POST['usernamecheck'])) {
$sql3 = "INSERT INTO `storedata`.`basket` (user_id, ware_id, number, complete)
VALUES
('$found_user_id', '$ware_number', 1, 0)";
$derp = mysql_query($sql3, $conn);
print $derp;
}
?>
The document itself is usernamecheck.php, and I was just printing to try and locate the error. When i check the basket table nothing has happened, even though no error is displayed. Right now the variable $ware_number is causing errors. What am I doing wrong?
I have also made user_id and ware_id foreign keys in the storedata.basket table, since they are primary keys in their own respective table. This means they can only be specific values, but I'm testing with these values, primarily 1's and 0's...
What if $_GET['id'] is not set? it will fail. Also please read up into correct usage of SQL in PHP. Your code is vulnerable to SQL injection attacks and whatnot.
EDIT:
updated piece of code
if(isset$_GET['id'] && is_numeric($_GET['id']))
{
$ware_number = $_GET['id'];
Echo "<form action='usernamecheck.php?id=" . $_GET['id'] . "' method='post'>";
.....
Thanks for taking the time to look at this question.
Currently, I have a piece of code that creates four checkboxes labeled as "Luxury, Brand, Retailer," and "B2B." I have looked into a number of PHP methods to create checkboxes, and I felt the implode() function was the most simple and suitable for my job. I have looked into a number of tutorials to create the implosions, however, they did not fit my criteria, as I would like the database values be reflected in the front-end. Currently in my database, the implode() works, therefore (for example), if I check "Luxury", "Brand", "Retailer", and press the "Submit" button, the three items "Luxury, Brand, Retailer" will be in that specified cell. It looks like my code works in the back-end, but these are my issues:
I am not exactly sure (despite multiple Googles) how to retrieve those values stored in the single-cell array, and have it selected as "selected" (this would "check" the box in the front-end)
Could someone kindly take a look at my code below and let me know what seems to be missing/wrong/erroneous so I could attempt the revisions? Anything would be appreciated, thank you!
<?
if (isset($_POST['formSubmit2'])){
$category = mysql_real_escape_string(implode(',',$_POST['category']));
$accountID = $_POST['accountID'];
mysql_query("UPDATE Spreadsheet SET category='$category' WHERE accountID='$accountID'");
}
$query = mysql_query("SELECT * FROM Spreadsheet LIMIT $firstRow,$rpp");
while($row = mysql_fetch_array($query)){
// Begin Checkboxes
$values = array('Luxury','Brand','Retailer','B2B');
?>
<form name ="category" method ="POST" action ="" >
<?
echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";
for($i = 0; $i < count($values); $i++){
?>
<input type="checkbox" name="category[]" value="<?php echo $values[$i]; ?>" id="rbl_<? echo $i; ?>" <? if($row['category'] == $i) echo "checked=\"checked\""; ?>/>
<? echo $values[$i] ?><br>
<? } ?>
<input type ="Submit" name ="formSubmit2" value ="Submit" />
</form>
<? } ?>
The best approach i can recommend given what you have is to, explode the values out of the db giving you a new array of all the select fields. Then use in_array to compare the list you have with this new list in the loop. then flag the checkboxs as needed.
I creating a simple site with PHP where the users can submit blogs and other users (who are logged in) can post comments on them. I have made a link called "comments" below each blog that when clicked will show / hide all the comments relevant to the specific blog (also if the user is logged in, it will show a form field in which they can submit new comments). So basically each blog will have multiple comments. I have done two different codes for this but they both have the same problem that each comment appears twice (everything else works fine). Could anyone point out why?
mysql_select_db ("ooze");
$result = mysql_query ("select * from blog") or die(mysql_error());
$i = 1;
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • Comments<div id='something$i' style='display: none;'>";
$i++;
$a = $row["ID"];
$result2 = mysql_query ("select * from blog, blogcomment where $a=blogID") or die(mysql_error());
while($sub = mysql_fetch_array($result2))
{
echo "<p class='third' >$sub[commentdate] • $sub[username]</p><p>said:</p> <p>$sub[comment]</p>";
}
if ( isset ($_SESSION["gatekeeper"]))
{
echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>';
}
else
{
echo '<p class="third">Signup to post a comment</p>';
}
echo "</div>";
}
mysql_close($conn);
//second version of inner loop://
if ( isset ($_SESSION["gatekeeper"]))
{
while($sub = mysql_fetch_array($result2))
{
echo "<p class='third' >$sub[commentdate] • $sub[username] said:</p> <p>$sub[comment]</p>";
}
echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>';
}
else
{
while($sub = mysql_fetch_array($result2))
{
echo "<p class='third' >$sub[commentdate] • $sub[username] said:</p> <p>$sub[comment]</p>";
}
echo '<p class="third">Signup to post a comment</p>';
}
echo "</div>";
}
mysql_close($conn);
Your problem lies in this query from the first example.
$result2 = mysql_query ("select * from blog, blogcomment where $a=blogID")
You have already queried the blog table so there is no need to query it again. Simply changing this to
$result2 = mysql_query ("select * from blogcomment where $a=blogID")
should solve the problem.
However there are many things you need to think about.
Why are you re-inventing the wheel? There are plenty of good blog applications out there. You'd be better off using one of them.
It's not recommended to use the mysql_ family of functions any more. Go away and learn mysqli_ or better still PDO.
You should learn about separation of concerns. At the very least you should make sure your data access/business logic is separate from your display logic. MVC is very common in PHP.
You should also learn about JOINs. Even in this simple inline script you have a query within a loop which is not very efficient. You can combine your queries into one (as you've tried with the inner query). The difference is the one query should be outside your main loop.
Warning: This question might be a bit long, my apologies in advance.
So in my last question seen here:
Using input checkboxes with a database
I asked the question: "How do I manage multiple users raid attendance with checkboxes and a database loop" and I got a solution that worked in the shortrun, but failed in the longer-run.
Here's the code that runs the loop / allows the user to select who raided:
checked />
When I add this to the database, I actually use 3 queries, shown here:
foreach($_POST['member'] as $member)
{
mysql_query("UPDATE attend set rAttend=(rAttend+1) WHERE UserName='$member'");
mysql_query("INSERT INTO attend set rDate =(CURDATE()) WHERE UserName='$member'");
mysql_query("UPDATE attend set rTotal=(rTotal+1) WHERE UserName='$member'");
}
The reason why I can't use a single 'total' is because each user needs to have the total be based off the amount of raids they attended. Right now the page is displaying like this:
http://i.imgur.com/dwxLf.png
Despite the fact that I entered a date (with CURDATE()) and had selected the checkbox to be checked.
Here's the full code for the query that displays the above: (warning long)
$query="SELECT rTotal FROM rAttend WHERE Username=('$v_member')";
$total=mysql_query($query) or die(mysql_error());
$query="SELECT * FROM rAttend WHERE UserName =('$v_member') order by UserName";
$result=mysql_query($query);
$num=mysql_num_rows($result);
?>
<center><h3><?php echo ($v_member)?>'s attendence record</h3></center>
<?php
$i=0;
$j=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"rDate");
$f2=mysql_result($result,$i,"UserName");
$f3=mysql_result($result,$i,"rAttend");
?>
<tr>
<td><?php echo $f1; ?></td>
<td><?php echo ''.$f2.''; ?></td>
<?php if ($f3 == 1){
echo "<td>yes"; $j++;
}else{ echo "<td>no" ;} ?></td>
</tr>
<?php
$i++;
}
?>
<center>"Raid Attendence: "<?php echo ($j/$total)*100; ?> %</center><br />
</table>
If anyone could help me debug this, I would be most grateful, as php / mysql has never been my favorite language.
Thanks a TON!!!
Edit 1: Shortened posted code by about 30%.
On the second line of your display code:
$total=mysql_query($query) or die(mysql_error());
It seems you're expecting $total to be a number, but it's actually a resource (that's what mysql_query does, it returns a resource to the resultset). You need something like this:
$total_query = mysql_query($query) or die(mysql_error());
$total_row = mysql_fetch_array($total_query);
$total = $total_row['rTotal'];
After working for this on the past few hours, HunderThooves and I finally got the solution. Haha.