Jump to content

Recommended Posts

Which the best and faster from where the code ?!

To write general add function usable in all websites(include many parameters), Or To write private add function usable in only a one website (less parameters) and in another website change it

what your openion with this function ?!

 

function fun_add($tab,$id,$iman,$lenght,$error,$succ)

{

 

 

 

if ($id==0){//its insert function !

$res=("insert into $tab

()values()");

mysql_query($res);

$id=mysql_insert_id();

}//end if

// Get table Info

$res1=mysql_query("desc $tab ");

$c=0;

while ($row1=mysql_fetch_array($res1)){

$c=$c+1;

$fname[$c]=  $row1[0];

}

//2 to the first field acept id

$q="update $tab set  ";

for ($i=0; $i<$lenght ; $i++) {

$fi=$i+2;

$q.="$fname[$fi]= '".$iman[$i]."' ";

if($i!=$lenght-1)

$q.=",";

}

$q.=" where id = '".$id."'";

//print $q;

 

mysql_query($q) or die("not query");

if ( mysql_affected_rows()>0)

{

header("location:"."$succ");

exit;

}

else

{

header("location:"."$error");

exit;

}

}

 

//$iman =array("hi","iman",125,"wo");

//iman_add("test",2,$iman,4,"error.php?a=1","view.php");

 

 

 

no idea its hard to read as you didn't use the code tag(#)

 

OK after a quick look

$lenght isn't needed (use count),

and

$error,$succ i would handle on the returned page

so replace the header with return true/false and on the return redirect

 

of course the script can be improved that just after a quick review and that my personal view

 

function fun_add($tab,$id,$iman,$lenght,$error,$succ)
{



if ($id==0){//its insert function !
$res=("insert into $tab
()values()");
mysql_query($res);
$id=mysql_insert_id();
}//end if
// Get table Info
$res1=mysql_query("desc $tab ");
$c=0;
while ($row1=mysql_fetch_array($res1)){
$c=$c+1;
$fname[$c]=  $row1[0];
}
//2 to the first field acept id
$q="update $tab set  ";
for ($i=0; $i<$lenght ; $i++) {
$fi=$i+2;
$q.="$fname[$fi]= '".$iman[$i]."' ";
if($i!=$lenght-1)
$q.=",";
}
$q.=" where id = '".$id."'";
//print $q;

mysql_query($q) or die("not query");
if ( mysql_affected_rows()>0)
{
header("location:"."$succ");
exit;
}
else
{
header("location:"."$error");
exit;
}
}

//$iman =array("hi","iman",125,"wo");
//iman_add("test",2,$iman,4,"error.php?a=1","view.php");

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.