Re: [PHP] Re: fetch then put record?
On 10-Jun-2003 Jean-Christian Imbeault wrote: > [reply to a personal email posted here for the benefit of all :)] > > > This bugs me because my db has 125 fields and it will be a very long > sql string! > > I bet! > > > The form page generates form contents by using a while loop. > > > > How would you build the sql string from the form page? > > Use a while loop ;) Name the GET or POST vars the same as the field > names in the DB. Then you could use something like (I say like b/c this > won't work, it's just an idea): > > $sql = "update table A SET "; > while (list($fieldName, $value) == each($_POST)) { >$sql .= " $fieldName='$value', "; > } > > This won't work because there will be POST values passes in that are not > part of your form data. Oh, and there will be a trailing "," you need to > trim off ... > > Just a quick idea. You can make it a little smarter: //refetch the old row ... $qry="SELECT * FROM tbl WHERE id=" .$_POST['id']; $r=mysql_query($qry); $row=mysql_fetch_array($r); unset($chgflds); foreach($row as $fld => $val) { if (isset($_POST[$fld]) && ($_POST[$fld] != $val)) { $chgflds[] = "$fld='" .$_POST[$fld] ."'"; } } $update='UPDATE tbl SET ' .implode(', ', $chgflds) .'WHERE id=' .$_POST['id']; mysql_query($update); Regards, -- Don Read [EMAIL PROTECTED] -- It's always darkest before the dawn. So if you are going to steal the neighbor's newspaper, that's the time to do it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: fetch then put record?
[reply to a personal email posted here for the benefit of all :)] > Are you sure you can't? Yes. > Could ASP do something that MySQL/SQL can't? You confusing apples and oranges ... ASP is not a database. You should be comparing ASP to PHP. So the short answer is PHP does not have an equivalent function to ASP's update() or whatever it is. > That is very strange. No it isn't. These are different languages. > I wonder what mode ASP is accessing DB which lets you set record variables then paste them into > it? A very bad mode I would think. Very dangerous. But I can see how it is convenient. > Perhaps it just does an update in the background. Of course. No you understand. ASP is using a convenience method that does all that ugly SQL updating in the background. As far as I know PHP does not have such functionality. > This bugs me because my db has 125 fields and it will be a very long sql string! I bet! > The form page generates form contents by using a while loop. > > How would you build the sql string from the form page? Use a while loop ;) Name the GET or POST vars the same as the field names in the DB. Then you could use something like (I say like b/c this won't work, it's just an idea): $sql = "update table A SET "; while (list($fieldName, $value) == each($_POST)) { $sql .= " $fieldName='$value', "; } This won't work because there will be POST values passes in that are not part of your form data. Oh, and there will be a trailing "," you need to trim off ... Just a quick idea. -- Jean-Christian Imbeault -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: fetch then put record?
Steve B. wrote: Hello, In ASP you can set records fields then call a dbupdate function. There is no such function in PHP. It appears mysql only supports update with the UPDATE query? Huh ... MySQL is a database. It understands SQL. dbupdate() is an ASP function ... don't blame MySQL if it doesn't understand ASP ;) The only way to update data in MySQL is to use SQL, and in SQL to update data you use UPDATE queries :) Here is my code: $dbrec=getrec($result); This is not PHP code. There is no function called getrec(). If you want to access a MySQL database using PHP you will also need to learn SQL. You need to learn how to write SQL update queries ... I suggest that you read the PHP online manual and learn all about the MySQL functions. Read this section and then try again: http://jp.php.net/manual/en/ref.mysql.php Jean-Christian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php