Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,286 members, 7,811,850 topics. Date: Sunday, 28 April 2024 at 09:16 PM

What Is Wrong With This Simple PHP - MYSQL Script - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / What Is Wrong With This Simple PHP - MYSQL Script (1435 Views)

How To Create A Simple Php/mysql Search / How To Create A Simple PHP Website With PBNL Anybody Can Do It! (naijazoom.com) / Another Simple Php/python/asp/etc Challenge (2) (3) (4)

(1) (Reply) (Go Down)

What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 8:47pm On Jul 31, 2013
please someone should help me solve this little problem, am trying to create a simple connection to mysql database, here is the code

<?php
$con = mysql_connect("localhost", "root", ""wink
or die("can't connect to mysql database".mysql_error());

$selectdb = mysql_select_db('nameofdb', $con)
or die("could not select database".mysql_error());

?>
<?php

$name = $_POST['un'];
$pass = $_POST['pd'];
$email = $_POST['em'];

$sql = "INSERT INTO reg(username, password, email) VALUES('$name', '$pass', $email')";
if(mysql_query($sql, $con)) {
echo "Congratulations, you are now a member <br/>
click<a href = 'index.html'>here</a> to login ";
}
else
echo "Registration failed";
mysql_close($con);
?>


i kept getting this error


Notice: Undefined index: un in C:\xampp\htdocs\signuptest\reg.php on line 11

Notice: Undefined index: pd in C:\xampp\htdocs\signuptest\reg.php on line 12

Notice: Undefined index: em in C:\xampp\htdocs\signuptest\reg.php on line 13
Registration failed



any suggestion will be greatly appreciated
Re: What Is Wrong With This Simple PHP - MYSQL Script by CODEEATER(m): 9:08pm On Jul 31, 2013
the values of dat $_POST, r dey defined in d script dat calls dis one?
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 9:10pm On Jul 31, 2013
CODE-EATER:
the values of dat $_POST, r dey defined in d script dat calls dis one?
thanks for replying. yes they were define in the registration form with a name attribute

here is how it looks
<input type="text" id="Editbox1" style="position:absolute;left:160px;top:58px;width:169px;height:18px;line-height:18px;z-index:3;" name="un" value="" placeholder="please enter a username">
Re: What Is Wrong With This Simple PHP - MYSQL Script by DualCore1: 9:15pm On Jul 31, 2013
$sql = "INSERT INTO reg(username, password, email) VALUES('$name', '$pass', $email')";


Look at the bolded part. Its missing an opening single quote.
It should be '$email' not $email'

That's just what I have spotted on initial inspection. There could be more issues. Just fix that one and run it.
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 9:20pm On Jul 31, 2013
Dual Core: $sql = "INSERT INTO reg(username, password, email) VALUES('$name', '$pass', $email')";


Look at the bolded part. Its missing an opening single quote.
It should be '$email' not $email'

That's just what I have spotted on initial inspection. There could be more issues. Just fix that one and run it.

thank you so much, i cant believe i missed that... here is the new error am getting


Notice: Undefined index: un in C:\xampp\htdocs\signuptest\reg.php on line 11

Notice: Undefined index: pd in C:\xampp\htdocs\signuptest\reg.php on line 12

Notice: Undefined index: em in C:\xampp\htdocs\signuptest\reg.php on line 13
Congratulations, you are now a member
clickhere to login
Re: What Is Wrong With This Simple PHP - MYSQL Script by DualCore1: 9:23pm On Jul 31, 2013
When you have "undefined index" notices cover the declaration in isset();

so
$name = $_POST['un'];

should be

if(isset($_POST['un'])){$name = $_POST['un'];}
else{die("Name field not being captured"wink;}

The else block, I have added as an error handle for troubleshooting.


P.S: Seun it is my strong hope that the new office and better work environment will give your developers the fortitude to fix the wink smiley that shows up in codes whether they are in the [code] tags or not.
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 9:31pm On Jul 31, 2013
Dual Core: When you have "undefined index" notices cover the declaration in isset();

so
$name = $_POST['un'];

should be

if(isset($_POST['un'])){$name = $_POST['un'];}
else{die("Name field not being captured"wink;}

The else block, I have added as an error handle for troubleshooting.


P.S: Seun it is my strong hope that the new office and better work environment will give your developers the fortitude to fix the wink smiley that shows up in codes whether they are in the [code] tags or not.



thanks once again, i tried it and got the error report, ie Name field not being captured. could it be that i the form is not functioning? what is the possible cause?
Re: What Is Wrong With This Simple PHP - MYSQL Script by DualCore1: 9:36pm On Jul 31, 2013
It means the html form is not communicating and passing post values to the php processor.

Please post your full html form code and your full php code.

By full form code I mean the code starting from <form> to </form> including the opening and closing form tags.



Also ensure your form's method attribute is "post".
i.e <form method="post" action="processor.php">
assuming your php script is in the same directory and called processor.php.
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 9:38pm On Jul 31, 2013
observation: before i added the isset, when i tried submitting the form, the entry is record/stored in my database with just the id, while the username, password and email remains blank
Re: What Is Wrong With This Simple PHP - MYSQL Script by DualCore1: 9:41pm On Jul 31, 2013
neo_welsh: observation: before i added the isset, when i tried submitting the form, the entry is record/stored in my database with just the id, while the username, password and email remains blank
That is expected, because the form values are not being captured... the reason you are getting undefied notices. You're only seeing the ID in your db because that field is an auto-incrementing field.
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 9:48pm On Jul 31, 2013
Dual Core: It means the html form is not communicating and passing post values to the php processor.

Please post your full html form code and your full php code.

By full form code I mean the code starting from <form> to </form> including the opening and closing form tags.



Also ensure your form's method attribute is "post".
i.e <form method="post" action="processor.php">
assuming your php script is in the same directory and called processor.php.



here is the form

<form name="Form1" method="post" action="reg.php" enctype="text/plain" id="Form1" onsubmit="return ValidateForm1(this)">
<div id="wb_Text1" style="position:absolute;left:41px;top:62px;width:68px;height:16px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">username:</span></div>
<div id="wb_Text2" style="position:absolute;left:47px;top:115px;width:68px;height:16px;z-index:1;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">password:</span></div>
<div id="wb_Text3" style="position:absolute;left:50px;top:174px;width:68px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">email:</span></div>
<input type="text" id="Editbox1" style="position:absolute;left:160px;top:58px;width:169px;height:18px;line-height:18px;z-index:3;" name="un" value="" placeholder="please enter a username">
<input type="password" id="Editbox2" style="position:absolute;left:163px;top:119px;width:169px;height:18px;line-height:18px;z-index:4;" name="pd" value="" placeholder="please enter a pasword">
<input type="text" id="Editbox3" style="position:absolute;left:168px;top:175px;width:169px;height:18px;line-height:18px;z-index:5;" name="em" value="" placeholder="please enter an email">
<input type="submit" id="Button1" name="login" value="login" style="position:absolute;left:195px;top:266px;width:96px;height:25px;z-index:6;">
</form>

and this is the script

<?php
$con = mysql_connect("localhost", "root", ""wink
or die("can't connect to mysql database".mysql_error());

$selectdb = mysql_select_db('wysiwyg', $con)
or die("could not select database".mysql_error());

?>
<?php
if(isset($_POST['un'])){$name = $_POST['un'];}
else{die("Name field not being captured"wink;}

$pass = $_POST['pd'];
$email = $_POST['em'];

$sql = "INSERT INTO reg(username, password, email) VALUES('$name', '$pass', '$email')";
if(mysql_query($sql, $con)) {
echo "Congratulations, you are now a member <br/>
click<a href = 'index.html'>here</a> to login ";
}
else
echo "Registration failed";
mysql_close($con);
?>
Re: What Is Wrong With This Simple PHP - MYSQL Script by DualCore1: 10:00pm On Jul 31, 2013
Hmmm, I can't spot anything that should throw up an error in your code except for the ValidateForm1() javascript function.

My suggestion remove onsubmit="return ValidateForm1(this)" from that opening form declaration and retry submitting the form.

This may sound dumb but are you entering any values into the name, password and email fields before submitting?

Let's know how it goes, me I'm going off now cuz I have to go out. I should be back in about 2 hours. Will try to check this thread up while i'm away if I can. Just update us and someone else may be around to help.
Re: What Is Wrong With This Simple PHP - MYSQL Script by yawatide(f): 10:25pm On Jul 31, 2013
This is why some people who come to me for reviews, advice, etc hate me:

Dude, I think before you even start to ask for a solution, you need to get back to the basics. Start by cleaning up your code:
a) Get rid of the inline styles in your html
b) Try not to use "or die" in your code - makes it vulnerable to hacks
c) Use better naming conventions for your form fields
d) Don't use "echo" to print out what the browser can do for you
e) Clean up the actual database insert workflow code

