How to insert checkbox data of selected value? - php

Hi i am trying to insert checkbox data Yes/No of selected customer. Selected customer (who's value is retrieved from database) if checked yes then Y will be inserted in table field and if checked no then N will be inserted in table field. I can't find out the solution to do so, tried it!
Thanks for help in advance.
Here is what i have so far;
<table width="400" border="1" bordercolor="#598DD5">
<tr>
<td bordercolor="#598DD5" bgcolor="#598dd5"><span class="style1">Projects</span></td>
<td bordercolor="#598DD5" bgcolor="#598dd5"><span class="style1">Hot Project</span>
</td>
</tr>
<tr>
<td bordercolor="#598DD5"><select name="customer">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY customer ORDER BY customer";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["customer"]."'".($row["customer"]==$_REQUEST["customer"] ? " selected" : "").">".$row["customer"]."</option>";
} ?>
</select></td>
<td bordercolor="#598DD5">
<div align="center">
Yes <input type="checkbox" name="hotproject" value="Y" <?php if( isset( $_GET['customer'] ) ){ ?> checked="checked" <?php } ?> />
No <input type="checkbox" name="hotproject" value="N" <?php if( isset( $_GET['customer'] ) ){ ?> checked="checked" <?php } ?> /></div> </td>
</tr>

You can use Javascript with jQuery for change the value of checkbox
OR
Change the type "checkbox" to "radio" with the same name of the input.
Here : http://jsbin.com/UPuQ/1/edit

Related

how do fetching and then inserting from multi check boxes in php

