<?php
require '../common/connect-db.php';
require_once 'admin-template.php';
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php
// add button
if(isset($_POST['add_dayoff'])) {
$AddQuery = "INSERT INTO dayoff (fname,lname,datef)
VALUES ('$_POST[select1]')";
mysqli_query($db,$AddQuery);
};
?>
<?php
$query="SELECT DISTINCT fname, lname FROM employees";
$result = $db->query($query);
?>
<table><caption>Dayoff Table</caption>
<tr>
<th>First Name</th>
<th>Employee Dayoff</th>
</tr>
<tr>
<td><select name="select1" id="select1" >
<?php
echo '<option value="">Please select...</option>';
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['fname'] . $row['lname'] . "'>" . $row['fname'] ." ". $row['lname'] . "</option>";
}
?>
<td><input type="date" name="date" required /></td>
</select></td>
<td><input type="submit" name="add_dayoff" /></td>
</tr>
</table>
the primary key is email in employees table, i know it's not a secure way to do it, and i should use prepare statements, but it's just a small project. 'dayoff' has an auto incremented int 'id' as primary key.
So what happens is that all records are inserted more than once. And some records fname and lname are concatenated and stored in fname in dayoff table
You have syntax error
INSERT INTO dayoff (fname,lname,datef) VALUES ($_POST[select1]).$_POST[select1] will contains fnamelname.
Moreover you didn't get date value ie:$_post['date']. Try this and let me know the result
<?php
require_once 'admin-template.php';
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php
// add button
if(isset($_POST['add_dayoff'])) {
$fname=$_POST['select1'];// get the value which stores firstname-lastname
$n=explode('-',$fname);//explode the value with -
$datef=$_POST['date'];
$AddQuery = "INSERT INTO dayoff (fname,lname,datef) VALUES ('$n[0]','$n[1]',$datef)";// query should be like this
mysqli_query($db,$AddQuery);
};
?>
<?php
$query="SELECT DISTINCT fname, lname FROM employees";
$result = $db->query($query);
?>
<table><caption>Dayoff Table</caption>
<tr>
<th>First Name</th>
<th>Employee Dayoff</th>
</tr>
<tr>
<td><select name="select1" id="select1" >
<?php
echo '<option value="">Please select...</option>';
while ($row = mysqli_fetch_array($result)) {
echo "<option value='". $row['fname']."-".$row['lname'] . "'>" . $row['fname'] ." ". $row['lname'] . "</option>";
}
?>
<td><input type="date" name="date" required /></td>
</select></td>
<td><input type="submit" name="add_dayoff" /></td>
</tr>
</table>
Related
This problem's driving me crazy since yesterday. I have a table consist of 5 columns: kode_barang (item ID), nama_barang (name of item), qty (quantity), harga_beli (price), jumlah (total). User can input 2 items. This is the code of the form:
<HTML>
<?php include "koneksi.php"; ?>
<form action="insert3.php" method="POST">
<table id="theTable" border="1">
<thead>
<tr>
<th> Kode Barang </th>
<th> Nama Barang </th>
<th> Qty </th>
<th> Harga Beli </th>
<th> Jumlah </th>
<tr>
</thead>
<tbody>
<tr>
<td type="text" name="kode_barang" id="kode_barang1"/readonly>
<?php
mysql_connect("localhost","root","");
mysql_select_db("skripsi_1");
$result = mysql_query("select * from input_data_barang");
$met = "var kode_barang = new Array();\n";
echo '<select name="kode_barang" onchange="changeValue1(this.value)">';
echo '<option></option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';
$met .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};\n";
}
echo '</select>';
?>
</td>
<td><input type="text" name="nama_barang" id="nama_barang1"/readonly>
<script type="text/javascript">
<?php echo $met; ?>
function changeValue1(id){
document.getElementById('kode_barang1').value = kode_barang[id].name;
document.getElementById('nama_barang1').value = kode_barang[id].desc;
};
</script>
</td>
<td><input class="valOne" type="text" name="qty"></td>
<td><input class="valTwo" type="text" name="harga_beli"></td>
<td><input class="result" type="text" name="jumlah"></td>
</tr>
<tr>
<td type="text" name="kode_barang" id="kode_barang2"/readonly>
<?php
mysql_connect("localhost","root","");
mysql_select_db("skripsi_1");
$result = mysql_query("select * from input_data_barang");
$jsArray = "var kode_barang = new Array();\n";
echo '<select name="kode_barang" onchange="changeValue2(this.value)">';
echo '<option></option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';
$jsArray .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};\n";
}
echo '</select>';
?>
</td>
<td><input type="text" name="nama_barang" id="nama_barang2"/readonly>
<script type="text/javascript">
<?php echo $jsArray; ?>
function changeValue2(id){
document.getElementById('kode_barang2').value = kode_barang[id].name;
document.getElementById('nama_barang2').value = kode_barang[id].desc;
};
</script>
</td>
<td><input class="valOne" type="text" name="qty"></td>
<td><input class="valTwo" type="text" name="harga_beli"></td>
<td><input class="result" type="text" name="jumlah"></td>
</tr>
<script>
document.getElementById("theTable").addEventListener("input", function(e) {
var row = e.target.parentNode.parentNode
var val1 = row.querySelector(".valOne").value
var val2 = row.querySelector(".valTwo").value
row.querySelector(".result").value = val1 * val2
})
</script>
</tbody>
<td><input type="submit" value="OK"></a>
<input type="reset" value="Reset"></td>
</table>
</form>
</HTML>
And this is the connection to my database:
<?php
include "koneksi.php";
$kode_barang=$_POST['kode_barang'];
$nama_barang=$_POST['nama_barang'];
$qty=$_POST['qty'];
$harga_beli=$_POST['harga_beli'];
$jumlah=$_POST['jumlah'];
$query ="INSERT INTO faktur (kode_barang, nama_barang, qty, harga_beli, jumlah) VALUES ('".$kode_barang."', '".$nama_barang."', '".$qty."', '".$harga_beli."', '".$jumlah."'), ('".$kode_barang."', '".$nama_barang."', '".$qty."', '".$harga_beli."', '".$jumlah."')";
$sql =mysqli_query($connect, $query);
if ($sql){
header ("location: faktur.php");
}else{
echo "Error.";
echo "<br><a href='input_faktur.php'>Back</a>";
}
?>
Notice that my 'Kode Barang' is a dropdown option, and everytime a user click an item ID, name of the item will be shown automatically in 'Nama Barang' column. Everything in this page works perfectly.
But, when I saved it to database, it didn't save both of the items (item in the first row and the second row). Database only saved the second item but saved it twice. When I add a row to the table become 3 rows, database only save the third item and save it three times. When I add [] to the name, like name=kode_barang[], database didn't save the item ID, but only show "Array" text.
Would anybody please help me with this? Thanks.
You are using the same name for both your fields
use a array
name="nama_barang[]" and name="kode_barang[]"
do a loop in the php to save each field individually
I'm trying to allow a user to specify how many rows they would like to add to the order form for the customer's purchase. This allows the user to have as many rows as needed for purchasing products rather than having a set list. I have the functionality working properly, where if you type in 3 and submit, it will give you three rows to enter in product order information.
The problem I am running into is where I am populating a listbox with the product id and name for the user to select. It populates the first row's list box, but the following list boxes only get the " - " and not the $row[] values. It seems like it's not passing in the sql statement anymore, why is this?
This is the area in my code where I'm running into a problem with the functionality:
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
And this is my entire code:
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
$row = mysqli_fetch_array($result);
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="order.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
<h3>Piedmont Furnishings</h3>
<img id="couch-img" src="couch.jpg" alt="couch">
<table class="orderInfo">
<tr>
<th class="textCol">Order Number:</th>
<td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
<th class="textCol">Order Date:</th>
<td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
</tr>
<tr>
<th class="textCol">Customer:</th>
<td class="inputCol"><input type="text" name="customer"></td>
<td class="textCol"></td>
<td class="inputCol"></td>
</tr>
<tr>
<th class="textCol">Sales Agent:</th>
<td class="inputCol">
<select name="salesAgent">
<option value="selectAgent">Select One</option>
<!-- Populate listbox with Sales Agents ID -->
<?
do { ?>
<option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>
<? } while($row2 = mysqli_fetch_array($result2)) ?>
</select>
</td>
<th class="textCol">Order Status:</th>
<td class="inputCol">
<select name="orderStatus">
<option value="selectStatus">Select One</option>
<!-- Populate listbox with Status ID -->
<?
do { ?>
<option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>
<? } while($row3 = mysqli_fetch_array($result3)) ?>
</select>
</td>
</tr>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_close($db);
?>
</div>
the problem with your code is for first iteration while($row = mysqli_fetch_array($result)) the internal pointer of $result reached at the end... so for next iteration $i=1 there is nothing in the $result but As you use do-while loop the loop must run at least one time and $row[0] & $row[2] is null so you get only "-" . to fix the problem you need to change code slightly.
remove this line $row = mysqli_fetch_array($result);
and add
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
then change like this inside for loop :
<td class="inputCol2">
<select name="'product<?= $i ?>">
<?php
echo $options;
?>
</select>
</td>
I'm creating a form where it allows a user to enter in the number of product rows they would like to have in the form. Then with the corresponding rows, you can select the Product with the Product_ID & Product_Name from a Drop Down List. With the selected item, it should pull the Product_Cost from the table and populate the Unit Price textbox. I can't seem to get the textbox to populate with the correct data. My if statement if (isset($_POST['product' . $i])){ doesn't seem to be working properly, it runs as if the statement were false. I'm trying to say "If a select box has an option selected, take the option selected and find it's corresponding row in the database and take the price found in that row for that product and populate the unit price textbox."
<? require_once("connect_to_DB.php"); //inserts contents of this file here ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Order Form</title>
<meta charset="utf-8">
</head>
<body>
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
//This is for the options in the product list box
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="' . $row[0] . '">' . $row[0] . ' - ' . $row[2] . '</option>';
}
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="example.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>'">
<?
echo $options;
?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>'" ></td>
<? if (isset($_POST['product' . $i])){ ?>
<td class="inputCol2"><input type="text" name="'unit<?= $i ?>'" value="<?= $row[3] ?>" placeholder="$" ></td>
<? } else { ?>
<td class="inputCol2"><input type="text" name="'unit<?= $i ?>'" value="" placeholder="$"></td>
<? } ?>
<td class="inputCol2"><input type="text" name="'total<?= $i ?>'" placeholder="$"></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2"><input type="text" name="totalfinal" placeholder="$"></td>
</tr>
</table>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
</table>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_free_result($result2);
mysqli_free_result($result3);
mysqli_free_result($result4);
mysqli_close($db);
?>
</div>
</body>
There appear to be some syntax errors in there, for instance:
<select name="'product<?= $i ?>'">
<?
echo $options;
?>
</select>
There seems to be several instances where this type of quotation marks are used - I think they ought to be more like:
<select name="product<?= $i ?>">
<?
echo $options;
?>
</select>
Also, in a few places you are not using the correct opening tag for the php blocks though someone might point out different. Generally they ought to be:
<?php
/* statements */
?>
or
<?="print out this string";?>
I have a table with select row with radio button and when click button, the status updated in mysql table but when I click the button, the last user updated and other user not.
<form method="post" accept-charset="utf-8" name="frmUser" >
<table>
<thead>
<tr>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$qr=$mysqli->query("SELECT * FROM `user`");
while($row=$qr->fetch_object()){
?>
<tr>
<td><input type="radio" name="users[]" value="<?php echo $row->id; ?>">
</input></td>
<td class="blck min"><?php echo $row->status; ?></td>
<td><span>
<input type="submit" value="" id="st" name="st">
<?php
if(isset($_POST['st'])){
$rowCount = count($_POST["users"]);
for($i=0;$i<$rowCount;$i++) {
if($row->status == 'ENABLE'){
$mysqli->query("UPDATE `user` SET `status`='DISABLE' WHERE `id`='" . $_POST["users"][$i] . "'");
}
else{
$mysqli->query("UPDATE `user` SET `status`='ENABLE' WHERE `id`='" . $_POST["users"][$i] . "'");
}
}
header("location:users.php");
}
?>
</span></td>
</tr>
<?php}?>
</tbody>
</table>
</form>
Please place below line and check how much count is display:
$rowCount = count($_POST["users"]);
print('<pre>COUNT :: ' . $rowCount . ' >> ');
print_r($_POST["users"]);
print('</pre>');exit;
Above code just display your count value only.
Let me know for more help.!
I need to fetch the rows based on form submission values.
Here is my form
<form name="choose" method "post" t" action="search.php">
<table>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Prof</font1>
</td>
<td>
<select name proffession on>
<option value=""></option>
<option value="doctor"><font4>Doctor</font></option>
<option value="designer">Designer</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Source</font1>
</td>
<td>
<select name source>
<option value=""></option>
<option value="x"><font4>X</font></option>
<option value="y">Y</option>
<option value="z">Z</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Location</font1>
</td>
<td>
<select name location on>
<option value=""></option>
<option value="bangalore">Bangalore</option>
<option value="delhi">Delhi</option>
</select>
</td>
</tr>
<tr>
<td>
<input name=look type=submit value=submit>
</td>
</tr>
</form>
If there is any empty field submission i need to fetch the rows excluding that column.
Here is my search.php
<?php
mysql_connect("localhost","root","");//database connection
mysql_select_db("alldata");
$qry = "SELECT * FROM data WHERE location LIKE '" . mysql_escape_string($_POST['location']) . "' And proffession LIKE '" . mysql_escape_string($_POST['proffession']) . "' And source LIKE '" . mysql_escape_string($_POST['source']) . "'";
$res = mysql_query($qry);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<tr>
"; for($j = 0; $j < count($dataarr); $j++) { echo "<td>".$dataArr[$j]."
</td>
"; } echo "
</tr>
"; } $all = mysql_fetch_all($res); echo "
<table class='data_table'>
"; for($i = 0; $i < count($all); $i++) { create_table($all[$i]); } echo "</table>";
?>
But this script is not able to get me a solution.
Please help
1.Correct your function mysql_fetch_all($res). There is no query
inside the function.
2. Deprecated: mysql_escape_string(): This
function is deprecated; use mysql_real_escape_string()
3. Correct: <select name source> to: <select name="source"> and <select name location on> to <select name="location"> and <input name=look type=submit value=submit> to <input name="look" type="submit" value="submit"> and delete t" from choose form AND <form name="choose" method="post" action="search.php">
Firstly, you should not be using mysql_* functions, see the big red box here. Consider using PDO or MySQLi instead.
Second, you appear to be missing some = such as method="post" You may want to check you're actually receiving your POST values correctly with var_dump($_POST)
Thirdly to exclude non-submitted values, you could construct the query string based on these. Something like:
$qry = "SELECT * FROM data WHERE ";
if($_POST['location']) {
$qry .= 'LIKE "%'. mysql_real_escape_string($_POST['location']) .'%"';
}
// etc...
$qry = "SELECT * FROM data WHERE location LIKE '%" . mysql_escape_string($_POST['location']) . "%' And proffession LIKE '%" . mysql_escape_string($_POST['proffession']) . "%' And source LIKE '%" . mysql_escape_string($_POST['source']) . "%'";