Updateing a SQL select populated form

Updateing a SQL select populated form

am 30.03.2008 09:16:17 von bangsundara

Dear Sir/Madam

As i am very new to PHP. i need some help 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

Re: Updateing a SQL select populated form

am 30.03.2008 17:57:35 von George Maicovschi

On Mar 30, 10:16 am, bangsund...@gmail.com wrote:
> Dear Sir/Madam
>
> As i am very new to PHP. i need some help 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.

Re: Updateing a SQL select populated form

am 30.03.2008 19:11:45 von Jerry Stuckle

bangsundara@gmail.com wrote:
> Dear Sir/Madam
>
> As i am very new to PHP. i need some help 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 help, you need to post what you've already tried so we
can help you with it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Updateing a SQL select populated form

am 05.04.2008 06:14:47 von bangsundara

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

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


test php


Display 10 records at a time


$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 "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "
IDnameagesexplaceflgmdate
".$row["id"]."".cnv_enc($row["name"], $enc_disp, $enc_db)."".$row["age"]."".cnv_enc($row["sex"], $enc_disp, $enc_db)."".cnv_enc($row["place"], $enc_disp, $enc_db)."".$row["flg"]."".$row["mdate"]."
";
mysql_close($conn);
?>


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

Re: Updateing a SQL select populated form

am 05.04.2008 20:52:10 von Jerry Stuckle

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
>
> -------------------
>
>
> test php
>
>
>

Display 10 records at a time


> > $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 "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> echo "";
> }
> echo "
IDnameagesexplaceflgmdate
".$row["id"]."".cnv_enc($row["name"], $enc_disp, $enc_db)."".$row["age"]."".cnv_enc($row["sex"], $enc_disp, $enc_db)."".cnv_enc($row["place"], $enc_disp, $enc_db)."".$row["flg"]."".$row["mdate"]."
";
> mysql_close($conn);
> ?>
>
>
> ------------------
>

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
==================

Re: Updateing a SQL select populated form

am 06.04.2008 07:15:57 von bangsundara

>>On 4月6日, 午前3:52, Jerry Stuckle 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

--------------------------
// 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 "


\n";
print "";
print " No";
print " Emp No";
print " Emp Name";
print " Amount";
print " Flg ";
print " Date";

while ($rec = mysql_fetch_array($result)) {
print " value='{$rec['slno']}' />";
print "";
print "

{$rec['slno']} {$rec['empid']} {$rec['empname']}
{$rec['amt']} value='{$rec['flg']}'> value='{$rec['date']}' />

";
++$i;
}
print "";
print "

";
print "";
print "

";
print "
";
mysql_close();
?>
-------------------------


Thanki you very much in advance

-evolu

Re: Updateing a SQL select populated form

am 06.04.2008 16:22:08 von Jerry Stuckle

bangsundara@gmail.com wrote:
>>> On 4月6日, 午前3:52, Jerry Stuckle 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
>
> --------------------------
> > // 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 "


> \n";
> print "";
> print " No";
> print " Emp No";
> print " Emp Name";
> print " Amount";
> print " Flg ";
> print " Date";
>
> while ($rec = mysql_fetch_array($result)) {
> print " > value='{$rec['slno']}' />";
> print "";
> print "

{$rec['slno']} {$rec['empid']} {$rec['empname']}
> {$rec['amt']} > value='{$rec['flg']}'> > value='{$rec['date']}' />

";
> ++$i;
> }
> print "";
> print "

";
> print "";
> print "

";
> print "
";
> mysql_close();
> ?>
> -------------------------
>
>
> Thanki you very much in advance
>
> -evolu
>

I hope this helps.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Updateing a SQL select populated form

am 12.04.2008 10:05:34 von bangsundara

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
---------------------------------
$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 "

action='tblupdaten.php'>\n";

while ($rec = mysql_fetch_array($result)) {
print " value='{$rec['slno']}' />";
print "";
print "

{$rec['slno']} {$rec['empid']} {$rec['empname']}
{$rec['salesamt']} ";
$checked = (($rec['flg'] ==1)?"CHECKED":"");
print " $checked/> ";
print " value='{$rec['date']}' />

";
++$i;
}
print "";
print "

";
print "
";
mysql_close();
?>
-----------------------------------------

Now for the update:tblupdaten.php
it fails get proper values in the arry all are blanks
----------------------------------------------
$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

Updated!

";
++$i;
}
mysql_close();
?>
------------------------------------------------------------

-evolu

Re: Updateing a SQL select populated form

am 12.04.2008 15:19:30 von Jerry Stuckle

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
> ---------------------------------
> > $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 "

> action='tblupdaten.php'>\n";
>
> while ($rec = mysql_fetch_array($result)) {
> print " > value='{$rec['slno']}' />";
> print "";
> print "

{$rec['slno']} {$rec['empid']} {$rec['empname']}
> {$rec['salesamt']} ";
> $checked = (($rec['flg'] ==1)?"CHECKED":"");
> print " > $checked/> ";
> print " > value='{$rec['date']}' />

";
> ++$i;
> }
> print "";
> print "

";
> print "
";
> mysql_close();
> ?>
> -----------------------------------------
>
> Now for the update:tblupdaten.php
> it fails get proper values in the arry all are blanks
> ----------------------------------------------
> > $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

Updated!

";
> ++$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 "
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 " $checked/> ";
echo " value='{$rec['date']}' />

";

And finally add one more field to contain the previous value of the
checkbox:

echo " 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
==================

Re: Updateing a SQL select populated form(Nearly finish)

am 20.04.2008 06:04:38 von bangsundara

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 help from:
Mr.Jerry Stuckle (Ms ??? i do not know)
and Mr.Peter of canada (Google search)

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

code for populate data :tdisplaynew.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 "

action='tblupdatenew.php'>\n";
Print "

No Emp_ID Name Amount ChkModify Date
ChkDelete

";
while ($rec = mysql_fetch_array($result)) {
//empid is the primary key
echo " value={$rec['empid']} /> ";
print "";
print "

{$rec['slno']} {$rec['empid']} {$rec['empname']}
{$rec['salesamt']} ";
$checked = (($rec['flg'] ==1)?"CHECKED":"");
echo "
";
//the only edit filed/s on the web page in this code
echo " value='{$rec['date']}' />";
echo "


";

++$slno;
}
print "";
print "

value='ClickallChkbox' onclick='allcheck()' />

";
print "

";
print "
";
mysql_close();
?>


-------------------------------------------------
=================================================

Code for update:tblupdatenew.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 "Update success

";
}
// here i am deleting
if ($nflgdel== '1')
{
echo "

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 "Delete Success

";
}
++$i;
}

?>


-------------------------------------------------------
Thanks you all

Re: Updateing a SQL select populated form(Nearly finish)

am 20.04.2008 13:36:35 von Courtney

bangsundara@gmail.com wrote:
> Thank you very much.Attaching worjking code at the end.
>
> But still javascript I/F is not working,


>
>

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

}