I want to select a row from MySQL database which there are multiple rows.
I've chosen radio button for selection and I've put id of that row in radio button value.
The problem is the posted radio button is set and when I echo it as you can see in the code it shows me the right id.
When I want to select from database with MySQL query it gives me and index undefined error, the strange point is when I select a table that contains just one row this code is working but when number of rows are more than one I am not able to select the chosen row.
Table code:
<?
if($_SESSION["count"]!=0 || $_SESSION["count"]!="")
{
?>
<!-- begining of levels informations -->
<form name="form2" method="POST" dir="rtl" action="" style="font-family:'B_yekan';">
<div align="center" width="900" >
<table class="styled-table" cellspacing="0" width="900" border="1">
<tr>
<th width="10" scope="col" ></th>
<th width="60" scope="col">level</th>
<th width="60" scope="col">date</th>
<th width="60" scope="col">time</th>
<th width="54" scope="col">price</th>
<th width="60" scope="col">mark</th>
</tr>
<?php
$id = array();
while($rows=mysql_fetch_array($result)){
$id[]=$rows['id'];
?>
<tr>
<td><input class="styled-input" type="hidden" name="id[]" id="id" value= "<? echo $rows['id']; ?>" /></td>
<td><input class="styled-input" type="text" name="lev" id="lev" value="<? echo $tbl_name; ?>" /></td>
<td><input class="styled-input" type="text" name="date[]" id="date" value="<? if($rows['date']=="1"){echo "even";}else{echo "odd";}?>" /></td>
<td><input class="styled-input" type="text" name="time[]" id="time"
value="<?if($rows['time']=="pm1"){ echo"16 - 17:30";}
elseif($rows['time']=="pm2"){ echo "17:45 - 19:15";}
elseif($rows['time']=="pm3"){echo "19:30 - 21";}?>" />
</td>
<td><input class="styled-input" type="text" name="price[]" id="price" value= "<? echo $rows['price']; ?>" /></td>
<td><input class="styled-input" style="padding: 5px;width:20px" type="radio" name="mark[]" id="mark" value="<? echo $rows['id']; ?>" /></td>
</tr>
<?php
}//end of loop
?>
</table>
</div>
<input class="styled-button-8" type="Submit" value="firstchoose" name="firstchoose" />
<?}//end of if check for count?>
</form>
Select code:
<!-- first choose level -->
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['firstchoose']) && $_POST['firstchoose'] == 'firstchoose')
{
$tbl_name=$_POST['lev'];
for($i=0;$i<$_SESSION["count"];$i++)
{
$checked =mysql_real_escape_string($_REQUEST['mark'][$i]);
echo $checked;
$sql2="SELECT * FROM $tbl_name WHERE `id`='".mysql_real_escape_string($checked)."' ";
$result2=mysql_query($sql2);
$data = mysql_fetch_assoc($result2);
$count2=mysql_num_rows($result2);
}
if(isset($result2)){
?>
<script language="javascript">alert('the level is selected successfully.'); </script>
<?php
}
}
?>
<!-- end of first choose level -->
Related
I have this code in php
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<form method="POST" action="addtocart.php">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item=mysqli_query($conn,"select * from store");
while($pqrow=mysqli_fetch_array($item)){
$iid=$pqrow['id_item'];
?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td"><input type="hidden" name="item" value="<?php echo $iid; ?>"><input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>"></td>
</tr><?php } ?>
</table></form>
</div>
When I click AddToCart in first row, the value of the last row has been fetch. I want to get the id and the input quantity when I click the AddToCart in 1st row.
As you have only one form tag and multiple inputs with same name on this form, value of next input overwrites previous one. As a result you always have value of the last row.
You can fix it for example with creating a separate form for every row, e.g:
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item = mysqli_query($conn,"select * from store");
while ($pqrow=mysqli_fetch_array($item)) {
$iid=$pqrow['id_item'];?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td">
<!-- Separate form for each record -->
<form method="POST" action="addtocart.php">
<input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>">
<input type="hidden" name="item" value="<?php echo $iid; ?>">
<input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>">
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
In this case you should track with javascript the value of quantity field. Of course, you can move <input type="number"> in a form too, but this will break your markup.
Also, if you're familiar with javascript, you can add some client preprocessing (getting values from inputs of a certain row) and use ajax-request to send these values to server.
I have a script that successfully stores the hex color code in my database when the user selects a color. How do i display this color that is being stored in my database on my page color.php instead of it displaying the hex color code of that picture stored in the database?
color.php
<?php
$con=mysqli_connect("localhost","root","root","car");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM general";
$records=mysqli_query($con,$sql);
?>
<table class="table table-striped table-hover table-bordered datatable">
<tbody>
<?php
$result = mysqli_query($con,"SELECT * FROM general WHERE sex ='male' ");
$count = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result)){
?>
<tr>
<td><center><input type="checkbox" name="check[<?php echo $row['general_id'];?>]" value="1" ></center> </td>
<td><input type="" name="f_name[]" value="<?php echo $row['f_name'];?>"></label></td>
<td><input style="width: 100px" type="" name=" color[]" value="<?php echo $row['color'];?>"></label></td>
<td><input type="" name="options[]" value="<?php echo $row['options'];?>"></label></td>
<td><input style="width: 100px" type="" name="comment[]" value="<?php echo $row['comment'];?>"></label></td>
</tr>
<?php } //end of while loop?>
</tbody>
</table>
Try giving the cell of the color the background-color of your color inline like this:
<tr>
<td><center><input type="checkbox" name="check[<?php echo $row['general_id'];?>]" value="1" ></center> </td>
<td><input type="" name="f_name[]" value="<?php echo $row['f_name'];?>"></label></td>
<td style="background-color:<?php echo $row['color'];?>;"><input style="width: 100px" type="" name=" color[]" value="<?php echo $row['color'];?>"></label></td>
<td><input type="" name="options[]" value="<?php echo $row['options'];?>"></label></td>
<td><input style="width: 100px" type="" name="comment[]" value="<?php echo $row['comment'];?>"></label></td>
</tr>
You could also give your text the color of the hex color you stored in the database or even the entire row, it's all up to you.
I'm making a quiz. I'm getting all the questions on one page, but I don't want that. I only want the first question. After the button is clicked, then i want the second question. How can I accomplish this? Here's my code:
$qry=mysql_query("select `id`,`question` from `paper_details` where `paper_id`='$id'") or die(mysql_error());
$num_rw= mysql_num_rows($qry);
while($f=mysql_fetch_assoc($qry))
{
$q2=mysql_query("select `id`,`que`,`unique_id`,`image` from `que_master` where `id`='".$f['question']."'") or die(mysql_error());
$f2=mysql_fetch_row($q2);
$i=$pid;?>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="4" class="form_border" id="form_table">
<tr>
<th width="3%" rowspan="2" align="left" valign="top" class="form_label"></th>
<th width="5%" height="27" align="left" valign="top" class="form_label">Que<?php echo $i;?> : </th>
<th width="92%" align="left" valign="top" class="form_label"> <?php echo $f2[1]; if($f2[3]!=""){?>
<img src="admin/que_image/<?php echo $f2[3];?>" border="noborder" width="20px" height="20px"/><?php }?>
<input type="hidden" id="que_id" name="que_id" value="<?php echo $f2[0];?>"/>
</th>
</tr>
<tr>
<th height="78" align="left" valign="top" class="form_label">Ans : </th>
<td align="left" valign="top" style="line-height:2.0em;">
<input type="hidden" id="id" name="id" value="<?php echo $id;?>"/>
<input type="hidden" id="pid" name="pid" value="<?php echo $pid+1;?>"/>
<input type="hidden" id="num_raw" name="num_raw" value="<?php echo $num_rw;?>"/>
<input type="hidden" id="time" name="time" value="<?php echo $time;?>" />
<input type="hidden" id="que_id" name="que_id" value="<?php echo $f2[0];?>" />
<?php
$q3=mysql_query("select `id`,`ans`,`ans_img` from `ans_master` where `unique_id`='$f2[2]'") or die(mysql_error());
while($f3=mysql_fetch_row($q3)){
?>
<input type="radio" value="<?php echo $f3[0]; ?>" name="ans" id="ans"><?php echo $f3[1]; ?><?php if($f3[2]!=""){?>
<img src="admin/ans_image/"<?php echo $f3[2];?>" border="noborder" width="20px" height="20px"/>
<?php }
}}
?>
</td>
</tr>
<tr>
<th height="33" align="left" valign="middle" class="form_label"> </th>
<th colspan="2" align="left" valign="middle" class="form_label">
<input type="submit" name="Submit" value="Next >" />
Please help me.
If I understand correctly, you want to have a single-page application to run your quiz.
For this purpose, you could use ajax requests with jquery.
Or else, you can make a separate page for each question.
i am facing problem i am developing school system in php.
i want use checkbox dynamic for attendance but checkbox value i cant use in another file please help me
first file attendacne.php
<form name="abc" method="post" action="attendance-con.php">
<p>Date:
<input name="dat" type="text" class="tcal"/>
<input name="Save" type="submit" style="float:right 20ox;" value="Save Attendace" />
<input name="class_id" type="hidden" value="<?php echo $_REQUEST['class_id']; ?> " />
<?php
$a=0;
$qry=("SELECT DISTINCT
s.rollnum as rollnum,
s.std_name as name
FROM student s WHERE s.std_class=".$_REQUEST['class_id']."");
$result=mysql_query($qry);
while($row=mysql_fetch_array($result))
{
?>
<table width="100%" border="1" align="center" cellpadding="4" cellspacing="4" style="text-align:left;" id="stname">
<?php
if($a==0)
{ ?>
<tr>
<th width="117" height="39" bgcolor="#FFE1CC">Student RollNo</th>
<th bgcolor="#FFE1CC">Student Name</th>
<th width="100" bgcolor="#ECFAFF">Present</th>
</tr>
<?php
}?>
<?php
if($a==1)
{ ?>
<tr>
<th width="117" height="-5" bgcolor="#FFE1CC"></th>
<th bgcolor="#FFE1CC"></th>
<th width="100" bgcolor="#ECFAFF"></th>
</tr>
<?php
}
?>
<?php $a=1; ?>
<input type="hidden" name="rollnum[]" value="<?php echo $row['rollnum'];?>"/><tr>
<td>
<?php echo $row['rollnum'];?>
</td>
<td>
<?php echo $row['name'];?>
</td>
<td>
<input type="checkbox" name="stid[]" value="1">
</td>
</tr>
<?php }?>
</table>
</form>
second file attendance-con.php
<?php
include_once("include/config.php");
$i=0;
$Dat=$_POST['dat'];
$cid=$_POST['class_id'];
$sid=$_POST['stid'];
foreach($_POST['rollnum'] as $x)
{
if($sid[$i]=='1')
{
$query=mysql_query("INSERT INTO attendance (sid,cid,Date,status)
values('$x','$cid','$Dat','1')");
}
if($sid[$i]!='1')
{
$query=mysql_query("INSERT INTO attendance (sid,cid,Date,status)
values('$x','$cid','$Dat','0')");
}
$i=$i+1;
}
?>
<input type="checkbox" name="stid" value="1">
not
<input type="checkbox" name="stid[]" value="1">
I have this PHP/HTML Code:
<form method="post" action="create_quote2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Select</strong></td>
<td><strong>Image</strong></td>
<td><strong>Name</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Quantity</strong></td>
</tr>
<?php
$sql="SELECT * from products ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$counter=0;
while($result=mysql_fetch_array($rs))
{
$counter++;
echo '<input type="hidden" name="product'.$counter.'" value="'.$_POST["checkbox$i"].'" />';
echo '<tr>
<td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox'.$counter.'" /></td>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td><input type="text" name="qty" id="qty" size="20" /></td>
</tr>';
}
echo '<input type="hidden" name="counter" value="'.$counter.'" />';
?>
</table>
<input type="submit" name="submit" value="Next" />
</form>
so when boxes are checked you go to the next page with this code:
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Trade Price</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Total Cost</strong></td>
</tr>
<?php
for($i=1; $i<=$_POST["counter"]; $i++)
{
if($_POST["checkbox$i"])
{
$counter++;
$sql="SELECT * from products where sequence = '".$i."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
echo '<tr>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td>£'.$result["tradeprice"].'</td>
<td> </td>
<td> </td>
</tr>';
}
}
?>
</table>
it works fine and selects all the correct products from the products table but i need a way to get the posted quantity values for each row.
how can i display the posted quantity values on the second page?
P.S. im not worried about SQL injection on this code...
Use <input type="text" name="qty'.$counter.'" id="qty'.$counter.'" size="20" /> on the first page
Then $_POST["qty{$counter}"] or $_POST['qty'.$i] in the relevant cell
As a side note, you may find it easier to use a HEREDOC structure so that you don't have to keep on adding quote marks to echo stuff:
echo <<<BLOCK
<tr>
<td>Image</td>
<td>{$result["title"]}</td>
<td>£{$result["saleprice"]}</td>
<td>£{$result["tradeprice"]}</td>
<td>Quantity - {$_POST['qty'.$i]}</td>
<td> </td>
</tr>
BLOCK;
I found HEREDOC a great help as the quote marks don't blend into one another so much
To get your quantities just change:
<input type="text" name="qty" id="qty" size="20" />
to
<input type="text" name="qty'.$counter.'" id="qty" size="20" />
and then reference the same way you do for other inputs.