Jump to content

[SOLVED] The specification of the good code


omarh2005

Recommended Posts

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");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.