/    Sign up×
Community /Pin to ProfileBookmark

PHP Submit Button

Hi,

I am unable to make this code to work could anyone kindly to help please

<?php
$username = “root”;
$password = “”;
$hostname = “localhost”;

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;

//select a database to work with
$selected = mysql_select_db(“sgptt”,$dbhandle)
or die(“Could not select examples”);

if (isset($_POST[‘$submit’])) {
$SQL = “INSERT INTO useraccount (Username, Password, Firstname, Surname, Email, AccountType) VALUES (“”.$Username.””, “”.$Password.””, “”.$Firstname.””)”;
$result = mysql_query($SQL);
}
//close the connection
mysql_close($dbhandle);
?>
———————————-Html——————————————–
<form4 action=”insert.php” method=”post”>
<br><input id=”Username” name=”Username” type=”text” placeholder=”Username’abcd123′”><br>
<input id=”Password” name=”Password” type=”password” placeholder=”Password”><br>
<br><input id=”Firstname” name=”Firstname” type=”text” placeholder=”First Name”><br>
<br><input id=”Surname” name=”Surname” type=”text” placeholder=”Surname”><br>
<br><input id=”Email” name=”Email” type=”email” placeholder=”Email Address”><br>
<br><select id=”AccountType” name=”AccountType”>
<option value=”Student” selected=”selected”>Student</option>
<option value=”Staff”>Staff</option>
</select>
<input id=”regB” type=”submit”>
<input id=”reset” type=”button” value=”Reset”>
</form4>

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmFeb 08.2014 — Numerous errors with var names and your insistence on using only one type of quote (double & single work!) make this full of problems. BUT - what exactly is NOT working for you?
Copy linkTweet thisAlerts:
@ginerjmFeb 08.2014 — OK - took the time to do some work on your sample code. DESPITE THE FACT THAT MYSQL IS DEPRACATED AND SHOULD NOT BE USED (see mysqli or PDO) here is your cleaned up code.
[code=php]
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("sgptt",$dbhandle)
or die("Could not select examples");
if (isset($_POST['$submit']))
{
// $SQL = "INSERT INTO useraccount (Username, Password, Firstname, Surname, Email, AccountType) VALUES ("".$Username."", "".$Password."", "".$Firstname."")";
// Try this instead of above; note how single quotes make it much easier to type and to interpret later.
$SQL = "INSERT INTO useraccount (Username, Password, Firstname, Surname, Email, AccountType) VALUES ('$Username', '$Password', '$Firstname')";
// Your use of $Username is an error - var undefined.
// Your use of $Password and $Firstname are also errors. Same reason.
// Not sure, but your query expects 6 input values but you only supply 3. Another error.
$result = mysql_query($SQL);
// THEN add some error checking to ensure that your query ran!
if (!$result)
{
echo "Could not insert data - error msg is:<br> ".mysql_error();
exit();
}
if (mysql_affected_rows() == 0)
{
echo "No row inserted by query";
exit();
}
}
//close the connection
mysql_close($dbhandle);
?>
----------------------------------Html--------------------------------------------
<!-- WHAT IS A 'FORM4' TAG????? -->
<form4 action="insert.php" method="post">
<br>
<!-- YOU DON'T NEED THE ID= ATTRIBUTE ON EVERY SINGLE INPUT TAG -->
<input name="Username" type="text" placeholder="Username'abcd123'">
<br>
<input name="Password" type="password" placeholder="Password">
<br>
<br>
<input name="Firstname" type="text" placeholder="First Name">
<br>
<br>
<input name="Surname" type="text" placeholder="Surname">
<br>
<br>
<input name="Email" type="email" placeholder="Email Address">
<br>
<br>
<select name="AccountType">
<option value="Student" selected="selected">Student</option>
<option value="Staff">Staff</option>
</select>
<input type="submit" value='Submit'>
<input type="button" value="Reset">
</form4>
[/code]


Your html is setup to provide you with several values but your php is not grabbing them. Perhaps that is why your don't have enough values in your query statement.

I think that your 'reset' button will not do what you want it to do. I've never used it, but I think you want to say "type='reset'", not type='button'.

This makes your code easier to read and debug I think. You still have some work to go to complete it.

Most importantly though - when you finally figure out how to grab those inputs from your html with your php, you need to study up on protecting yourself from bad input. You need to edit the incoming values to ensure they are valid and are not containing things that will be harmful to your script/appl/server/database. I'm not the one to teach you that, but you do need to learn it!!!!

Good luck with your learning process.
Copy linkTweet thisAlerts:
@JpergegaauthorFeb 08.2014 — Thank you for the code ?
×

Success!

Help @Jpergega 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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