PDA

View Full Version : best way to do multiple updates, I think?


student101
07-07-2008, 09:59 AM
What would be the best way to have one page do all the updates / inserts and deletes?
Like so: (or with arrays?)

$var = "$_GET['value']";
// match contents in $var
switch($var){
case 'string1': // does the contents of the var match the case
$result = mysql_query("UPDATE example SET age='form value' WHERE age='db value'") or die(mysql_error());
break;

case 'string2':
$result = mysql_query("UPDATE example SET age='form value' WHERE age='db value'") or die(mysql_error());
break;

case 'string3':
$result = mysql_query("UPDATE example SET age='form value' WHERE age='db value'") or die(mysql_error());
break;
}
Just an idea, any other ideas would be great.
Cheers

davidj
07-07-2008, 11:17 AM
you can be better than that

create 1 function which does them all and just pass in the query as an argument

your starting to break through the confines of procedural code and your looking at ways to streamline and reuse code elegantly

the answer is in OOP methods and classes

student101
07-07-2008, 11:34 AM
In response to your response I found these: http://www.phpfreaks.com/tutorials
They mention classes more than functions, why or how would this 1 function help me? (I'll have to find more tuts on functions first.)

Cheers

davidj
07-07-2008, 12:43 PM
you have one function which queries the db then spits out the result which you can manipulate

you can call the function everytime you need it like ...


$SQL = "SELECT * FROM TABLE WHERE FIELD = 'stuff'";
$result = Querydb($SQL);


$result will contain the result from the Query you submitted.

Querydb() is the function which you will have to write and include into every page you need it

a function can be even better than this

you could actually build a table inside the function and output that already formated.

////////////////////

here is an example i wrote a while ago

its a bit long winded but it will give you an idea of whats possible

http://www.dreamweaverclub.com/forum/showthread.php?t=26141&highlight=class

student101
07-07-2008, 12:56 PM
Having this, still means that you would need as many as you need.

$SQL = "SELECT * FROM TABLE WHERE FIELD = 'stuff'";
$result = Querydb($SQL);
$SQL2 = "SELECT * FROM TABLE2 WHERE FIELD = 'stuff2'";
$result = Querydb($SQL2);
Looking at the class.php - it's like a module or .bas file in VB.
It only holds the needed parts, interesting - never thought PHP could do this(you know what thought did...)

I will have to play around.

Thank you.

davidj
07-07-2008, 01:04 PM
remember that you can be too clean with your code

in 6 months time when you comeback to add a mod you dont want to be looking around trying to reverse engineer your code because you forgot where everything is

Always comment everything for you and others for future trouble shooting

Keep it clean but simple. Dont over complicate it by trying to over streamline it. You do need the SQL on the page as you should only need to just glance at the page to workout what it does

student101
07-07-2008, 01:15 PM
Will keep that in mind.
I don't think that my code was ever complicated though, it should be second nature to comment the code anyway.

student101
07-07-2008, 02:25 PM
In terms of security;
Could I use the code created from dreamweaver concerning updates, deletes and inserts or is it better to use the mysql_real_escape_string and stripslashes from PHP?

Cheers

davidj
07-07-2008, 03:09 PM
dont know what dreamweaver uses but i would do this yourself

i dont want you to ever use anything dreamweaver uses


mysql_real_escape_string() This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.


http://uk2.php.net/mysql_real_escape_string

student101
07-07-2008, 03:30 PM
Cool thanks,
off to start the with the idea and put what I've learnt to use.

Cheers