I am going to fetching table values in a html table along checkbox in each row and then inserting values in another database table from multi check boxes in php.
Only the values of checked boxes should be submitted to that table.
db name "laboratory":
test: fetching values.
package: inserting table.
view
Status
Active
Inactive
<?php
$conn=mysqli_connect("localhost","root","","laboratory") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$query="SELECT * FROM test";
$result=mysqli_query($conn,$query);
if ($result) {
while ($record=mysqli_fetch_array($result)) {
Please try to follow this code and implement in your program . Hope that this will cooperate you much
if(isset($_POST['name'])){
$name = $_POST['name'];
$status = $_POST['status'];
if(empty($name) || empty($status)){
echo "Field Must Not be empty";
} else{
$conn=new mysqli("localhost","root","","test");
if($conn){
$query = "SELECT * FROM userdata limit 5";
$stmt = $conn->query($query);
$val = '<form action="" method=""> ';
$val .= '<table> ';
if ($stmt) { ?>
<form action="" method="post">
<table>
<?php while ($result=$stmt->fetch_assoc()) { ?>
<tr>
<td><?php echo $result['post']; ?></td>
<td><input value="<?php echo $result['post']; ?>" type="checkbox" name="check[]" /></td>
</tr>
<?php } ?>
<tr>
<td>Actual Price </td>
<td>Discount</td>
<td>Final Price</td>
</tr>
<tr>
<td><input type="text" name="actual"/></td>
<td><input type="text" name="discount"/></td>
<td><input type="text" name="final"/></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="description" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Cancel" /></td>
</tr>
</table>
</form>
<?php }} }}?>
<?php
if(isset($_POST)){
echo "<pre>";
print_r($_POST);
echo "<pre>";
}
?>`enter code here`
First of all you have to decide that what are you using either mysqli or mysql, if you are using mysqli then you have to improve your code
$query="SELECT * FROM test";
$result=mysqli_query($conn,$query);
if ($result) {
while ($record=mysqli_fetch_array($result)) {
and when you want to insert the checked data will be inserted in package table. If package table in another database then you have to give us the full detail i mean tell us the database name of package table.

Updating a field from an sql table from php

I have a table with more columns. On one column I have 3 buttons for each row with different queries associated. When I click one of the buttons, field in the table from my database should update.The problem is that when I click on a button on any row in the table, it updates only the field from the first row.For example, I have a table with 10 rows.If I click one of the buttons on the 10th row IT will update my first row, not the 10th one as I want. Is there any possibility to solve this?
This is the code..I'm sorry it is too long:
<?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
$result = mysqli_query($link ,$query);
$row = mysqli_fetch_assoc($result);
if(isset($_POST['locul1']))
{
$sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
}
else if(isset($_POST['locul2']))
{
$sql = "UPDATE masculin SET Premiu=2 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
}
else if(isset($_POST['locul3']))
{
$sql = "UPDATE masculin SET Premiu=3 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
} ?>
<u><i><h1 align="center">Administrare concurenti</h1></i></u>
<u><i><h2>MASCULIN</h2></i></u>
<?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
$result = mysqli_query($link ,$query);
if (mysqli_num_rows($result) == 0) {
echo 'Inca nu s-a inscris niciun concurent.';
} else {
?>
<table width="100%">
<tr>
<th>Nr.<br />concurs</th>
<th>Nume</th>
<th>Prenume</th>
<th>CNP</th>
<th>Categoria</th>
<th>Varsta</th>
<th>Premiu</th>
<th>Modifica<br />rezultat</th>
<th>Descalifica</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)){ ?>
<tr>
<?php
$query1="SELECT id_concurent FROM concurenti WHERE CNP='".$row['CNP']."'";
$result1=mysqli_query($link,$query1);
$nr_conc=mysqli_fetch_assoc($result1);
?>
<td> <?php echo $nr_conc['id_concurent'] ?> </td>
<td> <?php echo $row['Nume'] ?> </td>
<td> <?php echo $row['Prenume'] ?> </td>
<td> <?php echo $row['CNP'] ?> </td>
<td> <?php echo $row['Categorie'] ?> </td>
<td> <?php echo $row['Varsta'] ?> </td>
<td> <?php echo $row['Premiu'] ?> </td>
<td>
<form action="administrare.php" method="post">
<input type="submit" name="locul1" value="Premiul 1">
<input type="submit" name="locul2" value="Premiul 2">
<input type="submit" name="locul3" value="Premiul 3">
</form>
</td>
</tr>
<?php } ?>
</table> <?php } ?>
for updated the da tbale row when you submit the values related to a html table row you should add and hidden input with the value that let you reach the correct row eg:
<form action="administrare.php" method="post">
<input type="submit" name="locul1" value="Premiul 1">
<input type="submit" name="locul2" value="Premiul 2">
<input type="submit" name="locul3" value="Premiul 3">
<input type="hidden" name="CNP" value="<?php echo $row['CNP'] ?>">
</form>
and in your sql updated query add the POST value related to the row
$sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$_POST['CNP']. "'";

select option selection in php

I have a form for input order, there are 2 fields, branch and customer model. Before we click button "search", we must select branch and customer fields. After I filled the field, then click button search, the data appears. The problem is, after the data appears, where the condition branch and customer models are filled, I want to create another search, then I changed branch field only (customer model field was same with the previous search), change branch field from 'A' to 'B' and click button search, the data not appears / empty (whereas the data exists in database). Branch field,the field when I changed into B, after I click button search, the branch filled changed into
'SELECT' (branch option are SELECT -> A , B).
I'm confused with this problem.. here is the code :
<?php
if ($_POST)
{
$_SESSION['data_id'] = $_POST['data_id'];
$_SESSION['branch'] = $_POST['branch'];
$_SESSION['customer_model'] = $_POST['customer_model'];
}
?>
<form id="search" class="formular" method="post" action="search_data.php" style="z-index:1;">
<table width="100%">
<tr>
<td>Data ID :</td>
<td><input type="text" name="data_id" value="<?php echo $_SESSION['data_id'];?>"></td>
</tr>
<tr>
<?php
$branch = "'".str_replace(",", "','", $_SESSION['moi_status_UserBranch'])."'";
$check_data = $db->GetAll("select cab_id, cabang_name from company_profile where cab_id in(".$branch.")");
?>
<td>Branch :</td>
<td>
<select id="branch" name="branch">
<option value="0">Select</option>
<?php foreach($check_data as $row): ?>
<option <?php echo ($_SESSION['branch'] == $row['CAB_ID']) ? "selected" : ""; ?> value="<?php echo $row['CAB_ID']; ?>"><?php echo $row['CABANG_NAME']; ?></option>
<?php endForeach; ?>
</select>
</td>
</tr>
<tr>
<td width="110">Customer Model</td>
<td>
<select id="customer_model" name="customer_model" class="readonly validate[required]">
<option <?php echo ($_SESSION['customer_model'] == '0') ? "selected" : ""; ?> value="0">Pilih</option>
<option <?php echo ($_SESSION['customer_model'] == 'I') ? "selected" : ""; ?> value="I">Personal</option>
<option <?php echo ($_SESSION['customer_model'] == 'C') ? "selected" : ""; ?> value="C">Corporate</option>
</select>
</td>
</tr>
</table>
<input type="submit" id="btnsubmits" name="btnsubmit" class="button" value="Search" />
<a onclick="mypopup();" style="text-decoration:underline; cursor:pointer; font-weight:bolder">Add New</a>
</form>
<script>
function mypopup()
{
<?php unset($_SESSION['regional_saved']); ?>
mywindow = window.open("choose_custtype.php" ,"Item","width=700px,height=350px,resizable=1,scrollbars=1,top=150,left=200");
mywindow.moveTo(200, 200);
}

Why are my values not being updated?

I have a simple, unsecured, local database that I'm learning with (yes still using MySQL for now) I can view my database rows in a table and click on an edit button which then displays the single record chosen with each column inside a text box to update and save. Here is my edit page.
<?php
$id = $_GET['id'];
$connect = mysql_connect("localhost", "XXXXXX", "XXXXXXX") or
die ("Check your connection.");
mysql_select_db("toner");
$quey1="select * from inventory where id ='".$id."'";
$result=mysql_query($quey1) or die(mysql_error());
?>
<html>
<form action="updateinfo.php" method="post">
<table>
<?php
while ($row=mysql_fetch_array($result))
{
?>
<tr>
<td align="right">Partnumber:</td>
<td align="left"><input type="text" name="partnumber" value="<?php echo $row['partnumber'];?>"/></td>
</tr>
<tr>
<td align="right">Description:</td>
<td align="left"><input type="text" name="description" value="<?php echo $row['description'];?>"/></td>
</tr>
<tr>
<td align="right">Vendor:</td>
<td align="left"><input type="text" name="vendor" value="<?php echo $row['vendor'];?>"/></td>
</tr>
<tr>
<td align="right">Price:</td>
<td align="left"><input type="text" name="price" value="<?php echo $row['price'];?>"/></td>
</tr>
<tr>
<td align="right">Quantity:</td>
<td align="left"><input type="text" name="quantity" value="<?php echo $row['quantity'];?>"/></td>
</tr>
</table>
<br>
<input type="hidden" name="id" value="<?php echo $row['id'];?>"/>
<input type="submit" value="Edit/Update Toner">
<?php
}
?>
</form>
</html>
Here is my UPDATED updateinfo
<?php
mysql_connect("localhost","XXXXX","XXXXX") or die("Error: ".mysql_error());
mysql_select_db("toner");
$id =$_POST['id'];
$partnumber = $_POST['partnumber'];
$description = $_POST['description'];
$vendor = $_POST['vendor'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$sql = "UPDATE inventory SET partnumber ='".$partnumber."',description ='".$description."',vendor ='".$vendor."',price ='".$price."',quantity ='".$quantity."' WHERE id ='".$id."'";
mysql_query($sql) or die ("Error: ".mysql_error());
echo "Updated successfully";
header( "refresh:10;url=toner.php" );
?>
After adding a primary key I'm able to update the DB however $id = $_POST['id']; still gives an error, any assistance is appreciated. Thanks. Yes I'm aware its vulnerable to injection and that I should be using PDO or MySQLi but I'm still a beginner and this is where I've chosen to start learning. Thank you.
the problem is because of partnumber,here you are again reseting the partnumber but the new one is not present in your database and because of that query does not update the new values.
$sql = "UPDATE inventory SET description ='".$description."',vendor ='".$vendor."',price ='".$price."',quantity ='".$quantity."' WHERE partnumber = '".$partnumber."'";
Solution
Don't again set the partnumber use it only in where clause.

Checkbox checked or unchecked boolean php mysql

I am trying to make this form with checkboxes get values 1 or 0, sort of a boolean, and UPDATE it on mysql, but I havent found a way yet. Maybe you can help. I'll post the form and the code that receives it. I am sure there might be a conflict of loops, but I tryed everything and couldnt get it working. Tks in advance.
THE FORM
<form action="esconde_cat.php" method="post">
<table width="300px" align="center" width="140" border="1">
<tr>
<td></td>
<td width="200">Categoria</td>
<td width="100">Mostrar</td>
</tr>
<?php
include "conecta.php";
include "verifica.php";
$sql = "SELECT * FROM categorias ORDER BY ordem";
$res = mysql_query($sql) or die (mysql_error());
while ($linha=mysql_fetch_array($res)) {
?>
<tr>
<td><input name="user[]" type="checkbox" value="true"/></td>
<td><?=$linha['1']; ?></td>
<td><?php if ($linha['3'] == TRUE){ echo 'SIM'; } else {echo 'NÃO'; } ?></td>
</td>
</tr>
<?php
}
?>
</table>
<br/>
<table align="center">
<tr>
<td>
<input type="submit" name="Delete" type="button" value="Alterar">
</td>
</tr>
</table>
</form>
THE CODE THAT RECEIVES THE FORM AND UPDATE MYSQL
<?php
include "conecta.php";
include "verifica.php";
\\THIS PART WORKS, GETS IF THE CHECKBOX IS CHECKED AND RECORD IT ON DB
if(isset($_POST['user'])) {
foreach ($_POST['user'] as $key => $read){
$read =(#$_POST['user']=='0')? $_POST['user']:'1';
echo $read;
$sql = "UPDATE categorias SET mostrar='$read' WHERE ordem='$key'";
$res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
}
}
\\ON THIS PART OF THE CODE THAT I CANT GET THE VARIABLE TO GET THE
\\VALUE FROM CHEBOXES THAT ARE UNCHECKED RECORDED TO DATABASE
if (!isset($_POST['user'])) {
foreach ($_POST['user'] as $chave => $leia) {
$leia =(#$_POST['user']=='1')? $_POST['user']:'0';
echo $leia;
$sql = "UPDATE categorias SET mostrar='$leia' WHERE ordem='$chave'";
$res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
}
}
?>
If a checkbox is left unchecked you will get NO key in the $_POST variable. If it's checked the key will be set and the value will be the value of the checkbox.
Assuming a name of "user[]" works, with something like:
<input type="checkbox" name="user[]" value="aa" />
<input type="checkbox" name="user[]" value="bb" checked="checked" />
<input type="checkbox" name="user[]" value="cc" checked="checked" />
<input type="checkbox" name="user[]" value="dd" />
<input type="checkbox" name="user[]" value="ee" checked="checked" />
You would expect $_POST to be:
array(
"user" => array(
"1" => "bb"
"2" => "cc"
"4" => "ee"
)
);

Categories