I am receiving an internal 500 error when trying to insert data from HTML form into MySQL database via PHP. I am hosting a site with an Apache 2 webserver on an Ubuntu server (droplet using DigitalOcean). PHP and MySQL are both installed on the server. HTML below and PHP below. I have tested with a PHP echo with the variable $name, which was successful, but I am thrown the error whenever I try to connect to mysql. I know 500 is an internal error, but am not sure where to go from here.
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>who this</title>
<link rel="stylesheet" type="text/css" href="STYLE.css">
</head>
<body>
<h1 id="header2">me</ h1>
<div id="back">
back
</div>
<div id="me-intro">
<p>tell me about yourself</p>
</div>
<div id="joke">
<form action="process.php" method="POST">
<div id="one">
<p>name: <input type="text" name="name" id='first'></p>
</div>
<div id="two">
<p>gender: <input type="text" name="gender" id='second'></p>
</div>
<div id="three">
<p>age: <input type="text" name = "age" id='third'></p>
</div>
<div id="four">
<p>occupation: <input type="text" name="occupation" id='fourth'></p>
</div>
<div id="five">
<p>education: <input type="text" name="education" id='fifth'></p>
</div>
<div id="six">
<p>car you drive: <input type="text" name="car" id='sixth'></p>
</div>
<div id="seven">
<p>phone number: <input type="text" name="phone" id="seventh"></p>
</div>
<div id="eight">
<p>address: <input type="text" name="address" id="eighth"></p>
</div>
<div id="nine">
<p>social security number: <input type="text" name="ssn" id="ninth">
</div>
<div id="ten">
<input type="submit">
</div>
</form>
</div>
</body>
</html>
process.php below
<?php
$conn = new mysqli("localhost", "root", "password_here", "database_here");
$name = $_POST["name"];
$gender = $_POST["gender"];
$age = $_POST["age"];
$occupation = $_POST["occupation"];
$education = $_POST["education"];
$car = $_POST["car"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$ssn = $_POST["ssn"];
if($conn -> connect_error) {
die("err: dis not working: " . $conn->connect_error);
}
$sql = "INSERT INTO table_here (name, gender, age, occupation, education, car, phone, address, ssn) VALUES ('$name', '$gender', '$age', '$occupation', '$education', '$car', '$phone', '$address', '$ssn')";
if($conn->query($sql) === TRUE){
echo "success";
} else {
echo "error: did not submit form to mysql";
}
$conn->close();
?>
Related
I just started learning PHP, and I wanted to see if I could make a form where the information would be stored in a database. To do this I am using phpMyAdmin. The problem occurs when I press the "Submit" button. I get an error that states the site is not working due to an HTTPS 405 error. Any help or guidance would be appreciated. Thankyou.
HTML
<!DOCTYPE html>
<html>
<!-- Head -->
<head>
<title></title>
</head>
<!-- Body -->
<body>
<h1 id="title">Info Form</h1>
<form action="info.php" method="POST">
<section id="firstName">
First Name:
<input type="text" placeholder="First Name" name="userFirstName" required>
<br><br>
</section>
<section id="lastName">
Last Name:
<input type="text" placeholder="Last Name" name="userLastName" required>
<br><br>
</section>
<section id="genderChoice">
Male:
<input type="radio" name="userGender" value="m" required>
Female:
<input type="radio" name="userGender" value="f" required>
<br><br>
</section>
<section id="submit">
<input type="submit" value="Submit">
</section>
</form>
</body>
</html>
PHP
$userFirstName = $_POST['userFirstName']; $userLastName = $_POST['userLastName']; $userGender = $_POST['userGender'];
$host = "127.0.0.1"; $dpUsername = "root"; $dpPassword = ""; $dpname = "form";
$conn = new mysqli($host, $dpUsername, $dpPassword, $dpname);
$INSERT = "INSERT Into info (userFirstName, userLastName, userGender) values($userFirstName, $userLastName, $userGender)";
Hi I have multiple check boxes name make and female I want that if a person select make and don't select female it input nothing or false in database now if I don't select example: female it return nothing in female row but it also get a error
Undefined index: Female in /storage/sdcard1/www/3/signup/index.php on line 11
but I don't want this error and I want the if female not select it insert false and don't show error I don't want the selected check box value to be true because it is wrote as on in database
Code index.php
<?php
if(isset($_POST['submit'])){
$conn = new PDO("sqlite: sign.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$Email = $_POST['Email'];
$First = $_POST['First'];
$Last = $_POST['Last'];
$Password = $_POST['Password'];
$Male = $_POST['Male'];
$Female =$_POST['Female'];
$Dateofb = $_POST['Dateofb'];
require_once 'imp.php';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css" >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post" >
<div class="container" >
<div class="form" >
<input type="email" class="first" id="Email" name="Email" placeholder="Email" required="required">
<input type="text" class="second" id="First" name="First" placeholder="First name" required="required">
<input type="text" class="last" id="Last" name="Last" placeholder="Last name" required="required">
<input type="password" class="pass" name="Password" id="Password" placeholder="Password" required="required">
<div class="day" >
<p class="bd" >Birthday Date:</p>
<input type="date" class="date" id="Dateofb" name="Dateofb" >
</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="Male" name="Male">
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="Female" name="Female" >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit" name="submit" >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
Code imp.php
<?php
$sql = "INSERT INTO signup (`Email`, `First`, `Last`, `Password`, `Male`, `Female`, `Dateofb`) VALUES ('$Email', '$First', '$Last', '$Password', '$Male', '$Female', '$Dateofb');";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn->connection = null;
?>
The problem is that only checked boxes are submitted.
You need to check if a box's name is set in $_POST and IF NOT:
manually assign a value you wish to use instead
I.e. use this short hand if/else
$Female = (isset ($_POST['Female'])) ? 1 : 0;
I want to insert data from a form into a database.
I've searched for a long time but can't figure out what I am doing wrong. Any help will be appreciated
HTML
<!DOCTYPE html>
<html>
<head>
<title>PHP insertion</title>
<link href="css/insert.css" rel="stylesheet">
</head>
<body>
<div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Book Information</h2>
</div>
<form action="new 12.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h3>Enter the Details</h3>
<label>Access number</label>
<input class="input" name="access" type="text" value=""><br>
<label>Title</label>
<input class="input" name="title" type="text" value=""><br>
<label>Author</label>
<input class="input" name="author" type="text" value=""><br>
<label>Edition</label>
<input class="input" name="edition" type="text" value=""><br>
<label>Publisher</label>
<input class="input" name="publisher" type="text" value=""><br>
<input class="submit" name="submit" type="submit" value="Submit"><br>
</form>
</div>
</div>
</body>
</html>
PHP
<?php
$link = mysqli_connect("localhost", "root", "admin", "library");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "INSERT INTO library (access,title,author,edition,publisher) VALUES ('$access','$title','$author','$edition','$publisher')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
$access is undefined. You need to refer to $_POST['access'] to get the value of your POSTed form. Same for all other fields.
First of all, I strongly recommend to make a PDO conection like:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
Second: I would put id to all of the inputs
Third: You´re missing the $_POST["value"]So you are not sending information
The html should look like:
<!DOCTYPE html>
<html>
<head>
<title>PHP insertion
</title>
<link href="css/insert.css" rel="stylesheet">
</head>
<body> <div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Book Information</h2>
</div>
<form action="new 12.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h3>Enter the Details</h3>
<label>Access number</label> <input class="input" name="access" id="access" type="text" value=""><br>
<label>Title</label> <input class="input" name="title" id="title" type="text" value=""><br>
<label>Author</label> <input class="input" name="author" id="author" type="text" value=""><br>
<label>Edition</label> <input class="input" name="edition" id="edition" type="text" value=""><br>
<label>Publisher</label> <input class="input" name="publisher" id="publiser" type="text" value=""><br>
<input class="submit" name="submit" type="submit" value="Submit"><br>
</form>
</div>
</div>
</body>
</html>
And the php like this:
<?php
try {
$conn = new PDO("mysqli:server = yoursever; Database = yourdatabase", "user", "pass");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to Server.");
die(print_r($e));
}
$access= $_POST['access'];
$title= $_POST['title'];
$author= $_POST['author'];
$edition= $_POST['edition'];
$publisher= $_POST['publisher'];
$sql = "INSERT INTO library (access, title, author, edition, publisher) VALUES (?,?,?,?,?)";
$stmti= $conn->prepare($sql);
$stmti->execute([$access, $title, $author, $edition, $publisher]);
if ($stmti->error){
echo "ERROR";
}
else{
echo "Records inserted successfully.";
}
$conn->close();
?>
This is one safe way to insert info in your server to prevent SQL inyection, please read How does the SQL injection from the "Bobby Tables" XKCD comic work? to understand better how to prevent inyection into your server
Installed Apache2 and uncommented php7 module with: https://websitebeaver.com/set-up-localhost-on-macos-high-sierra-apache-mysql-and-php-7-with-sslhttps
Location of apache config in my machine:
/etc/apache2
Installed MySQl with homebrew according to : https://tecadmin.net/install-mysql-macos/#
Locations of MySQl on my machine is:
mysql is /usr/local/bin/mysql
mysql is /usr/local/mysql/bin/mysql
Wrote unique mysql tables under same database for two unique html registration forms written on Visual Studio Code. Live server extension on VS Code allows browser to display and navigate html/css but after I submit data on signup.html, my interpreters.php code is automatically returned as a download with unknown server script injection.
signup.html file displays on browser
!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="signup.css">
<link rel="stylesheet" type="text/css" href="navigationbar.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign up with ...</title>
</head>
<header>
<div id="menu-content">
<div class="navbar">
<ol>
<li> Home </li>
<li> For Professionals </li>
<li> Request Service </li>
</ol>
</div>
</div>
</header>
<body>
<form name="signup" action="interpreters.php" method="get" style="border:1px solid #ffff">
<div class="container">
<h1><center>Professional Interpreter Registration</center></h1>
<label for="first">First Name: </label>
<input type="text" placeholder="Your name..." name="first" required>
<label for="last">Last Name:</label>
<input type="text" placeholder="Your last name..." name="last" required>
<label for="industry"><b> Industry: </b></label>
<input type="text" placeholder="Your industry..." name="industry" required>
<label for="type"><b> Service Type: </b></label>
<input type="text" placeholder="Description:Translation of legal documents Mandarin and Arabic/Interpret Italian through video only." name="industry" required>
<label for="email">Email:</label>
<input type="text" placeholder="email" name="email" required>
<div class="clearfix">
<button type="submit" class="signupbtn name="submit">Sign Up</button>
</div>
</div>
</form>
</body>
</html>
The action that the html is linked to is the following interpreters.php
<?php
// php code to Insert data into mysql database from input text
if(isset($_GET ['insert'] ) )
{
$hostname = "localhost";
$username = "root";
$password = "......";
$databaseName = "registrations";
// get values form input text and number
$last = $_GET ['last'];
$first = $_GET['lfirst'];
$industry = $_GET['industry'];
$type = $_GET['type'];
$email = $_GET['email'];
// connect to mysql database using mysqli
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql query to insert data
$query = "INSERT INTO `interpreters`(`last`, `first`, `industry`, 'type', 'email') VALUES ('$last','$first','$industry', '$type', '$email' )";
$result = mysqli_query($connect,$query);
// check if mysql query successful
if($result)
{
echo 'Data Inserted';
}
else
{
echo 'Data Not Inserted';
}
mysqli_free_result($result);
mysqli_close($connect);
}
?>
<html>
<body>
Thank you, <?php echo $_POST['first']; ?><br />
We will contact you at <?php echo $_POST['email']; ?><br />
<br />
</body>
</html>
I am having trouble with a some php code. I was able to get the code to work on my test website. I am now trying to register a user with a popup register menu. When I enter the information and click on my register button nothing is sent to my database table.
Heres my code.
<!doctype html>
<html lang="en">
<head>
<title>untitled.com</title>
<meta name="description" content="">
<link rel="stylesheet" type="text/css" href="frontPage.css">
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script src="js/general.js" type="text/javascript">
</script>
</head>
<body>
<h1> untitled </h1>
<h2> description of website</h2>
<div id="wrapper">
<nav id="nav">
<ul id="navigation">
<a class="button" href="" >Login</a>
<div class="popup">
CLOSE
<form>
<P><span class="title">Username</span> <input name="" type="text" /></P>
<P><span class="title">Password</span> <input name="" type="password" /></P>
<P><input name="" type="button" value="Login" /></P>
</form>
</div>
</ul>
</nav>
<?php
require('db.php');
if (isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];;
$query = "INSERT into `users` (username, password, email, city, state, zip, phone) VALUES ('$username', '".md5($password)."', '$email', '$city', '$state', '$zip', '$phone' )";
$result = mysql_query($query);
if($result){
echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<a class="Regbutton" href="#" >Register</a>
<div class="Regpopup">
CLOSE
<form>
<P><span class="title">Username</span> <input name="username" type="text" /></P>
<P><span class="title">Password</span> <input name="password" type="password" /></P>
<P><span class="title">Email</span> <input name="email" type="email" /></P>
<P><span class="title">City</span> <input name="city" type="text" /></P>
<P><span class="title">State</span> <input name="state" type="text" maxlength="2" /></P>
<P><span class="title">zip</span> <input name="zip" type="text" maxlength="5" /></P>
<P><span class="title">phone</span> <input name="phone" type="text" maxlength="15"/></P>
<P><input type="submit" name="submit" value="Register" /></P>
</form>
</div>
<?php } ?>
</body>
</html>
To get the value from input field by post method, it is important to define form method. Example
<form action="" method="POST">
</form>