Jump to content
lady_rysher

Multiple Update

Recommended Posts

salam to all

sy ade prob ckit utk coding multiple update nih.....boleh x sesaper yg xpert tolong tenggok kn kt mane yg salah...asyik x boleh update je.....

ni coding utk form update dia

<form id="form1" name="form1" method="post" action="proses_kemaskini.php">
  <table width="50%" border="0" cellspacing="2" cellpadding="2">

    <tr>
          <?

    
     while($row=mysql_fetch_array($result))
  {
  ?>
      <td>Kod Barang </td>
      <td><input name="id[]" type="text" id="id[]" value="<?=$row['id'];?>" disabled/></td>
    </tr>
    <tr>
      <td>Butiran</td>
      <td>Kuantiti</td>
    </tr>
    <tr>
      <td><input name="butiran1[]" type="text" id="butiran1[]" value="<?=$row['barang'];?>" /></td>
      <td><input name="kuantiti1[]" type="text" id="kuantiti1[]" value="<?=$row['kuantiti'];?>" /></td>
    </tr>    
    <? 

    } ?>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
yang ni lak tuk proses dia
<?php
// connect to the database and select the correct database
mysql_connect('localhost','root','');
mysql_select_db("course") or die("Unable to select database");

$size = count($_POST['butiran1']);


for ($i = 0; $i < $size; $i++)
{
$sql = "UPDATE barang2 set barang = '".$_POST['butiran1'.$i]."', kuantiti = '".$_POST['kuantiti1'.$i]."' where id = '".$_POST['id'.$i]."'";
mysql_query($sql) or die ("Error in query: $sql");
}



