|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Dear Sir/Madam
As i am very new to PHP. i need some from this group. I have a form with 10 records populated from my_sql DB, for simple say Name ,Age ,Sex,place,flg,MoveDate (flg is chek mark) say i have populated this info on the php form, now i want to update only flg and MoveDate, (MoveDate should be updated whenever flg is set) Please advise me how to go about this in php with Pear Package installed Thanks in Advance -evolu |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Mar 30, 10:16 am, bangsund...@gmail.com wrote:
> Dear Sir/Madam > > As i am very new to PHP. i need some from this group. > > I have a form with 10 records populated from my_sql DB, > for simple say Name ,Age ,Sex,place,flg,MoveDate > (flg is chek mark) > > say i have populated this info on the php form, > now i want to update only flg and MoveDate, > (MoveDate should be updated whenever flg is set) > > Please advise me how to go about this in php with Pear Package > installed > > Thanks in Advance > > -evolu I didn't quite understand your request, could you please be more speciffic? You want it updated IN the database on the POST of the form or oyu want it updated live on the user side when the flg is changed? Please be more speciffic, because the approach is rather different on those two scenarios. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
bangsundara@gmail.com wrote:
> Dear Sir/Madam > > As i am very new to PHP. i need some from this group. > > I have a form with 10 records populated from my_sql DB, > for simple say Name ,Age ,Sex,place,flg,MoveDate > (flg is chek mark) > > say i have populated this info on the php form, > now i want to update only flg and MoveDate, > (MoveDate should be updated whenever flg is set) > > Please advise me how to go about this in php with Pear Package > installed > > Thanks in Advance > > -evolu > Not hard. Fetch the data from your $_POST or $_GET array element and use a SQL UPDATE statement to update the appropriate rows in your database. If you want more , you need to post what you've already tried so we can you with it. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Hello
I sorry for writing late and thanks for the reply, this is an eg, what i want to do is , display all the records frm the table, when the "flg" column is changed i want change MoveDate and all the operation or over i want to update at once. Thanks in Advance ------------------- <html> <head> <title>test php</title> </head> <body> <h3>Display 10 records at a time </h3> <?php $sv = "localhost"; $dbname = "test"; $user = "root"; $pass = "****"; $enc_disp = "utf8"; $enc_db = "utf8"; function cnv_enc($string, $to, $from) { $det_enc = mb_detect_encoding($string, $from . ", " . $to); if ($det_enc and $det_enc != $to) { return mb_convert_encoding($string, $to, $det_enc); } else { return $string; } } $conn = mysql_connect($sv, $user, $pass) or die("DB Connect error"); mysql_select_db($dbname) or die("DB select error"); $st = 0; $lim = 10; $sql = "SELECT id, name, age, sex,place,flg,mdate FROM tbltest ORDER BY id LIMIT $st, $lim;"; $res = mysql_query($sql, $conn) or die("Data extract error"); echo "<table border=\"1\">"; echo "<tr>"; echo "<td>ID</td>"; echo "<td>name</td>"; echo "<td>age</td>"; echo "<td>sex</td>"; echo "<td>place</td>"; echo "<td>flg</td>"; echo "<td>mdate</td>"; echo "</tr>"; while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { echo "<tr>"; echo "<td>".$row["id"]."</td>"; echo "<td>".cnv_enc($row["name"], $enc_disp, $enc_db)."</td>"; echo "<td>".$row["age"]."</td>"; echo "<td>".cnv_enc($row["sex"], $enc_disp, $enc_db)."</td>"; echo "<td>".cnv_enc($row["place"], $enc_disp, $enc_db)."</td>"; echo "<td>".$row["flg"]."</td>"; echo "<td>".$row["mdate"]."</td>"; echo "</tr>"; } echo "</table>"; mysql_close($conn); ?> </body> </html> ------------------ |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
bangsundara@gmail.com wrote:
> Hello > I sorry for writing late and thanks for the reply, > this is an eg, > what i want to do is , display all the records frm the table, > when the "flg" column is changed i want change MoveDate > and all the operation or over i want to update at once. > Thanks in Advance > > ------------------- > <html> > <head> > <title>test php</title> > </head> > <body> > <h3>Display 10 records at a time </h3> > <?php > $sv = "localhost"; > $dbname = "test"; > $user = "root"; > $pass = "****"; > $enc_disp = "utf8"; > $enc_db = "utf8"; > function cnv_enc($string, $to, $from) { > $det_enc = mb_detect_encoding($string, $from . ", " . $to); > if ($det_enc and $det_enc != $to) { > return mb_convert_encoding($string, $to, $det_enc); > } > else { > return $string; > } > } > > $conn = mysql_connect($sv, $user, $pass) or die("DB Connect error"); > mysql_select_db($dbname) or die("DB select error"); > > $st = 0; > $lim = 10; > $sql = "SELECT id, name, age, sex,place,flg,mdate FROM tbltest ORDER > BY id LIMIT $st, $lim;"; > $res = mysql_query($sql, $conn) or die("Data extract error"); > echo "<table border=\"1\">"; > echo "<tr>"; > echo "<td>ID</td>"; > echo "<td>name</td>"; > echo "<td>age</td>"; > echo "<td>sex</td>"; > echo "<td>place</td>"; > echo "<td>flg</td>"; > echo "<td>mdate</td>"; > echo "</tr>"; > while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { > echo "<tr>"; > echo "<td>".$row["id"]."</td>"; > echo "<td>".cnv_enc($row["name"], $enc_disp, $enc_db)."</td>"; > echo "<td>".$row["age"]."</td>"; > echo "<td>".cnv_enc($row["sex"], $enc_disp, $enc_db)."</td>"; > echo "<td>".cnv_enc($row["place"], $enc_disp, $enc_db)."</td>"; > echo "<td>".$row["flg"]."</td>"; > echo "<td>".$row["mdate"]."</td>"; > echo "</tr>"; > } > echo "</table>"; > mysql_close($conn); > ?> > </body> > </html> > ------------------ > OK, I'm not sure what your problem is, but I'll take a guess. If you want the user to be able to change the flg entry, you need an input element; in this case I'd probably use a checkbox. The name would be something like "flg[]", with the value being the id. When they submit the form, $_POST['flg'] will be an array with all of the values of the checked boxes. Just build your SQL statement (an UPDATE statement) to change the data you want. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
>>On 4$B7n(B6$BF|(B, $B8aA0(B3:52, Jerry Stuckle <jstuck...@attglobal.net> wrote:
Thank you very much for your kind reply. as per your advice, i made simple php like below pg, now i have 2 doubt. 1.when i read a data to check box from database, how can i set cheked when data is 1 not checked when data is 0. Then 2. After displaying the data, whe the user clicked, then date field can be entered, if not previous ino is retained then the update whereever the user changed the checkbox and date field onlt those info should be updated. here is the sample code wher i have done till now -------------------------- <?php // connect to the database :mysql $host="localhost"; // Host name. $dbuser="test"; // MySQL username. $dbpassword="******"; // MySQL password. $database="test"; // Database name. mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to thye database'); mysql_select_db($database) or die("Unable to select database"); $result = mysql_query("SELECT * FROM tbltest ORDER BY slno,empid"); $i = 0; print "<form name='namestoupdate' method='post' action='tblupdate.php'> \n"; print "<tr>"; print "<th nowrap> No</th>"; print "<th nowrap> Emp No</th>"; print "<th nowrap> Emp Name</th>"; print "<th nowrap> Amount</th>"; print "<th nowrap> Flg </th>"; print "<th nowrap> Date</th>"; while ($rec = mysql_fetch_array($result)) { print "<input type='hidden' name='slno[$i]' value='{$rec['slno']}' />"; print "<tr>"; print "<p>{$rec['slno']} {$rec['empid']} {$rec['empname']} {$rec['amt']} <input type='checkbox' name='{$rec['slno']}' value='{$rec['flg']}'> <input type='text' size='25' name='rec[$i]' value='{$rec['date']}' /></p> "; ++$i; } print "</table>"; print "<p><input type='submit' value='ClickallChkbox' /></p>"; print "<input type='submit' value=' Clear' />"; print "<p><input type='submit' value='Update' /></p>"; print "</form>"; mysql_close(); ?> ------------------------- Thanki you very much in advance -evolu |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
bangsundara@gmail.com wrote:
>>> On 4$B7n(B6$BF|(B, $B8aA0(B3:52, Jerry Stuckle <jstuck...@attglobal.net> wrote: > > Thank you very much for your kind reply. > as per your advice, i made simple php like below pg, > > now i have 2 doubt. > > 1.when i read a data to check box from database, how can i set > cheked when data is 1 not checked when data is 0. > In your checkbox code, you need to add the CHECKED attribute to the checkbox if the flag is set, i.e. echo "input type='checkbox' name='{$rec['slno']}' value='{$rec['flg']}'"; if ($rec['flg'] == 1) echo ' CHECKED'; echo ">\n"; > Then > 2. After displaying the data, whe the user clicked, then > date field can be entered, if not previous ino is retained > then the update whereever the user changed the checkbox and > date field onlt those info should be updated. > You can't do this with PHP on the client. You can use javascript to enable the date field when the checkbox is checked. Try comp.lang.javascript for info on that. When the user submits the data, just look at each of the checkboxes which has been checked. But to do this, you need to change your logic. Your checkbox name should be a constant, such as 'reccb[]'. The value should be the record number, i.e. $rec['slno']. Now your $_POST['reccb'] array will contain an item for each checked checkbox. You also have another problem in that you have the data field named rec[$i]. This can create a conflict with your checkbox as you have it if you have low numbers for your slno. The solution for this - and to keep your date field more closely related to the rest of the data - i.e. name it "recdate[$rec['slno']]". Now when you fetch the checkboxes from the $_POST array, you will have an entry for the date field, i.e. if $_POST['reccb'][0] contains 123, $_POST['recdate'][123] will have the new date value. Use that information to build your SQL statement to update the database. > here is the sample code wher i have done till now > > -------------------------- > <?php > // connect to the database :mysql > $host="localhost"; // Host name. > $dbuser="test"; // MySQL username. > $dbpassword="******"; // MySQL password. > $database="test"; // Database name. > mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to > thye database'); > mysql_select_db($database) or die("Unable to select database"); > $result = mysql_query("SELECT * FROM tbltest ORDER BY slno,empid"); > $i = 0; > > print "<form name='namestoupdate' method='post' action='tblupdate.php'> > \n"; > print "<tr>"; > print "<th nowrap> No</th>"; > print "<th nowrap> Emp No</th>"; > print "<th nowrap> Emp Name</th>"; > print "<th nowrap> Amount</th>"; > print "<th nowrap> Flg </th>"; > print "<th nowrap> Date</th>"; > > while ($rec = mysql_fetch_array($result)) { > print "<input type='hidden' name='slno[$i]' > value='{$rec['slno']}' />"; > print "<tr>"; > print "<p>{$rec['slno']} {$rec['empid']} {$rec['empname']} > {$rec['amt']} <input type='checkbox' name='{$rec['slno']}' > value='{$rec['flg']}'> <input type='text' size='25' name='rec[$i]' > value='{$rec['date']}' /></p> "; > ++$i; > } > print "</table>"; > print "<p><input type='submit' value='ClickallChkbox' /></p>"; > print "<input type='submit' value=' Clear' />"; > print "<p><input type='submit' value='Update' /></p>"; > print "</form>"; > mysql_close(); > ?> > ------------------------- > > > Thanki you very much in advance > > -evolu > I hope this s. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Thanks Jerry,
Thank you very muvh for your advise. you are a great teacher. Here is the code for populate and display check box data with check/d. and also i attaching a sample for update. display works ok. but the updated fails as i am not getting (POST) proper values to update all are blank. can you advise me some with some more hint. Thanks in Advance. --------------- display:tdisplay.php it works ok --------------------------------- <?php $host="localhost"; // Host name. $dbuser="***"; // MySQL username. $dbpassword="***"; // MySQL password. $database="test"; // Database name. mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to thye database'); mysql_select_db($database) or die("Unable to select database"); $result = mysql_query("SELECT * FROM tblsales ORDER BY slno,empid"); $i = 0; print "<form name='namestoupdate' method='post' action='tblupdaten.php'>\n"; while ($rec = mysql_fetch_array($result)) { print "<input type='hidden' name='slno[$i]' value='{$rec['slno']}' />"; print "<tr>"; print "<p> {$rec['slno']} {$rec['empid']} {$rec['empname']} {$rec['salesamt']} "; $checked = (($rec['flg'] ==1)?"CHECKED":""); print " <input type=\"checkbox\" NAME=\"flg[]\" VALUE=\"flg\" $checked/> "; print "<input type='text' size='25' name='rec[$i]' value='{$rec['date']}' /></p> "; ++$i; } print "</table>"; print "<p><input type='submit' value='Update' /></p>"; print "</form>"; mysql_close(); ?> ----------------------------------------- Now for the update:tblupdaten.php it fails get proper values in the arry all are blanks ---------------------------------------------- <?php $host="localhost"; // Host name. $dbuser="***"; // MySQL username. $dbpassword=""; // MySQL password. $database="test"; // Database name. mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to thye database'); mysql_select_db($database) or die("Unable to select database"); $size = count($_POST['slno']); $i = 0; while ($i < $size) { $slno= $_POST['slno'][$i];//echo "slno=$slno"; $empid= $_POST['empid'][$i];echo "empid=$empid"; $flg= $_POST['flg'][$i];echo "flg=$flg"; $sdate= $_POST['date'][$i];echo "date=$date"; $id = $_POST['id'][$i];$query = "UPDATE tblsales SET date = \"$sdate\" WHERE id = \"$id\" and slno = \"$slno\" LIMIT 1"; mysql_query($query) or die ("Error in query: $query"); print "$empid<br /><br /><em>Updated!</em><br /><br />"; ++$i; } mysql_close(); ?> ------------------------------------------------------------ -evolu |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
bangsundara@gmail.com wrote:
> Thanks Jerry, > > Thank you very muvh for your advise. you are a great teacher. > > Here is the code for populate and display check box data with check/d. > and also i attaching a sample for update. > display works ok. > but the updated fails as i am not getting (POST) proper values to > update all are blank. > can you advise me some with some more hint. > Thanks in Advance. > > --------------- > display:tdisplay.php it works ok > --------------------------------- > <?php > $host="localhost"; // Host name. > $dbuser="***"; // MySQL username. > $dbpassword="***"; // MySQL password. > $database="test"; // Database name. > mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to > thye database'); > mysql_select_db($database) or die("Unable to select database"); > $result = mysql_query("SELECT * FROM tblsales ORDER BY slno,empid"); > $i = 0; > print "<form name='namestoupdate' method='post' > action='tblupdaten.php'>\n"; > > while ($rec = mysql_fetch_array($result)) { > print "<input type='hidden' name='slno[$i]' > value='{$rec['slno']}' />"; > print "<tr>"; > print "<p> {$rec['slno']} {$rec['empid']} {$rec['empname']} > {$rec['salesamt']} "; > $checked = (($rec['flg'] ==1)?"CHECKED":""); > print " <input type=\"checkbox\" NAME=\"flg[]\" VALUE=\"flg\" > $checked/> "; > print "<input type='text' size='25' name='rec[$i]' > value='{$rec['date']}' /></p> "; > ++$i; > } > print "</table>"; > print "<p><input type='submit' value='Update' /></p>"; > print "</form>"; > mysql_close(); > ?> > ----------------------------------------- > > Now for the update:tblupdaten.php > it fails get proper values in the arry all are blanks > ---------------------------------------------- > <?php > $host="localhost"; // Host name. > $dbuser="***"; // MySQL username. > $dbpassword=""; // MySQL password. > $database="test"; // Database name. > mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to > thye database'); > mysql_select_db($database) or die("Unable to select database"); > $size = count($_POST['slno']); > $i = 0; > while ($i < $size) > { > $slno= $_POST['slno'][$i];//echo "slno=$slno"; > $empid= $_POST['empid'][$i];echo "empid=$empid"; > $flg= $_POST['flg'][$i];echo "flg=$flg"; > $sdate= $_POST['date'][$i];echo "date=$date"; > $id = $_POST['id'][$i];$query = "UPDATE tblsales SET date = \"$sdate\" > WHERE id = \"$id\" and slno = \"$slno\" LIMIT 1"; > mysql_query($query) or die ("Error in query: $query"); > print "$empid<br /><br /><em>Updated!</em><br /><br />"; > ++$i; > } > mysql_close(); > ?> > ------------------------------------------------------------ > > -evolu > You're close to what you need. No, the browser will not send info for checkboxes which are not checked - that's how HTML works. There are several ways to handle this. The one I prefer is is to use a hidden field to keep track of each value, i.e. echo "<input type='hidden' name='rec[]' value='rec{$slno}>' This gives you an array of recid values, each with the $slno of the row (replace your current hidden field with this) Then have the other fields based on this, i.e. echo "<input type='checkbox' name=flg[$slno]' value=1 $checked/> "; echo "<input type='text' size='25' name='text[$slno]' value='{$rec['date']}' /></p> "; And finally add one more field to contain the previous value of the checkbox: echo "<input type='hidden' name='flgval[$slno]' value={$rec['flg']}>" Now your $_POST might contain the following data: rec[0] = 5 rec[1] = 10 rec[2] = 20 rec[3] = 50 flg[5] = 1 flg[20] = 1 text[5] = My box was checked text[10] = My box was not checked text[20] = But my box was again text[50] = Mine wasn't! flgval[5] = 1 flgval[10] = 1 flgval[20] = 0 flgval[50] = 0 You have four rows of data; The first one had the checkbox checked and was not changed. The second one was changed from checked to unchecked. The third one was changed from unchecked to checked. The fourth one was unchecked and not changed. You can use code similar to: foreach ($_POST['recid'] as $recid) { $oldval = $_POST['flgval'][$recid]; $newval = isset($_POST['flg'][$recid] ? 1 : 0; if ($oldval <> $newval) // Flag has changed -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Thank you very much.Attaching worjking code at the end.
But still javascript I/F is not working, i.e when i click the check all chek box java script is able to put check mark on all thye populated check box already with or with out checked. Thanks for your advice and you are so nice and you are one among the best of best of JDS Computer Training Corp. (i only know you only at present) code: what it will do : It will read some info from table and populate and display data with edit date filed and check mark to modify and chek mark to delete the record. pending: javascript portion: check all chkbox, and depending on this enable and disable the edit field. please note No error check, no SQL injection avoidance, database conection is not good (used twice!!) but may be good for learner. taken from: Mr.Jerry Stuckle (Ms ??? i do not know) and Mr.Peter of canada (Google search) --------------------------------------- code for populate data :tdisplaynew.php ====================================== <html> <head> <script type="text/javascript"> <!-- function allcheck() { if (document.namestoupdate.allcheck.checked) { for(i=0; if (document.namestoupdate.elements[i].name == "flg[]") { document.namestoupdate.elements[i].checked = true; } } //--> </script> </head> <body> <?php // connecting to the DB better to use class so that we can use in later pg $host="localhost"; $dbuser="root"; $dbpassword=""; $database="test"; mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to thye database'); mysql_select_db($database) or die("Unable to select database"); // excuite the query $result = mysql_query("SELECT * FROM tblsales ORDER BY slno,empid"); // counter start to display data from db and hold the positions of an record $slno=0; // this is no relations with the table field better to use i or j print "<form name='namestoupdate' method='post' action='tblupdatenew.php'>\n"; Print " <p> No Emp_ID Name Amount ChkModify Date ChkDelete </p>"; while ($rec = mysql_fetch_array($result)) { //empid is the primary key echo "<input type='hidden' name='empid[$slno]' value={$rec['empid']} /> "; print "<tr>"; print "<p> {$rec['slno']} {$rec['empid']} {$rec['empname']} {$rec['salesamt']} "; $checked = (($rec['flg'] ==1)?"CHECKED":""); echo "<input type='checkbox' name=flg[$slno]' value=1 $checked/> "; //the only edit filed/s on the web page in this code echo "<input type='text' size='25' name='date[$slno]' value='{$rec['date']}' />"; echo "<input type='checkbox' name=flgdel[$slno]' value=1 /></p> "; ++$slno; } print "</table>"; print "<p><input type='button' name ='allcheck' value='ClickallChkbox' onclick='allcheck()' /></p>"; print "<p><input type='submit' value='Update' /></p>"; print "</form>"; mysql_close(); ?> </body> </html> ------------------------------------------------- ================================================= Code for update:tblupdatenew.php ================================ <?php // In this small code of PHP , i am doing the uodate and delete //when the Check box clicked in the previous display page // Do not rpeat the connection better to reuse the calls and require_once... $host="localhost"; $dbuser="root"; $dbpassword=""; $database="test"; mysql_connect($host,$dbuser,$dbpassword) or die('Unable to connect to the database'); mysql_select_db($database) or die("Unable to select database"); //print_r($_POST); // use it at the first time to get what values you can see in the $_POST $totchkd = count($_POST['empid']); // start a loop in order to update/delete each record $i = 0; while ($i < $totchkd) { $nflgval= $_POST['flg'][$i]; $nflgdel= $_POST['flgdel'][$i]; $eid = $_POST['empid'][$i]; $edate =$_POST['date'][$i]; $flgupdate =$flgdelelete= 0; if ($nflgval == '1') { $flgupdate = 1; echo "Inside the loop $i"; // update and print some info is required $queryu = "UPDATE `tblsales` SET `flg` = \"$flgupdate\" , `date` = \"$edate\" WHERE `empid` = '$eid' "; mysql_query($queryu) or die ("Error in query: $query"); print "<em>Update success</em><br /><br />"; } // here i am deleting if ($nflgdel== '1') { echo "<P>Inside the delete loop $i "; // Here delete if the delete flag is selected $queryd = "DELETE from `tblsales` WHERE `empid` = '$eid' "; mysql_query($query) or die ("Error in query: $query"); print "<em>Delete Success </em><br /><br /></p>"; } ++$i; } ?> ------------------------------------------------------- Thanks you all |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
bangsundara@gmail.com wrote:
> Thank you very much.Attaching worjking code at the end. > > But still javascript I/F is not working, > <head> > <script type="text/javascript"> > <!-- > function allcheck() { > if (document.namestoupdate.allcheck.checked) { > for(i=0; if (document.namestoupdate.elements[i].name == "flg[]") { > document.namestoupdate.elements[i].checked = true; > } > } > //--> > </script> There is a reason why people adopt conventions of indentation and brace spacing. function allcheck() { if (document.namestoupdate.allcheck.checked) { for(i=0; if (document.namestoupdate.elements[i].name == "flg[]") // I see no third term in here. it should be "for(a;b;c)" // And no IF statement is required, its implied in the // definition of the expression 'b' { document.namestoupdate.elements[i].checked = true; } } // Gosh. Three opening braces and only two closing ones..... wheres the last one } |
|
![]() |
| Outils de la discussion | |
|
|