After you clean up the code, then come back for some help.
Re: What Is Wrong With This Simple PHP - MYSQL Script by nollyj: 1:16am On Aug 01, 2013
I tested your form below

<form name="Form1" method="post" action="reg.php" enctype="text/plain" id="Form1" onsubmit="return ValidateForm1(this)">
<div id="wb_Text1" style="position:absolute;left:41px;top:62px;width:68px;height:16px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">username:</span></div>
<div id="wb_Text2" style="position:absolute;left:47px;top:115px;width:68px;height:16px;z-index:1;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">password:</span></div>
<div id="wb_Text3" style="position:absolute;left:50px;top:174px;width:68px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">email:</span></div>
<input type="text" id="Editbox1" style="position:absolute;left:160px;top:58px;width:169px;height:18px;line-height:18px;z-index:3;" name="un" value="" placeholder="please enter a username">
<input type="password" id="Editbox2" style="position:absolute;left:163px;top:119px;width:169px;height:18px;line-height:18px;z-index:4;" name="pd" value="" placeholder="please enter a pasword">
<input type="text" id="Editbox3" style="position:absolute;left:168px;top:175px;width:169px;height:18px;line-height:18px;z-index:5;" name="em" value="" placeholder="please enter an email">
<input type="submit" id="Button1" name="login" value="login" style="position:absolute;left:195px;top:266px;width:96px;height:25px;z-index:6;">
</form>


