/    Sign up×
Community /Pin to ProfileBookmark

$db->query not working.. is it a sql error or something else?

I have tried tweaking this code in every way imaginable (to me at least) with no success.. the query does not insert the data into my db so my form still does not work.. help???

[code=php]
include(‘inc/db.php’);
$error;
if($_SERVER[‘REQUEST_METHOD’]==’POST’){
$info[‘fname’] = stripslashes(htmlspecialchars($_POST[‘fname’]));
$info[‘lname’] = stripslashes(htmlspecialchars($_POST[‘lname’]));
$info[‘username’] = stripslashes(htmlspecialchars($_POST[‘username’]));
$info[’email’] = stripslashes(htmlspecialchars($_POST[’email’]));
$info[‘password1’] = stripslashes(htmlspecialchars(sha1($_POST[‘password1’])));
$info[‘password2’] = stripslashes(htmlspecialchars(sha1($_POST[‘password2’])));
$info[‘addr_line1’] = stripslashes(htmlspecialchars($_POST[‘addr_line1’]));
$info[‘addr_line2’] = stripslashes(htmlspecialchars($_POST[‘addr_line2’]));
$info[‘state_prov’] = stripslashes(htmlspecialchars($_POST[‘state_prov’]));
$info[‘city’] = stripslashes(htmlspecialchars($_POST[‘city’]));
$info[‘country’] = stripslashes(htmlspecialchars($_POST[‘country’]));
if($info[‘addr_line2’]==”ADDRESS LINE 2″){
$info[‘addr_line2′]=””;
}
if (!isset($error)) {
foreach( $info as $value ){
if( stripos($value,’Content-Type:’) !== FALSE ){
$error = “There was a problem with the information you entered.”;
} else
if($value!=$info[‘addr_line2′] && $value==””){
$error=”All sections must be completed”;
}
}
} else if(!isset($error) &&
!preg_match(“/^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/”, $info[’email’])){
$error=”Email is invalid”;

} else if(!isset($error) && strlen($info[‘password1’])<8){
$error=”Password must be 8 or more characters.”;

} else if(!isset($error) && $info[‘password1’]!=$info[‘password2’]){
$error=”Passwords do not match. Please try again.”;

} else if(!isset($error)){
$addr = $info[‘addr_line1’].” “.$info[‘addr_line2’].” “.$info[‘city’].” “.$info[‘state_prov’].” “.$info[‘country’];
try{
$db->query(“INSERT INTO members (fname, lname, username, email, pass, addr) VALUES (‘”.$info[‘fname’].”‘,'”.$info[‘lname’].”‘,'”.$info[‘username’].”‘,'”.$info[’email’].”‘,'”.$info[‘password1’].”‘,'”.$info[‘addr’].”‘)”);
}catch(Exception $e){
$error = “Could not execute query”;
}
}
}
[/code]

I have looked at the db access info and all the creds are correct so I’m clueless as to whats going on here.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 13.2014 — Are you getting any errors? If not, the the first thing I'd check is if you are actually getting to that code, starting by seeing if you get into the first if() block (error_log() or just echo something out), and work your way down through the if/else logic to see what is actually being executed.
Copy linkTweet thisAlerts:
@NogDogJan 13.2014 — Ooh, wait, trying changing your [B][FONT=Courier New]if(!isset($error))[/FONT][/B] checks to [B][FONT=Courier New]if(empty($error))[/FONT][/B], since it looks like you initialize that variable before the first if() condition.
Copy linkTweet thisAlerts:
@thewebiphyerauthorJan 14.2014 — if found out that the part it's not getting past is the email validation section... I'm horrible with regular expressions so I tried googling regex's for an email and none of the result that looked the most legit worked... i also tried using this function: http://www.linuxjournal.com/article/9585?page=0,3 but it also does not work

so now the conditional section looks like this:
[code=php]
if($info['addr_line2']=="ADDRESS LINE 2"){
$info['addr_line2']="";
}
if (empty($error)) {
foreach( $info as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error = "There was a problem with the information you entered.";
} else
if($value!=$info['addr_line2'] && $value==""){
$error="All sections must be completed";
}
}
} else if(empty($error) && !validEmail($info['email'])){
$error="Email is invalid";

} else if(empty($error) && strlen($info['password1'])<8){
$error="Password must be 8 or more characters.";

} else if(empty($error) && $info['password1']!=$info['password2']){
$error="Passwords do not match. Please try again.";

} else if(empty($error)){
$addr = $info['addr_line1']." ".$info['addr_line2']." ".$info['city']." ".$info['state_prov']." ".$info['country'];
try{
$db->query("INSERT INTO members (fname, lname, username, email, pass, addr) VALUES ('".$info['fname']."','".$info['lname']."','".$info['username']."','".$info['email']."','".$info['password1']."','".$info['addr']."')");
}catch(Exception $e){
$error = "Could not execute query";
}
}
[/code]
×

Success!

Help @thewebiphyer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...