I have displayed check box values(ugroup field) from ugroups table.now what i want to do is,when user select multiple check boxes and submit it should be insert into relavent feild in table.this is my code.it's doesn't work.please help me.
//select ugroup's from group table.
<?php
$result = "SELECT id,ugroup FROM group";
$res_result = db::getInstance()->query($result);
?>
group table
<form action="db_sql/db_add_page.php" method="get">
Tittle :<input type="text" size="100" name="tittle" />
Description :<textarea cols="80" id="editor1" name="description" rows="10"></textarea>
//Display ugroups in textboxes and checkboxes
<?php
while( $line=$res_result->fetch(PDO::FETCH_ASSOC)) {
echo '<input type="checkbox" name="ugroup" value=" '. $line['ugroup'] .'" />';
echo'<input type="text" name="ugroup" disabled="disabled" value=" '. $line['ugroup'] .'" size="7" "/>';
echo ' ';
}
?>
<input type="submit" value="Submit">
</form>
db_add_page.php
i want to add only selected check box values to relavant fields.
if(isset($_GET))
{
$tittle = $_GET['tittle'];
$description = $_GET['description'];
$ugroup = $_GET['ugroup'];
$acc_status = "INSERT INTO add_services (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8) VALUES(NULL,'".$tittle."','".$description."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."')";
$rate = db::getInstance()->exec($acc_status);
if(!$rate){
echo '<script type="text/javascript">alert("Update Error !");</script>';
}else{
header('Location:../add_page.php');
echo '<script type="text/javascript">alert("Successfuly Updated User Group !");</script>';
}
}
add_services table
Store them in array than Run your query in while loop
Change this
echo '<input type="checkbox" name="ugroup" value=" '. $line['ugroup'] .'" />';
To this
echo '<input type="checkbox" name="ugroup[]" value=" '. $line['ugroup'] .'" />';
//See added [ ] afte ugroup
Than in another file you do this
$check_boxes = implode("','", $_POST['ugroup']);
$query="INSERT add_services (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8)
VALUES (NULL,'".$tittle."','".$description."','{$check_boxes}')";
Related
I have a form with multiple checkboxes and hidden inputs, which I'm passing to a second page using GET.
I'm then trying to retrieve the value of each checkbox and the input in a loop and echo out the combined value.
HTML:
<form action="criteria.php" method="GET">
<input name="id[]" type="hidden" value="<? echo $criteria_id; ?>" />
<input type="checkbox" name="checked[]" class="checkbox-md" id="<? echo $criteria_id; ?>" value="Y">
<button type="submit" class="btn btn-lilac" role="button">Complete</button>
</form>
PHP:
$criteria_id = $_GET['id']; //get all criteria id
$criteria_checked = $_GET['checked']; //get checked criteria id
foreach($criteria_id as $id) //get id of all checkboxes {
echo "<BR>Criteria = ".$id."Checked = ".$criteria_checked; //returns id + array?
if ($checked='Y')//check if checked {
echo "<BR>Criteria =".$id." Checked = Y";
} else {
echo "<BR>Criteria =".$id." Checked = N";
}
}
You will need to make sure that the inputs have matching array keys:
<input name="id[0]" type="hidden" . . .
<input name="checked[0]" type="checkbox" . . .
<input name="id[1]" type="hidden" . . .
<input name="checked[1]" type="checkbox" . . .
Depending on how you create these you could use the $criteria_id:
<input name="id[<? echo $criteria_id; ?>]" type="hidden" . . .
<input name="checked[<? echo $criteria_id; ?>]" type="checkbox" . . .
This way the id and checked array keys will match. All hidden inputs will be passed from the form but only the checked checkboxes, so check if the key of the id is set in the checked array:
foreach($_GET['id'] as $key => $id) {
if (isset($_GET['checked'][$key])) {
echo "<BR>Criteria =".$id." Checked = Y";
} else {
echo "<BR>Criteria =".$id." Checked = N";
}
}
I'm trying to delete some information from the database, but when I ask the user to confirm action, something goes wrong. Can you help me?
The script prints the categories to choose if nothing is settled. Then, It asks the user to delete or not delete the categories chosen. In the end, it deletes what was chosen.
But there's some error in the last part(first in the script) and I cannot understad where it's going wrong:
<?php
//If the user confirm to delete...
if(isset($_POST['eliminazione_conferma']) and $_POST['eliminazione_conferma']=='conferma'){
//if conferma_eliminazione=1, don't delete
if(isset($_POST['conferma_eliminazione']) and $_POST['conferma_eliminazione']=='1'){
echo 'Eliminazione annullata';
}else{//Delete if conferma_eliminazione=0
while($row=$categoria){
$delete= "DELETE FROM categorie WHERE $row = category_id";
$query=mysql_query($delete);
echo "$row eliminato\n";
}
}
}else{//ELSE, print the form to confirm action
if(isset($_POST['eliminazione']) and $_POST['eliminazione']=='delete'){
//Array with the chosen "categories"
$categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
//Print the chosen categories to ask confirmation
echo'Sicuro di voler eliminare le seguenti categoire?<br />';
foreach($categoria as $row){
echo "$row<br />\n";
}
//Yes = 0, No = 1
echo '<form method="post" action="',$_SERVER['REQUEST_URI'],'">
<input type="radio" name="conferma_elimiazione" value="0" />Si<br />
<input type="radio" name="conferma_eliminzione" value="1" />No<br />
<input type="hidden" name="eliminazione_conferma" value="conferma" />';
foreach($categoria as $row){
echo'<input type="hidden" name="categoria[]" value="',$row,'" />',"\n",'' ;
}
echo'
<input type="submit" value="Conferma" />
</form>';
}else{//In the end, if nothing is settled, print the form to check the category to delete
//Select the categories from the database
echo'<form method="post" action="',$_SERVER['REQUEST_URI'],'" />',"\n",'';
$select = "SELECT nome,category_id FROM categorie ORDER BY category_id" ;
$select_result=mysql_query($select) or die("Mysql Error: ".mysql_error());
while($row = mysql_fetch_assoc($select_result)){
echo '<input type="checkbox" name="categoria[]" value="',$row['category_id'],'">',$row['nome'],'<br />';
}
echo'<input type="hidden" name="eliminazione" value="delete" />
<input type="submit" name="submit" value="Elimina" />
</form>';
}
}
The problem with the query
$delete= "DELETE FROM categorie WHERE $row = category_id";
is
here
WHERE $row = category_id";
What is category_id?
Do you have a value for that?
Probably you want WHERE category_id=/*something here like $row['column_name']*/
Thanks everyone for the help, but I've solved my problem in this way(the script wasn't transformig the value of "conferma_eliminazione" in the integer type):
<h2>Modifica o Elimina Categoria </h2>
<?php
if(isset($_POST['eliminazione_conferma']) and $_POST['eliminazione_conferma']=='conferma'){
$categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
$a=(int)$_POST['eliminazione'];
if($a=='1'){
echo 'Eliminazione annullata';
echo "\n";
}elseif($a=='0'){
foreach($categoria as $row){
$delete= "DELETE FROM categorie WHERE $row = category_id";
$query=mysql_query($delete);
echo "$row eliminato\n";
}
}
}else{
if(isset($_POST['eliminazione']) and $_POST['eliminazione']=='delete'){
$categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
echo'Sicuro di voler eliminare le seguenti categoire?<br />';
foreach($categoria as $row){
echo '',$row,'<br />',"\n";
}
echo '<form method="post" name="form_eliminazione_categoria" action="',$_SERVER['REQUEST_URI'],'">',"\n",'
<input type="radio" name="eliminazione" value="0" />Si<br />' ,"\n",'
<input type="radio" name="eliminazione" value="1" />No<br />',"\n",'
<input type="hidden" name="eliminazione_conferma" value="conferma" />',"\n",'';
foreach($categoria as $row){
echo '<input type="hidden" name="categoria[]" value="',$row,'" /><br />',"\n",'';
}
echo'
<input type="submit" value="Conferma" />
</form>';
}else{
echo'<form method="post" action="',$_SERVER['REQUEST_URI'],'" />',"\n",'';
$select = "SELECT nome,category_id FROM categorie ORDER BY category_id" ;
$select_result=mysql_query($select) or die("Mysql Error: ".mysql_error());
while($row = mysql_fetch_assoc($select_result)){
echo '<input type="checkbox" name="categoria[]" value="',$row['category_id'],'">',$row['nome'],'<br />';
echo "\n";
}
echo'<input type="hidden" name="eliminazione" value="delete" />',"\n",'
<input type="submit" name="submit" value="Elimina" />',"\n",'
</form>';
}
}
I have displayed check box values(ugroup field) from group table.now what i want to do is,when user select multiple check boxes and submit it should be insert into relavent column in one row.now it's insert check boxes values.but not in relevant column .this is my code.Please help me.
//select ugroup's from group table.
<?php
$result = "SELECT id,ugroup FROM group";
$res_result = db::getInstance()->query($result);
?>
<form action="db_sql/db_add_page.php" method="get">
Tittle :<input type="text" size="100" name="tittle" />
Description :<textarea cols="80" id="editor1" name="description" rows="10"></textarea>
//Display ugroups in textboxes and checkboxes
<?php
while( $line=$res_result->fetch(PDO::FETCH_ASSOC)) {
echo '<input type="checkbox" name="group[]" value=" '. $line['ugroup'] .'" />';
echo'<input type="text" name="ugroup" disabled="disabled" value=" '. $line['ugroup'] .'" size="7" "/>';
echo ' ';
}
?><input type="submit" value="Submit">
</form>
db_add_page.php
if(isset($_POST))
{
$tittle = $_POST['tittle'];
$description = $_POST['description'];
$ugroup = $_POST['group'];
$acc_status = "INSERT INTO add_services (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8)
VALUES(NULL,'".$tittle."','".$description."','".$ugroup[0]."','".$ugroup[1]."','".$ugroup[2]."','
".$ugroup[3]."','".$ugroup[4]."','".$ugroup[5]."','".$ugroup[6]."','".$ugroup[7]."')";
$rate = db::getInstance()->exec($acc_status);
if(!$rate){
echo '<script type="text/javascript">alert("Update Error !");</script>';
}else{
header('Location:../add_page.php');
echo '<script type="text/javascript">alert("Successfuly Updated User Group !");</script>';
}
}
i click on checkbox2,checkbox8 and submit.it's insert to g1 and g2.when i click on checkbox 1, checkbox3 its also added to g1 and g2.like below
Change line
echo '<input type="checkbox" name="group[]" value=" '. $line['ugroup'] .'" />';
To
echo '<input type="checkbox" name="group['.$line['id'].']" value=" '. $line['ugroup'] .'" />';
And yes start array index using 1
Normally, when we use $_POST values in checkboxes, those that are not checked will not be included in POST.
[group] => Array
(
[G1] => G1 // those the one you did not picked will not be included
[G3] => G3 // so that means your input is jagged
[G5] => G5 // you cannot hardcode each index (0 - 7)
[G7] => G7 // or they will be undefined (the ones that are missing)
)
So what you do is create a default value (an array) which will hold the defaults.
Then you combine those inputs, to the ones you have in default so that in return you will have a complete structure of insertion, instead of jagged inputs.
So in your form, do something like this:
while($line = $res_result->fetch(PDO::FETCH_ASSOC)) {
echo '<input type="checkbox" name="group['.$line['ugroup'].']" value=" '. $line['ugroup'] .'" />';
// assign G1, G2, indices
echo'<input type="text" name="ugroup" disabled="disabled" value=" '. $line['ugroup'] .'" size="7" "/>';
echo ' ';
}
Then on your processing form:
$default_values = array(); //create a default value
while($line = $res_result->fetch(PDO::FETCH_ASSOC)) {
$default_values[':' . $line['ugroup']] = '';
}
if(isset($_POST)) { // if submitted
$ugroup = array();
$temp = $_POST['group'];
foreach($temp as $val) {
$ugroup[':' . $val] = $val;
}
$combined_input = array_merge($default_values, $ugroup); // combine them so you have a complete structure
$sql = 'INSERT INTO add_services (tittle, description,g1,g2,g3,g4,g5,g6,g7,g8) VALUES (:title, :description, :G1, :G2, :G3, :G4, :G5, :G6, :G7, :G8)';
$acc_status = $db->prepare($sql);
$insert = array(':title' => $title, ':description' => $description,);
$insert = array_merge($insert, $combined_input);
$acc_status->execute($insert);
}
I'm trying to build a page where my users can paste in multiple item #s for that product and it will give them the parent model # for that particular item, where items are given individual identifiers.
However, my users paste there information into the textboxs, but it doesn't pull anything up. When I had one value to search it was able to find the items. My table structure is very simple.Fcsku varchar(45), fnsku varchar(45), updated time(45 are not important to this function).
Here is my query Updated:
<form action="" method="get">
Paste your ZZZ's here: <br><input type="text" name="item" id="textbox"/><br>
<input type="text" name="item2" id="textbox2"/>
<script>document.getElementById('textbox').focus()</script><br />
<input type="submit" value="Submit"/>
</form>
<?php
if (!empty($_REQUEST['item'])) {
$item = mysql_real_escape_string($_REQUEST['item']);
$item2 = mysql_real_escape_string($_REQUEST['item2']);
$sql = "select * from oak3_zzz_to_boo WHERE fcsku like '%".$item."%' or fcsku like '%".$item2."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)) {
echo "<font color=red size=7>";
echo '<center><br /> Parent ASIN: '.$row['fnsku'];
echo "</center></font>";
echo "<br><br><br><br><br>";
}
}
?>
This worked at my server:
<form action="" method="post">
Paste your ZZZ's here:<br />
<input type="text" name="item" id="textbox" /><br />
<input type="text" name="item2" id="textbox2"/><br />
<input type="submit" value="Submit" name="submit"/>
<script>document.getElementById('textbox').focus()</script>
</form>
<?php
if (isset($_POST['submit'])) {
$item = mysql_real_escape_string('%'.$_POST['item'].'%');
$item2 = mysql_real_escape_string('%'.$_POST['item2'].'%');
$sql = "SELECT * FROM oak3_zzz_to_boo WHERE fcsku LIKE '" . $item . "' OR fcsku LIKE '" . $item2 . "'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_assoc($r_query)) {
echo "<font color=red size=7>";
echo '<center><br />Parent ASIN: ' . $row['fnsku'];
echo "</center></font>";
echo "<br /><br /><br /><br /><br />";
}
}
?>
Update mysql with $_POST data from while loop
I'm trying to get this round my head, but i am failing quite hard :[
I have 3 rows in a database which i am echoing out in a while() loop.
A user can change search_terms and then save the fields in mysql, however i don't quite know how to do this, as there are 3 rows, so i can't just do;
<?php
if($_POST['update']) {
mysql_query("....");
}
?>
Here is my code;
<?php
$box_query = mysql_query("SELECT * FROM `ta_boxs`");
while($box = mysql_fetch_array($box_query)) {
echo '<div class="box_content">';
echo '<div class="box">'."\n";
echo '<span class="box_top"></span>';
echo '<div class="box_header"><input type="text" id="title" name="title" class="title-input" value="'.$box['title'].'" /></div>';
echo '<span class="sept"></span>';
echo '<div class="admin-back">';
echo '<form id="form-'.$box['id'].'" name="form-'.$box['id'].'" method="post" action="">';
echo '<p class="sub"><span>Includes:</span> Search for these terms in Twitter Feeds.</p>';
echo '<p class="sub-small">Please enter one word per field or click "add word".</p>';
echo '<p><input type="text" id="search_term_1" name="search_term_1" value="'.$box['search_term_1'].'" class="term-input" />';
echo '<p><input type="text" id="search_term_2" name="search_term_2" value="'.$box['search_term_2'].'" class="term-input" />';
echo '<p><input type="text" id="search_term_3" name="search_term_3" value="'.$box['search_term_3'].'" class="term-input" />';
echo '<span class="hr"></span>';
echo '<p class="sub"><span>Excludes:</span> Ignore these terms in Twitter Feeds.</p>';
echo '<p class="sub-small">Please enter one word per field or click "add word".</p>';
echo '<p><input type="text" id="search_term_1" name="search_term_1" value="'.$box['exc_search_term_1'].'" class="term-input" />';
echo '<p><input type="text" id="search_term_2" name="search_term_2" value="'.$box['exc_search_term_2'].'" class="term-input" />';
echo '<p><input type="text" id="search_term_3" name="search_term_3" value="'.$box['exc_search_term_3'].'" class="term-input" />';
echo '<input type="hidden" id="update" name="update" value="yes" />'
echo '<p><input type="submit" id="update_'.$box['id'].'" name="update_'.$box['id'].'" value="Update" /></p>';
echo '</form>';
echo '</div>';
echo '</div>'."\n";
echo '<span class="box_bottom"></span>';
echo '</div>';
}
?>
There could be 1 output, or 100, but i need a way to save them all, onsubmit. Any ideas?
Since you're outputting each box in a different <form> you'll only every get one back.
If you change:
<input type="hidden" id="update" name="update" value="yes" />
to
<input type="hidden" id="update" name="update" value="' . $box['id'] . '" />
Then you'll be able to look which one was posted back.
I don't think your approach will work because you are generating multiple forms, so PHP will only get the one form that is submitted, and you won't be able to save all the changes on your page.
You might want to consider using one form and naming your inputs like an array (see http://php.net/manual/en/faq.html.php#faq.html.arrays) .
e.g. (simplified)
<form method="post">
<?php while($box = mysql_fetch_array($box_query)): ?>
<!-- div box thing -->
<input name="box[<?php echo $box['id'];?>][search_term_1]" value="<?php echo $box['search_term_1']; ?>">
<input name="box[<?php echo $box['id'];?>][search_term_2]" value="<?php echo $box['search_term_2']; ?>">
<input name="box[<?php echo $box['id'];?>][search_term_3]" value="<?php echo $box['search_term_3']; ?>">
<input name="box[<?php echo $box['id'];?>][exc_search_term_1]" value="<?php echo $box['exc_search_term_1']; ?>">
<input name="box[<?php echo $box['id'];?>][exc_search_term_2]" value="<?php echo $box['exc_search_term_2']; ?>">
<input name="box[<?php echo $box['id'];?>][exc_search_term_3]" value="<?php echo $box['exc_search_term_3']; ?>">
<!-- end div box thing -->
<?php endwhile; ?>
</form>
If you print_r($_POST) after submitting that form you should see that it will be fairly easy to loop over/process.
make one form ,
and in this form set all fields,
one button to submit ,
give to the input elements unique id to every row like you do to the form with the box id