with my php script

<?php

$input_form_error = array();

if(isset($_POST['login'])){

if(isset($_POST['un'])){

$name = filter_var($_POST['un'], FILTER_SANITIZE_STRING);
}
else{
$input_form_error[] = "Please enter a name";
}

if(isset($_POST['pd'])){

$password = filter_var($_POST['pd'], FILTER_SANITIZE_STRING);
}
else{
$input_form_error[] = "Please enter a password";
}

if(isset($_POST['em'])){

$email = filter_var($_POST['em'], FILTER_SANITIZE_STRING);
}
else{
$input_form_error[] = "Please enter a valid email address";
}

echo $name . " " . $password . " " . $email;
}

?>


While your form is not submitting the input field data is because of this attribute enctype="text/plain". Just remove it.

You can read more about it on this post in stackoverflow http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 10:53pm On Aug 01, 2013
nollyj: I tested your form below

<form name="Form1" method="post" action="reg.php" enctype="text/plain" id="Form1" onsubmit="return ValidateForm1(this)">
<div id="wb_Text1" style="position:absolute;left:41px;top:62px;width:68px;height:16px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">username:</span></div>
<div id="wb_Text2" style="position:absolute;left:47px;top:115px;width:68px;height:16px;z-index:1;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">password:</span></div>
<div id="wb_Text3" style="position:absolute;left:50px;top:174px;width:68px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:arial;font-size:13px;">email:</span></div>
<input type="text" id="Editbox1" style="position:absolute;left:160px;top:58px;width:169px;height:18px;line-height:18px;z-index:3;" name="un" value="" placeholder="please enter a username">
<input type="password" id="Editbox2" style="position:absolute;left:163px;top:119px;width:169px;height:18px;line-height:18px;z-index:4;" name="pd" value="" placeholder="please enter a pasword">
<input type="text" id="Editbox3" style="position:absolute;left:168px;top:175px;width:169px;height:18px;line-height:18px;z-index:5;" name="em" value="" placeholder="please enter an email">
<input type="submit" id="Button1" name="login" value="login" style="position:absolute;left:195px;top:266px;width:96px;height:25px;z-index:6;">
</form>


with my php script

<?php

$input_form_error = array();

if(isset($_POST['login'])){

if(isset($_POST['un'])){

$name = filter_var($_POST['un'], FILTER_SANITIZE_STRING);
}
else{
$input_form_error[] = "Please enter a name";
}

if(isset($_POST['pd'])){

$password = filter_var($_POST['pd'], FILTER_SANITIZE_STRING);
}
else{
$input_form_error[] = "Please enter a password";
}

if(isset($_POST['em'])){

$email = filter_var($_POST['em'], FILTER_SANITIZE_STRING);
}
else{
$input_form_error[] = "Please enter a valid email address";
}

echo $name . " " . $password . " " . $email;
}

?>


While your form is not submitting the input field data is because of this attribute enctype="text/plain". Just remove it.

You can read more about it on this post in stackoverflow http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible



Sorry guys for replying late, my account was ban for no reason. I want to say a big thank you to everyone that replied to my cry for help, i really appreciate it.
a special thanks to Dual Core and also to nollyj [/b]for pointing out [b]enctype="text/plain", everything started working immediately i remove enctype="text/plain". may God Bless you guys. happy new month.
Re: What Is Wrong With This Simple PHP - MYSQL Script by sisqology(m): 7:52am On Aug 03, 2013
Warris dis
Re: What Is Wrong With This Simple PHP - MYSQL Script by neowelsh(m): 12:02pm On Aug 03, 2013
sisqology: Warris dis
what does it look like?
Re: What Is Wrong With This Simple PHP - MYSQL Script by Kingsonly: 12:11pm On Aug 03, 2013
<?php

if(isset($_POST['this should be your send button name '])){

$name = $_POST['un'];}

$pass = $_POST['pd'];
$email = $_POST['em'];
if($_POST['un']=='' or $_POST['em']==''){ echo 'sorry space cant be left empty';}

else{
$sql = "INSERT INTO reg(username, password, email) VALUES('$name', '$pass', '$email')";
mysql_query($sql, $con)
echo "Congratulations, you are now a member <br/>
click<a href = 'index.html'>here</a> to login ";}

}

?>

(1) (Reply)

Benefits And Importance Of Digital Marketing: Beyond SEO And Adwords! / Finally!!! Google Adsense Congratulate Me With Smile / Steps To Add Ads.txt File To Blogger For Google Adsense

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 55
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.