Hello I am having some trouble inserting my form information onto my database. I am recieving the connected to database echo however when click on submit i get a 404 error. Any help would be greatly appreciated. Thank you in advance!
<?php
$host="localhost";
$dbuser="root";
$pass="******";
$dbname="learn";
$con = mysqli_connect ($host, $dbuser, $pass, $dbname);
if (mysqli_connect_errno ())
{
die ("Connection Failed!" . mysqli_connect_error());
}
else
{
echo "Connected to database {$dbname} ";
}
if(isset($_POST['submit']))
{
$fname=$_POST ['fname'];
$lname=$_POST ['lname'];
$email=$_POST ['email'];
$pswrd=$_POST ['pswrd'];
$sql = $con->query ("INSERT INTO users ( fname, lname, email, pswrd,)
VALUES ( ' {$fname} ' , ' {$lname} ' , ' {$email} ' , ' {$pswrd} ' , ')" );
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Signup</title>
<meta charset="utf-8">
<meta name="description" content="description of webpage">
<meta name="keywords" content="keywords go here">
<meta name="author" content="me">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/signup.css">
<link rel="index" href="index.php">
<link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
<div class="header">
<div id="logo">
<a href="index.php"><img src="img/logo.png" alt="logo" title="learnlogo"/></a>
</div>
</div>
<div id="signupform">
<form action="submit" method="post"><br>
<input type="text" name="fname" placeholder="First name">
<input type="text" name="lname" placeholder="Last name"><br><br>
<input type="text" name="email" placeholder="Email address">
<input type="password" name="pswrd" placeholder="Password"><br><br>
<input type"submit" value="Submit">
</form>
</div>
<?php
include ("inc/footer.php");
?>
remove action="submit"
from your <form>
tag. The action="{URL}" attribute "specifies where to send the form-data when a form is submitted" - you don't need it when sending back to the same url.
e.g. change:
<form action="submit" method="post"><br>
to:
<form method="post"><br>
below, action should be the name of your php file that handles the code.In this case, $_SERVER['PHP_SELF'] probably.
<div id="signupform">
<form action="submit" method="post"><br>
You should have your connection info in another file.
Create a file called Config.php as read only
<?
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysql_connect("localhost", "root", "******", "demo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysql_connect_error());
}
?>
Create a file called Insert.php
<?
include 'Config.php';
?>
<?
// Escape user inputs for security
$first_name = mysql_real_escape_string($link, $_POST['firstname']);
$last_name = mysql_real_escape_string($link, $_POST['lastname']);
$email_address = mysql_real_escape_string($link, $_POST['email']);
$pswr = mysql_real_escape_string($link, $_POST['pswrd']);
// attempt insert query execution
$sql = "("INSERT INTO users ( fname, lname, email, pswrd,)
VALUES ( ' {$fname} ' , ' {$lname} ' , ' {$email} ' , ' {$pswrd} ' , ')" );
if(mysql_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysql_error($link);
}
// close connection
mysql_close($link);
?>
Your Form:
<div id="signupform">
<form action="Insert.php" method="post"><br>
<input type="text" id="fname" name="fname" placeholder="First name">
<input type="text" id="lname" name="lname" placeholder="Last name"><br><br>
<input type="text" id="email" name="email" placeholder="Email address">
<input type="password" id="pswrd" name="pswrd" placeholder="Password"><br><br>
<input type"submit" value="Submit">
</form>
</div>