php - HTML POST and fetch array -
i have 2 problems here:
1. have problem value of checkbox
2. have problem mysql_fetch_array($variable, assoc); method --> data database varchar type
1. variable with, @ end 'cb', signified value checkbox of form. if checkbox checked, means row (ex: username) in select method database (ex: select username from...)
i receive error 1. undefined index: fonctioncb in c:\wamp\www\projet compte utilisateur\manip_liste.php on line 7 2. undefined variable: tab in c:\wamp\www\projet compte utilisateur\manip_liste.php on line 14 ...etc checkbox if not checked .. here php code
<?php $prep = ""; if(!$_post['username']) echo 'nom d\'utilisateur manquant'; if(($_post["usercb"]) && ($_post["suffixecb"]) && ($_post["fonctioncb"]) && ($_post["passwordcb"])){ $prep = " * "; $tab = "user suffixe site fonction passwd"; } else{ if($_post["usercb"]){ $prep += "username ,"; $tab += "user "; } if($_post["suffixecb"]){ $prep += "suffixe ,"; $tab += "suffixe "; } if($_post["passwordcb"]){ $prep += "password ,"; $tab += "passwd "; } if($_post["sitecb"]){ $prep += "siteweb ,"; $tab += "site "; } if($_post["fonctioncb"]){ $prep += "fonction "; $tab += "fx "; } }//else require("db_action.php"); //require in database connection. $bd = db_open(); $data = db_select($prep, $_post["username"]); //envoie des variables à afficher et du username pour le select echo "'$tab'"; echo "'$data'"; ?>
2. second error , got error fetch_array method error: warning: mysql_fetch_array() expects parameter 1 resource, string given in c:\wamp\www\projet compte utilisateur\db_action.php on line 68
here code method
function db_select($prep, $username){ $querycon = "select '$prep' info_compte username = '$username'"; if(!mysql_query($querycon)){ echo "il n\'y aucun '$username' dans la base de données"; $response = ""; } else{ while ($row = mysql_fetch_array(mysql_query($querycon), mysql_assoc)) printf("%s %s %s %s %s", $row["username"], $row["suffixe"], $row["password"], $row["siteweb"], $row["fonction"]); //the printf dont work } return $response; }//db_select
thank answer , specify i'm beginner in practice wants learn ..
for part 1, 1 problem may have trailing comma on end of $prep depending on columns end selecting. solve try below code (note i've added backticks around column names, it's practice not required). isset()
function checks if variable exists. trying access variables may not exist, hence errors.
<?php $prep = array(); $tab = ''; if(!isset($_post['username'])) echo 'nom d\'utilisateur manquant'; if(isset($_post["usercb"]) && isset($_post["suffixecb"]) && isset($_post["fonctioncb"]) && isset($_post["passwordcb"])){ $prep[] = "*"; $tab = "user suffixe site fonction passwd"; } else{ if(isset($_post["usercb"])){ $prep[] = "`username`"; $tab .= "user "; } if(isset($_post["suffixecb"])){ $prep[] = "`suffixe`"; $tab .= "suffixe "; } if(isset($_post["passwordcb"])){ $prep[] = "`password`"; $tab .= "passwd "; } if(isset($_post["sitecb"])){ $prep[] = "`siteweb`"; $tab .= "site "; } if(isset($_post["fonctioncb"])){ $prep[] = "`fonction`"; $tab .= "fx "; } }//else // line glues pieces of $prep array $prep = implode(',', $prep); require("db_action.php"); //require in database connection. $bd = db_open(); $data = db_select($prep, $_post["username"]); //envoie des variables à afficher et du username pour le select echo "'$tab'"; echo "'$data'"; ?>
for part 2 error, have assign return of mysql_query
variable , pass mysql_fetch_array
. don't pass query string. try this:
function db_select($prep, $username){ $username = mysql_real_escape_string($username); $querycon = "select $prep info_compte username = '$username'"; $response = mysql_query($querycon); if(!$response){ echo "il n\'y aucun '$username' dans la base de données"; $response = ""; } else{ while ($row = mysql_fetch_array($response, mysql_assoc)) foreach($row $item) { echo $item.' '; } } return $response; }//db_select
update:
note large addition part 1, i've removed single quotes around $prep in sql statement in part 2.
read on isset()
also, can consider using array_key_exists()
. i'm not sure on relative performance comparison of two.
Comments
Post a Comment