Bagaimana saya bisa memperbarui beberapa nilai kotak centang di php?

Saya Perlu memperbarui beberapa nilai kotak centang pada beberapa catatan secara bersamaan. Saat formulir dimuat, itu harus menampilkan ukuran yang sudah dipilih untuk setiap gaya seperti yang disimpan dalam tabel gaya dan jika kita memilih atau membatalkan pilihan ukuran apa pun pada gaya, itu harus memperbarui bidang ukuran dengan semua ukuran yang sesuai dicentang


DATABASE MySQL

--
-- Table structure for table `style`
--

CREATE TABLE IF NOT EXISTS `style` (
  `id` mediumint(8) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `colour` varchar(255) NOT NULL,
  `sizes` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `style`
--

INSERT INTO `style` (`id`, `name`, `colour`, `sizes`) VALUES
(1, 'Ndombolo', 'Blue', '[39],[40],[41],[42]'),
(2, 'Namondo', 'Green', '[39],[40],[41],[42]');

FORMULIR
Ambil informasi dari database dan jika ada perubahan kotak centang yang dibuat, itu akan memperbarui catatan dengan kotak centang baru yang dipilih

<?php
$db_host = "localhost"; // Databse host
$db_name = "checkbox"; // Database name
$db_username = "root"; // Database username
$db_pass = ""; // Database password
$db_table = "style"; // Database table name
// Establish database Connection
mysql_connect("$db_host","$db_username","$db_pass")
// Disconnect if database user detail are wrong
or die(mysql_error('Cannot conect to database server'));
// Select database
mysql_select_db("$db_name")
// Disconnect if database does not exist
or die("Database does not exist");
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple Shoe Size Edit Via Checkbox</title>
</head>

<body>
<?php
// If the page was modified and submited run this
if (isset ($_POST['submit'])) {
   
// create loop to update multiple rows in the table
// $size = count($_POST['colour_sizes']);
$count = $_POST['count'];
$i = 0;
while ($i <= $count) {
    $sizes = $_POST['sizes'];
    
	$id = $_POST['id'];
    $query = "UPDATE style SET sizes = '$sizes'
                WHERE id = '$i'";
				mysql_query($query);
                ++$i;
}
echo '<h2>Records updated</h2>';
}
?>

<form action="" method="post" name="submit">
<table width="100%" border="0">
  <tr>
    <td>ID</td>
    <td>Name</td>
    <td>Colour</td>
    <td>Sizes</td>
  </tr>
  
 <?
// Loop through the table and display the records in tabular format
$query = "SELECT * FROM style";
$result = mysql_query ($query);
$count = mysql_num_rows($result); // Count table rows
while ($row = mysql_fetch_array($result))
{
    $id = $row["id"];
    $name = $row["name"];
    $colour = $row ["colour"];
    $sizes = $row ["sizes"];
?> 
  
  <tr>
    <td><?php echo $id?></td>
    <td><?php echo $name?></td>
    <td><?php echo $colour?></td>
    <td>
<input type="checkbox" name="sizes[]" value="39" <?php if (preg_match("/39/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 39</label>
     
<input type="checkbox" name="sizes[]" value="40" <?php if (preg_match("/40/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 40</label>    
     
<input type="checkbox" name="sizes[]" value="41" <?php if (preg_match("/41/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 41</label>    
     
<input type="checkbox" name="sizes[]" value="42" <?php if (preg_match("/42/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 42</label>
    
    
    
    </td>
  </tr>
<?php
// End the while loop
}
?>

</table>
<input type="hidden" name="count" value="<?php echo "$count" ?>" />
<input type="submit" class="chromebutton" value="Update" name="submit"/>
</form>


</body>
</html>
_

Saat ini ketika saya mencentang kotak nilai yang perlu saya perbarui. itu hanya memperbarui bidang ukuran dengan kata "Array" tetapi saya ingin memasukkan nilai dalam tanda kurung seperti ini [39], [40], [41] jika ukuran tersebut dicentang dan diperbarui

Ada ide?

kotak centang dari mysql php

0 0

Membagikan

  • 2 Kontributor
  • 7 Balasan
  • 10K Tampilan
  • 9 Jam Rentang Diskusi
  • Pos Terbaru Pos Terbaru oleh patrick1981

Jawaban yang Direkomendasikan

Dijawab oleh jstfsklh211 79 dalam

saat menggunakan tanda kurung "sizes[]" di nama input, php mendapatkan variabel sebagai array

$query = "UPDATE style SET sizes = '$sizes'
                WHERE id = '$i'";

Anda perlu mengulang array ukuran dan membangun string "ukuran" Anda

$sizeString = '';
foreach($sizes …

Dijawab oleh jstfsklh211 79 dalam

coba ubah

<?php
$db_host = "localhost"; // Databse host
$db_name = "checkbox"; // Database name
$db_username = "root"; // Database username
$db_pass = ""; // Database password
$db_table = "style"; // Database table name
// Establish database Connection
mysql_connect("$db_host","$db_username","$db_pass")
// Disconnect if database user detail are wrong
or die(mysql_error('Cannot conect to database server'));
// Select database
mysql_select_db("$db_name")
// Disconnect if database does not exist
or die("Database does not exist");
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple Shoe Size Edit Via Checkbox</title>
</head>

<body>
<?php
// If the page was modified and submited run this
if (isset ($_POST['submit'])) {
   
// create loop to update multiple rows in the table
// $size = count($_POST['colour_sizes']);
$count = $_POST['count'];
$i = 0;
while ($i <= $count) {
    $sizes = $_POST['sizes'];
    
	$id = $_POST['id'];
    $query = "UPDATE style SET sizes = '$sizes'
                WHERE id = '$i'";
				mysql_query($query);
                ++$i;
}
echo '<h2>Records updated</h2>';
}
?>

<form action="" method="post" name="submit">
<table width="100%" border="0">
  <tr>
    <td>ID</td>
    <td>Name</td>
    <td>Colour</td>
    <td>Sizes</td>
  </tr>
  
 <?
// Loop through the table and display the records in tabular format
$query = "SELECT * FROM style";
$result = mysql_query ($query);
$count = mysql_num_rows($result); // Count table rows
while ($row = mysql_fetch_array($result))
{
    $id = $row["id"];
    $name = $row["name"];
    $colour = $row ["colour"];
    $sizes = $row ["sizes"];
?> 
  
  <tr>
    <td><?php echo $id?></td>
    <td><?php echo $name?></td>
    <td><?php echo $colour?></td>
    <td>
<input type="checkbox" name="sizes[]" value="39" <?php if (preg_match("/39/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 39</label>
     
<input type="checkbox" name="sizes[]" value="40" <?php if (preg_match("/40/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 40</label>    
     
<input type="checkbox" name="sizes[]" value="41" <?php if (preg_match("/41/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 41</label>    
     
<input type="checkbox" name="sizes[]" value="42" <?php if (preg_match("/42/", "$sizes")) { echo "checked";} else {echo "";} ?> />
<label for="size1">Size 42</label>
    
    
    
    </td>
  </tr>
<?php
// End the while loop
}
?>

</table>
<input type="hidden" name="count" value="<?php echo "$count" ?>" />
<input type="submit" class="chromebutton" value="Update" name="submit"/>
</form>


</body>
</html>
_9 menjadi

$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

dan memposting hasilnya

Semua 7 Balasan

Bagaimana saya bisa memperbarui beberapa nilai kotak centang di php?

jstfsklh211 79 Poster Ringan

10 tahun yang lalu

saat menggunakan tanda kurung "sizes[]" di nama input, php mendapatkan variabel sebagai array

$query = "UPDATE style SET sizes = '$sizes'
                WHERE id = '$i'";

Anda perlu mengulang array ukuran dan membangun string "ukuran" Anda

$sizeString = '';
foreach($sizes as $size) {
    $sizeString .= ($sizeString == '' ? '' : ', ');
    $sizeString .= "[".$size."]";
}
$query = "UPDATE style SET sizes = '$sizeString'
                WHERE id = '$i'";

0 0

Membagikan

Bagaimana saya bisa memperbarui beberapa nilai kotak centang di php?

patrick1981 0 Poster Pemula

10 tahun yang lalu

Saya memodifikasi kode dalam program saya seperti yang Anda jelaskan untuk memasukkan semua ukuran untuk satu catatan ke dalam string menjalankan loop saya seperti ini, tetapi tidak memperbarui database saya

Bagaimana cara memperbarui beberapa nilai dalam PHP?

Langkah-langkah dalam Pembaruan/Hapus Beberapa Baris PHP .
Pilih baris menggunakan input kotak centang
Tampilkan UI formulir untuk memperbarui kolom tabel
Kirim array detail baris ke PHP
Iterasi melalui larik detail baris untuk menerapkan kueri pembaruan/hapus untuk masing-masing

Bisakah kotak centang memilih beberapa nilai?

Dalam hal checkbox , lebih dari satu opsi dapat dipilih .

Bagaimana saya bisa memasukkan beberapa nilai kotak centang dalam database di PHP w3schools?

Anda dapat menambahkan atribut value='YES' ke setiap kotak centang Anda . Kemudian buat array di mana Anda menganggap tidak ada kotak centang yang dicentang. Karena saat kita mengulanginya nanti, hanya kotak centang yang dicentang yang akan dikirim melalui $_REQUEST. Dan di SQL-Query Anda, ganti variabel dengan item dari array.

Bagaimana cara memasukkan nilai kotak centang dalam database di PHP PDO?

Buat kotak centang. php .
sertakan ("konfigurasi. php");
$checkbox1 = $_POST['chkl'] ;
if ($_POST["Kirim" ]=="Kirim")
for ($i=0; $i
$query="MASUKKAN KE NILAI (nama) karyawan ('". $ kotak centang[$i]. " ')";
mysql_query($query) atau die(mysql_error());