if ($result == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini</a> ";
if ($result == FALSE) echo "data gagal dikemaskini";
mysql_close();
?>

hope korang sume dapat membantu....plzzzz...dh blur sesangat dh nih.... :wacko::blush:

Share this post


Link to post
Share on other sites

ini yg awak buat

<?php
// connect to the database and select the correct database
mysql_connect('localhost','root','');
mysql_select_db("course") or die("Unable to select database");

$size = count($_POST['butiran1']);


for ($i = 0; $i < $size; $i++)
{
$sql = "UPDATE barang2 set barang = '".$_POST['butiran1'.$i]."', kuantiti = '".$_POST['kuantiti1'.$i]."' where id = '".$_POST['id'.$i]."'";
mysql_query($sql) or die ("Error in query: $sql");
}



if ($result == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini</a> ";
if ($result == FALSE) echo "data gagal dikemaskini";
mysql_close();
?>
sepatutnya macam ni
<?php
// connect to the database and select the correct database
mysql_connect('localhost','root','');
mysql_select_db("course") or die("Unable to select database");

$size = count($_POST['butiran1']);


for ($i = 0; $i < $size; $i++)
{
$sql = "UPDATE barang2 set barang = '".$_POST['butiran1'][$i]."', kuantiti = '".$_POST['kuantiti1'][$i]."' where id = '".$_POST['id'][$i]."'";
mysql_query($sql) or die ("Error in query: $sql");
}



if ($result == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini</a> ";
if ($result == FALSE) echo "data gagal dikemaskini";
mysql_close();
?>

memandangkan awak buat macam ni

<input name="butiran1[]" type=text" id="butiran1[]" value="<?=$row['barang];?>" />

Share this post


Link to post
Share on other sites

still xboleh jalan lagi...x tahu nape....still cakap data gagal dikemaskini

boleh bagi db awak tak, dalam bentuk *.sql

table terpilih aje, bukan semua table

sekali data contoh

Share this post


Link to post
Share on other sites

mmm...ni contoh table dia...tp data dalam table just dummy je la...just untuk testing query ni je

-- phpMyAdmin SQL Dump
-- version 2.11.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 02, 2008 at 10:20 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `course`
--

-- --------------------------------------------------------

--
-- Table structure for table `barang2`
--

CREATE TABLE IF NOT EXISTS `barang2` (
  `id` varchar(7) NOT NULL,
  `barang` varchar(30) NOT NULL,
  `kuantiti` varchar(30) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `barang2`
--

INSERT INTO `barang2` (`id`, `barang`, `kuantiti`) VALUES
('0002900', 'pensel', '3'),
('0002900', 'pembaris', '2'),
('0002900', 'pemadam', '1'),
('0003000', 'buku latihan', '10'),
('0003000', 'buku Computer Security', '1');

Share this post


Link to post
Share on other sites

ur database is wrong.. where is ur primary key.. primary key shouldnt be redundant... redundant primary key will cause problem when try to modify the data... modify ur table and create a new primary key.. or just modify the id to make it a primary key..

when posting data.. the textfield shouldnt be disabled.. data wont be sent if the textfield is disabled..

change the db to urs..

<?
 mysql_connect('localhost','root','');
 mysql_select_db("test") or die("Unable to select database");
 ?>

 <form id="form1" name="form1" method="post" action="proses_kemaskini.php">
   <table width="50%" border="0" cellspacing="2" cellpadding="2">

     <tr>
           <?
 $a="select * from barang2";
 $result=mysql_query($a);
     
      while($row=mysql_fetch_array($result))
   {
   ?>
       <td>Kod Barang </td>
       <td><input name="id[]" type="text" id="id[]" value="<?=$row['id'];?>" /></td>
     </tr>
     <tr>
       <td>Butiran</td>
       <td>Kuantiti</td>
     </tr>
     <tr>
       <td><input name="butiran1[]" type="text" id="butiran1[]" value="<?=$row['barang'];?>" /></td>
       <td><input name="kuantiti1[]" type="text" id="kuantiti1[]" value="<?=$row['kuantiti'];?>" /></td>
     </tr>    
     <?

     } ?>
     <tr>
       <td>&nbsp;</td>
       <td><input type="submit" name="Submit" value="Submit" /></td>
     </tr>
   </table>
 </form>
<?php
 // connect to the database and select the correct database
 mysql_connect('localhost','root','');
 mysql_select_db("test") or die("Unable to select database");

 $size = count($_POST['butiran1']);


 for ($i = 0; $i < $size; $i++)
 {
 $sql = "UPDATE barang2 set barang = '".$_POST['butiran1'][$i]."', kuantiti = '".$_POST['kuantiti1'][$i]."' where id = '".$_POST['id'][$i]."'";
 $result=mysql_query($sql) or die ("Error in query: $sql");
 }



 if ($result == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini</a> ";
 if ($result == FALSE) echo "data gagal dikemaskini";
 mysql_close();
 ?>

tiba2 plak hilang post yg tny tuh.. anyway nie reply aku

tak tau soalan nie untuk sapa if unt aku..

aku rasa ada declare $result tuh..

$result=mysql_query($sql) or die ("Error in query: $sql");

guna for loop pasal tuan punya thread guna for..

Edited by li9ht

Share this post


Link to post
Share on other sites

terdapat perbezaan kalau guna disabled dan juga readonly

kod proses_kemaskini.php

<?php
// connect to the database and select the correct database
mysql_connect('localhost','root','007');
mysql_select_db("s_course") or die("Unable to select database");

echo "<pre>";print_r($_POST);
$size = count($_POST['butiran']);
for ($i = 0; $i < $size; $i++)
{
echo $sql = "UPDATE barang2 set kuantiti = '".$_POST['kuantiti'][$i]."', barang = '".$_POST['butiran'][$i]."' ".
"where id = '".$_POST['id'][$i]."' and barang = '".$_POST['butiran'][$i]."' \r";
}
echo "</pre>";
?>
<td><input name="id[]" type="text" id="id[]" value="<?=$row['id'];?>" disabled/></td>
output Jika guna disable
Array
(
    [butiran] => Array
        (
            [0] => pensel
            [1] => pembaris
            [2] => pemadam
            [3] => buku latihan
            [4] => buku Computer Security
        )

    [kuantiti] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 5
            [3] => 10
            [4] => 1
        )

    [pilih] => disable
    [Submit] => Submit
)
UPDATE barang2 set kuantiti = '3', barang = 'pensel' where id = '' and barang = 'pensel' 
UPDATE barang2 set kuantiti = '2', barang = 'pembaris' where id = '' and barang = 'pembaris' 
UPDATE barang2 set kuantiti = '5', barang = 'pemadam' where id = '' and barang = 'pemadam' 
UPDATE barang2 set kuantiti = '10', barang = 'buku latihan' where id = '' and barang = 'buku latihan' 
UPDATE barang2 set kuantiti = '1', barang = 'buku Computer Security' where id = '' and barang = 'buku Computer Security'
<td><input name="id[]" type="text" id="id[]" value="<?=$row['id'];?>" readonly/></td>
Jika guna readonly
Array
(
    [id] => Array
        (
            [0] => 0002900
            [1] => 0002900
            [2] => 0002900
            [3] => 0003000
            [4] => 0003000
        )

    [butiran] => Array
        (
            [0] => pensel
            [1] => pembaris
            [2] => pemadam
            [3] => buku latihan
            [4] => buku Computer Security
        )

    [kuantiti] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 5
            [3] => 10
            [4] => 1
        )

    [pilih] => readonly
    [Submit] => Submit
)
UPDATE barang2 set kuantiti = '3', barang = 'pensel' where id = '0002900' and barang = 'pensel' 
UPDATE barang2 set kuantiti = '2', barang = 'pembaris' where id = '0002900' and barang = 'pembaris' 
UPDATE barang2 set kuantiti = '5', barang = 'pemadam' where id = '0002900' and barang = 'pemadam' 
UPDATE barang2 set kuantiti = '10', barang = 'buku latihan' where id = '0003000' and barang = 'buku latihan' 
UPDATE barang2 set kuantiti = '1', barang = 'buku Computer Security' where id = '0003000' and barang = 'buku Computer Security'

Edited by amin007

Share this post


Link to post
Share on other sites

ur database is wrong.. where is ur primary key.. primary key shouldnt be redundant... redundant primary key will cause problem when try to modify the data... modify ur table and create a new primary key.. or just modify the id to make it a primary key..

when posting data.. the textfield shouldnt be disabled.. data wont be sent if the textfield is disabled..

change the db to urs..

tiba2 plak hilang post yg tny tuh.. anyway nie reply aku

mengomen (komen laa) pada jawapan li9ht

nilah kod php selepas puas mengodek

echo "<pre>";//print_r($_POST)."";
$size = count($_POST['butiran']);
for ($i = 0; $i < $size; $i++)
{
$sql = "UPDATE barang2 set kuantiti = '".$_POST['kuantiti'][$i]."' ,barang = '".$_POST['butiran'][$i]."' 
where id = '".$_POST['id'][$i]."' and barang = '".$_POST['butiran'][$i]."' \r";

$result=mysql_query($sql);
if ($result == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini(".$_POST['id'][$i].",".$_POST['butiran'][$i].")</a><br>";
elseif ($result == FALSE) echo "data gagal dikemaskini(".$_POST['id'][$i].",".$_POST['butiran1'][$i].")<br>";

}
echo "</pre>";
tapi nampak macam buruk pulak buat macam ni :lol: kalau ikutkan, sepatutnya output $result keluar sekali tapi kalau ikut $size, dia akan ulang sebanyak tu juga lady_rysher, awak buat macam ni
mysql_query($sql) or die ("Error in query: $sql");
}
if ($result == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini</a> ";
if ($result == FALSE) echo "data gagal dikemaskini";

saya juga tertanya-tanya $result datang dari mana???

lepas tu awak nak buat if else , hasilnya mesti false

Edited by amin007

Share this post


Link to post
Share on other sites

to li9ht ur were right..i've change my db and added a new primary key

then...$result tuh sepatutnye $sql...hehehe...xperasan...pe pun thanks a bunch...ni coding yang sy dh edit dr hasil tunjuk ajar amin007 n li9ht...

<?php


// connect to the database and select the correct database
mysql_connect('localhost','root','');
mysql_select_db("course") or die("Unable to select database");

echo $size = count($_POST['barang']);


for ($i = 0; $i < $size; $i++)
{
echo $sql = "UPDATE barang2 SET barang = '".$_POST['barang'][$i]."', kuantiti = '".$_POST['kuantiti'][$i]."' where id = '".$_POST['id'][$i]."' AND no_pesanan = '".$_POST['no_pesanan'][$i]."'";
mysql_query($sql) or die ("Error in query: $sql");
}

if ($sql == TRUE) echo "<a href='papar.php'>data berjaya dikemaskini</a> ";
if ($sql == FALSE) echo "data gagal dikemaskini";

mysql_close(); 

?>

to amin007 tq bebanyak..... ^_^

Share this post


Link to post
Share on other sites

to li9ht ur were right..i've change my db and added a new primary key

then...$result tuh sepatutnye $sql...hehehe...xperasan...pe pun thanks a bunch...ni coding yang sy dh edit dr hasil tunjuk ajar amin007 n li9ht...

to amin007 tq bebanyak..... ^_^

pasal primary key tu,

sya rasa awak boleh buat dua medan sekali gus sebagai primary key

contoh

CREATE TABLE IF NOT EXISTS `barang2` (
  `id` varchar(7) NOT NULL,
  `barang` varchar(30) NOT NULL,
  `kuantiti` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`,`barang`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

primary_key.jpg

Share this post


Link to post
Share on other sites

mmmm....ok...ok...trimas.....mmm...kalau nk tanye lg satu boleh x...

tp ni mayb x berkaitan dengan dengan multiple insert nih....ade x sesaper yg tahu camne nk buat no rujukkan contoh macam ni PUO/JMSK/300-8/5(25)...yang boleh auto increment, bile setiap kali user issued out a new surat, nombor dia akan bertambah jd PUO/JMSK/300-8/5(26) and so on...

Share this post


Link to post
Share on other sites

mmmm....ok...ok...trimas.....mmm...kalau nk tanye lg satu boleh x...

tp ni mayb x berkaitan dengan dengan multiple insert nih....ade x sesaper yg tahu camne nk buat no rujukkan contoh macam ni PUO/JMSK/300-8/5(25)...yang boleh auto increment, bile setiap kali user issued out a new surat, nombor dia akan bertambah jd PUO/JMSK/300-8/5(26) and so on...

rujuk ->http://www.phpeasystep.com/phptu/5.html

for ($n=1; $n <= 30; $n++)
{
echo "PUO/JMSK/300-8/5(";
printf("%02d", $n);
echo ")<br>";
}

tapi bagi saya nombor surat tu macam susah nak buat

saya cadangkan buat medan berasingan

dari apa yang saya tahu,

kalau nombor dalam kurungan (25) tu dah sampai 100

kena tukar fail baru, jilid juga kena tukar kan

oleh itu ...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...