How do I insert data from one table to another in mysql? - php

I have a grid with records and them had a checkbox <input name="id[]" value="<?php echo $valor->id ?>" type="checkbox" class="check" /> and I am trying to copy the selected record to another table
$sql = "INSERT INTO `pendientes` SELECT * FROM clientes WHERE id = $idcopia";
mysql_query($sql);
but I don't know how to send the id, my idea is select a checkbox and with a button "Copy" send the id but maybe there is another way.

Most browsers don't send the checkbox's value, just the word "on". To get around this, you can pass the "value" as a key to the array:
<input type="checkbox" name="id[<?php echo $valor->id; ?>]" class="check" />
Then on the PHP side:
$idlist = implode(",",array_map("intval",array_keys($_POST['id'])));
$sql = "INSERT INTO `pendientes` SELECT * FROM `clientes` WHERE `id` IN (".$idlist.")";
mysql_query($sql);

Related

PHP (check or uncheck) a checkbox from Database

So I'm using a checkbox to insert data into MySql database.
When check it stores "finish"
When unchecked if stores "pending"
Now for an edit feature, I have to fetch and display the data from the database. I need the checkbox to be checked or unchecked if it is stored as finish or pending. Appreciate your help, thank you.
The code to fetch the data is shown below:
<?php
$query = ("SELECT * FROM ebook WHERE Eid='" . $Eid . "'");
$result = mysqli_query($con, $query);
$rows = mysqli_fetch_assoc($result);
?>
Displaying data and checkbox:
<html>
<input type="text" name="title" value="<?php echo $rows["Title"]; ?>">
<input class="tgl tgl-flip" id="chk" type="checkbox" name="chk" />
</html>
Put a condition echo inside the input.
<input class="tgl tgl-flip" id="chk" type="checkbox" name="chk" <?php if ($rows['status'] == 'finish') { echo "checked"; } ?>/>

How to take multiple data at checkbox value in php?

Here I just grabbed the ID or a data in the check box. But I want to capture more data with the ID. How to do it?
<input type="checkbox" name="man" value="<?php echo $row['id']; ?>">
And how do I insert that data into another table via PHP MySQL?
You have to set checkboxes name with array brackets.
Ex: <input type="checkbox" name="man[]" value="<?php echo $row['id']; ?>">
After that if you get their values with $_POST["man"], then an array with value of the checkboxes will be return.

How to Do multiple Deletion using php jquery

here I have a table which its data come from database (Mysql DBMS)
In this table I can delete a book one by one but what I need is to delete multiple Book in once
Something like this
But Actually I have no Idea How to do That Can you guys do me a favor how to do that plz I will be thankful
Create a list of checkboxes with the same name using [] notation, but different values, ids of records presumably:
<input type="checkbox" name="record_id[]" value="42" />
<input type="checkbox" name="record_id[]" value="43" />
<input type="checkbox" name="record_id[]" value="44" />
<input type="checkbox" name="record_id[]" value="45" />
After you select some of them, they will be passed to a server as $_POST['record_id'] array.
Do a foreach:
foreach ($_POST['record_id'] as $id) {
// delete record with $id here
}
Or implode'em, for example:
$sql = "DELETE FROM `mytable` WHERE id IN (" . implode(', ', $_POST['record_id']) . ")";

How to insert checked values from checkbox in mysql by php

There are many Subject in my database. I called them in a form in checkbox. When some checkbox checked, those value will go on my database in "select_subject" table in subject column. And when checked a radio button for course name, its value will go in "course" column. My mysql query and chackbox code was:
<?php
$result = mysql_query("SELECT * FROM vhsubject WHERE lid= 2");
while($row = mysql_fetch_array($result))
{
echo "<input type='checkbox' name='vhsubject[]' value=".$row['subject_name'].">" .$row['subject_name']."<br />";
}
?>
After checked and submit, my query was
<?php
if (isset($_POST['form'])) {
$query2 = "INSERT INTO select_subject (subject, course) VALUES ('$_POST[vhsubject]', '$_POST[course_radio]')";
$insert = mysql_query ($query2) or die(mysql_error());
if ($insert){
echo "Your registration is complete";
}
}
?>
But it doesn't work. Please help me;
Use Implode Function:
You can not store value in the form of an array in mysql, so first convert array into string by using implode function.
here is the example:
<input name="q1[]" type="checkbox" value="a1">Used e-mail<br>
<input name="q1[]" type="checkbox" value="a2">Used instant messenger & chat room<br>
<input name="q1[]" type="checkbox" value="a3">Made a purchase for personal use<br>
<input name="q1[]" type="checkbox" value="a4">Downloaded/Played a video game<br>
<input name="q1[]" type="checkbox" value="a5">Obtained news/information/current events<br>
<input name="q1[]" type="checkbox" value="a6">Looked for employment (Used classified listings)<br>
<input name="q1[]" type="checkbox" value="a7">Looked for recipes<br>
<input name="q1[]" type="checkbox" value="a8">Downloaded a movie<br>
and now use implode function:
<?php
$q1=implode(',', $_POST['q1']);
?>

Updating database with multiple checkbox values received from the loop?

Basically I am trying to update my database with multiple values. Input fields are in a loop, so I need to update multiple checkboxes and textboxes at the same time.
$cityID = $_POST["cityID"];
$cityStatus = $_POST["cityStatus"];
$cityOrder = $_POST["cityOrder"];
$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));
foreach($cities as $row) :
?>
<input type="checkbox" name="cityStatus[]" value="1">
<input type="text" name="cityOrder[]" value="<?php echo $row->city_order ?>">
<input type="hidden" name="cityID" value="<?php echo $row->city_ID; ?>">
What i understood is
1. you have array of 3 fields (cityid, cityorder, city status)
2. you are submitting all these values from the form
3. you want update cityorder and citystatus depends on cityid
The solutions is pretty simple
$i=0;
foreach($_REQUEST['cityOrder']) as $cityorder){
$cityID = $_POST["cityID"][$i];
$cityStatus = isset($_POST["cityStatus"][$i])?$_POST["cityStatus"][$i]:0;
$cityOrder = $_POST["cityOrder"][$i];
$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));
$i++;
}

Categories