I am working on a hotel feature page.
the peice of code I am stuck on is the following:
<?php
$result = mysqli_query($conn, "select * from facilities_type");
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<th scope="row">
<input class="form-check-input" type="checkbox" id="blankCheckbox" name="room_feature_cb[]" value="<?php echo $row["facilitiestype_id"]; ?>">
</th>
<td>
<?php echo $row["room_facilities"] ?>
</td>
<td>
<img src="upload-img/icon/<?php echo $row["facilities_icon"]; ?>" width="25">
</td>
</tr>
<?php
}
?>
I want to save the data by using the array, but i cannot get the name to save into the database
if (isset($_POST["room_feature_savebtn"]))
{
$feature = $_POST['room_feature_cb'];
for($result=0;$result>$feature;$result++)
{
mysqli_query($conn,"insert into facilities_details(facilitiestype_id) value ('$feature')");
}
}
You can use the foreach, is a better option in this case:
if (isset($_POST["room_feature_savebtn"]))
{
$features = $_POST['room_feature_cb'];
foreach ($features as $feature) {
mysqli_query($conn, "insert into facilities_details(facilitiestype_id) value ('$feature')");
}
}
Your error is when make a for you dont use the index of array ($result) that create in this, try with:
if (isset($_POST["room_feature_savebtn"]))
{
$feature = $_POST['room_feature_cb'];
for($result=0; $result < $feature; $result++) {
mysqli_query(
$conn,
"insert into facilities_details(facilitiestype_id) value ('" . $feature[$result] . "')"
);
}
}
I recomend the foreach
Related
I am doing attendance management system but I am unable to understand. please help me
<form method="post">
<div class="table-responsive py-4">
<table class="table table-flush" id="datatable-basic">
<thead class="thead-light">
<tr>
<th>Name</th>
<th>Father's Name</th>
<th>Hall Ticket</th>
<th>Department</th>
<th>Attendace</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_POST["search_students"])){
$department=$_POST['department'];
$sql = "SELECT * FROM student_info WHERE department='$department'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["father_name"]."</td>";
echo "<td>".$row["hall_ticket"]."</td>";
echo "<td>".$row["department"]."</td>";
echo "<td>
Present <input type='radio' name='attendance[".$row['hall_ticket']."][".$row['department']."]' value='Present'>
Absent <input type='radio' name='attendance[".$row['hall_ticket']."][".$row['department']."]' value='Absent'>
</td>";
echo "</tr>";
}
}
}
?>
</tbody>
</table>
</div>
<div class="col-lg-12 mb-3">
<button type="submit" name="mark_attendance" class="btn btn-success">Mark Attendance</button>
</div>
</form>
</div>
<?php
if(isset($_POST["mark_attendance"])){
$attendance=$_POST['attendance'];
foreach ($attendance as $key => $value) {
if($value=="Present") {
$query="Insert into attendance (hall_ticket,attendance) values ('$key','Present')";
$insertAttendance=$conn->query($query);
}
else {
$query="Insert into attendance (hall_ticket,attendance) values ('$key','Absent')";
$insertAttendance=$conn->query($query);
}
}
if($insertAttendance){
echo "Attendance Updated Sucessfully";
}
}
?>
</div>
Here i want 2 key to store into my database as hall ticket value and department value.
attendance[".$row['hall_ticket']."][".$row['department']."]
how to use both variables to store into my db. I want to store each table person attendance with their department and hallticket. i will get the hallticket if i remove department from name and simply use $key to store the values.
Try following code:
HTML Code (Created dummy data for reference):
<form action='new.php' method="POST" >
Presnt<input type="radio" name="attendance[111][555]" value="present"><br>
Absent<input type="radio" name="attendance[111][555]" value="absent"><br><br>
Presnt<input type="radio" name="attendance[222][555]" value="present"><br>
Absent<input type="radio" name="attendance[222][555]" value="absent"><br><br>
Presnt<input type="radio" name="attendance[333][555]" value="present"><br>
Absent<input type="radio" name="attendance[333][555]" value="absent"><br>
<input type="submit">
</form>
PHP Code:
<?php
$attendance=$_POST['attendance'];
$data = "";
foreach ($attendance as $key => $value) {
$department = array_keys($value)[0];
$data.="('". $key ."','". $department ."', '" . $value[$department] . "'),";
}
$query = "INSERT INTO attendance (hall_ticket,department,attendance) VALUES " .
rtrim($data, ",");
// Your Query will look like:
// Query = "INSERT INTO attendance (hall_ticket,department,attendance)
// VALUES ('111','555', 'present'),
// ('222','555', 'present'),
// ('333','555', 'absent')";
// Now execute your query
$conn->query($query);
echo $sql;
?>
Note :
Never hit the database in for loops
Even this code will work for you but this is open for SQL injection, you should use a prepared statement to build and execute you query.
so you $attendance is
$attendance=array(
$row['hall_ticket'] => array(
$row['departement']"=> [" present " OR "absence"]
);
this what i understand
my solution is
foreach ($attendance as $hall => $departements) {
foreach($departements as departement => $value){
$req="INSERT INTO attendance (hall_ticket,department,attendance)
values ('".$hall. "','". $departement ."' ,'". $value ."')";
$insertAttendance=mysqli_query($dataBaseConnect,$req);
}
look at my plugin it show all table from WordPress in select option. I want to show default one table in option this my code and screenshot.
<form id="wp_csv_to_db_form" method="post" action="">
<table class="form-table">
<tr valign="top"><th scope="row"><?php _e('Select
Database Table:','wp_csv_to_db'); ?></th>
<td>
<select id="table_select" name="table_select" value="w<option name=""
value="wp_orderlist"></option>
<?php // Get all db table names
global $wpdb;
$sql = "SHOW TABLES";
$results = $wpdb->get_results($sql);
$repop_table = isset($_POST['table_select'])
? $_POST['table_select'] : null;
foreach($results as $index => $value) {
foreach($value as $tableName) {
?><option name="<?php echo
$tableName ?>" value="<?php echo $tableName ?>" <?php
if($repop_table === $tableName) { echo 'selected="selected"'; } ?>>
<?php echo $tableName ?></option><?php
}
}
?>
</select>
[enter image description here][1]
Replace your current sql code:
$sql = "SHOW TABLES";
to:
$sql = "SHOW TABLES LIKE '" . $_POST['table_select'] . "'";
This will select one table specified in $_POST['table_select']
try this
work on native php
<select name="Kode" class="form-control">
<?php
$konek = mysqli_connect("localhost","root","","dbsia");
$query = "select * from tbpelajaran";
$hasil = mysqli_query($konek,$query);
while($data=mysqli_fetch_array($hasil)){
echo "<option value=$data[kodepelajaran]>$data[kodepelajaran]</option>";
}
?>
I have a dropdown list with 2 option (LOCAL and IMPORT). The option for Import is working and I can get the value and echo the $refnumb (IMCK2016-0000001). But when I select LOCAL I get the same value of $refnumb IMCK2016-0000001 instead of LOCCK2016-0000001. Please help me find what's wrong with my code. Thanks.
<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>
<!--<tr><td><input type='submit' name='lookup_master' value='COPY' /></td></tr>-->
<?php $shipmenttype = array("0"=>"Please select...", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype'>
<?php foreach($shipmenttype as $keyname=>$ship_type):?>
<?php
$selected ="";
$shiptype = $_POST['shiptype'];
if($keyname == $shiptype): $selected =" selected"; endif;
?>
<option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
<?php endforeach;?>
</select></b></td></tr>
</table>
<br><br>
<?php
$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($keyname);
if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}
$refnumb = $keyname.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT);
?>
Reference Number:
<input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly />
Here's the function for dbNextID.
function dbNextID($key) {
$sql1 = "insert into key_master (keyname) values (:keyname)";
$sql2= "update key_master set id = id + 1 where keyname = :keyname";
$sql3 = "select id from key_master where keyname = :keyname";
$conn = dbConnect();
$stmt1 = $conn->prepare($sql1);
$stmt2 = $conn->prepare($sql2);
$stmt3 = $conn->prepare($sql3);
$conn->beginTransaction();
$stmt1->execute(array(':keyname' => $key));
$stmt2->execute(array(':keyname' => $key));
$stmt3->execute(array(':keyname' => $key));
$value = $stmt3->fetchColumn(0);
$conn->commit();
$conn=null;
return $value;
}
Your variable $keyname is getting set inside your for loop. When you are printing out $refnumb, you are referencing $keyname outside the loop. This means that you will get the last element in $shipmenttype is "IM").
To fix this, you will need to track the selected value.
<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>
<!--<tr><td><input type='submit' name='lookup_master' value='COPY' /></td></tr>-->
<?php $selectedValue = 0; $shipmenttype = array("0"=>"Please select...", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype'>
<?php foreach($shipmenttype as $keyname=>$ship_type):?>
<?php
$selected ="";
$shiptype = $_POST['shiptype'];
if($keyname == $shiptype) $selected =" selected";$selectedValue = $keyname; endif;
?>
<option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
<?php endforeach;?>
</select></b></td></tr>
</table>
<br><br>
<?php
$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($selectedValue);
if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}
$refnumb = $selectedValue.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT);
?>
When I show this PHP on browser I couldn't get any value to table. My table is empty.
<?php
$sql = "select * from worksets where 'groupid'='$targetid'";
$query = mysql_query($sql);
$kadro=$_GET['kadro'];
if((mysql_num_rows($query) > 0)) {
while($result = mysql_fetch_array($query)) {
$groupid = $result[0];
$groupname = $result[2];
$sql2 = "select flag from havestatus where kadroid='$kadro' and yid='$groupid'";
$query2 = mysql_query($sql2);
if((mysql_num_rows($query2) == 0)) {
$sql3 = "insert into havestatus values('$kadro','$yid',0)";
} else {
$select='selected';
}
?>
<tr>
<td width='50%'><?php echo $groupname;?></td>
<td></td>
<td width='20%'><input type="checkbox" name="bev" checked="<?php echo $select; ?>"></td>
</tr>
<?php
}
}
?>
If I change code in simple way like;
It shows some values and unfunctional checkbox.
I need to get some values from database and fill checkboxs according to 0 or 1 from database
and I have to insert all check box values with one form submit button to database as different rows for every check box.
<?php
$sql = "select * from worksets where groupid='$targetid'";
$query = mysql_query($sql);
if((mysql_num_rows($query) > 0)) {
while($result = mysql_fetch_array($query)) {
$groupid = $result[0];
$groupname = $result[2];
?>
<tr>
<td width='50%'><?php echo $groupname;?></td>
<td></td>
<td width='20%'><input type="checkbox" name="bev"></td>
</tr>
<?php
}
}
?>
it is work after 'groupid'='$targetid'" to groupid='$targetid'"
I have a table I can select and echo the name of the columns (th) and field data (td). But the user can add and delete columns. How I write a more flexible code that could adapt to the user changes? I mean to be able to have the entire table without knowing all the fields.
<?php
$sql = "SELECT * from eee";
$result = mysql_query($sql,$con);
$id = mysql_field_name($result, 0);
$a = mysql_field_name($result, 1);
$b = mysql_field_name($result, 2);
$c = mysql_field_name($result, 3);
?>
<tr>
<th><input class="gris" type="text" name="<?php echo $id ?>" value="<?php echo $id ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $a ?>" value="<?php echo $a ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $b ?>" value="<?php echo $b ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $c ?>" value="<?php echo $c ?>"/></th>
</tr>
<?php
$result = mysql_query("SELECT * FROM eee");
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td> <input class="blanc" type="text" name="num" value="<?php echo $row['id']?>"/> </td>
<td><input class="blanc" type="text" name="a" value="<?php echo $row['a']?>"/></td>
<td><input class="blanc" type="text" name="b" value="<?php echo $row['b']?>"/></td>
<td><input class="blanc" type="text" name="c" value="<?php echo $row['c']?>"/></td>
</tr>
<?php } ?>
</table>
What you are attempting to do is provide for a poor man's ORM. I would suggest to you to read up on INFORMATION_SCHEMA. It is an ANSI standard that can provide you meta information about your databases and tables. You can select column names on the fly from there, and many modern RDMS's support it.
Another option would be to investigate Doctrine as it will provide this functionality for you.
First of all, like people have commented, you should use a new mysql library such as mysqli.
You can use mysql_fetch_assoc($result) to get an associative ( column => value ) array. Then you can loop through it.
$result = mysqli_query($query);
// Make the table headers
$assoc_data = mysqli_fetch_assoc($result);
echo "<tr>";
foreach ($assoc_data as $column => $data) {
echo "<th>$column<th>";
}
echo "</tr>";
// Fill in the columns with the data from the DB
do {
foreach($assoc_data as $column => $data) {
echo "<td><input name=\"$column\" value=\"$data\"></td>";
}
} while ($assoc_data = mysqli_fetch_assoc($result));
This way if the DB columns change, or are renamed, or whatever, your table will automatically adjust to those changes.
Let's say $rs is an array of associative arrays comprising the result set, like you can get from most database interfaces. [particularly those that don't use mysql_* functions since they're being deprecated soon]
<?php
if( count($rs) == 0 ) { die('no values returned'); }
foreach( $rs[0] as $key => $value ) {
printf('<th>%s</th>', $key);
}
foreach( $rs as $row ) {
foreach($row as $value) {
printf('<td>%s</td>', $value);
}
}
Or if you simply must keep using creaky old functions...
<?php
$row = mysql_fetch_array($result) or die('no values returned');
foreach( $row as $key => $value ) {
printf('<th>%s</th>', $key);
}
do {
foreach($row as $value) {
printf('<td>%s</td>', $value);
}
} while($row = mysql_fetch_array($result))
Both of those should print out tables with proper column headers for any size of result set you feed it.