I am using PHPStorm and I am trying to send my user's input from an html form to my database using PHP. My database seems to be getting the inputs since it creates a new id (which is the primary key and it is set to auto-increment) however, the fields are empty!
Here is my HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="i_createaccount" id="createaccount" action="info.php" method="post">
<h2 class="formtitle">Create an account</h2>
<input type="text" class="firstnlast" name="i_name" placeholder="First Name" required/>
<input type="text" class="firstnlast" name="i_last" placeholder="Last Name" required/>
<input id="txtEmail" type="email" name="i_email" placeholder="Email" required/>
<input type="submit" name="submit" class="next, button" value="Join Now"/>
</form>
</body>
</html>
Here is my info.php
<?php
if(empty($_SERVER['CONTENT_TYPE'])) {
$type = "application/x-www-form-urlencoded";
$_SERVER['CONTENT_TYPE'] = $type;
}
//Connecting to sql db.
define('DB_NAME', 'nabi_data');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Cant use ' . DB_NAME . ':' . mysql_error());
}
$message = '';
$i_name = (string)filter_input(INPUT_POST, 'i_name');
$i_last = (string)filter_input(INPUT_POST, 'i_last');
$i_email = (string)filter_input(INPUT_POST, 'i_email');
$sql = "INSERT INTO nabi_instructors (i_name, i_last, i_email) VALUES ('$i_name', '$i_last', '$i_email')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
mysql_close();
}
Related
I'm making a simple registration form for a test website and for some reason it isn't sending the data to the database, and I don't get an visual error. I've searched around for a fix but haven't found any that work.
This is basically my form (I only copied the form part of the page):
<form action="includes/insert.php" method="post">
<h3>Username</h3>
<input type="text" name="username">
<br>
<br>
<h3>Email Address</h3>
<input type="email" name="email">
<br>
<br>
<h3>Password</h3>
<input type="password" name="password">
<br>
<br>
<br>
<input id="submit-btn" type="submit" name="submit" value="Submit">
</form>
As you can see everything is as its suppose to be.
and this is my insert.php
<?
define('DB_NAME', 'logindb');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO `users` (`id`, `username`, `email`, `password`, `timestamp`) VALUES (NULL, '$username', '$email', '$password', CURRENT_TIMESTAMP)";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
Opening PHP tag is
<?php
Recent versions of PHP do not enable the short code syntax by default.
Use NOW() instead of CURRENT_TIMESTAMP.
I am new working on a project that has me connecting a database and a webpage and before doing the official database, I wanted to test a basic database.
I test to make sure everything is working by using phpMyAdmin. I can tell that I formed SOME kind of connection between my html and php, but that's where the issue is. whenever I press the submit button on my site, instead of getting a message telling me that everything works, I instead get a white page with all of my php code. I have tried making sure all my syntax is correct and all the variables are labeled correctly, but nothing is going through.
<!DOCTYPE php>
<?php
define('DB_NAME', 'training');
define('DB_USER', 'root');
define('DB_PASSWORD', '********');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!link){
die('Could not connect: ' .mysql_error);
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!db_selected){
die('Error: ' .mysql_error());
}
$Name = $_POST=['name'];
$Job = $_POST['job'];
$sql = "INSERT INTO employee (name,job) VALUES ('$Name', '$Job')";
if (!mysql_query($sql)){
die('Error: ' .mysql_error());
}
echo 'Success!';
mysql_close();
?>
<!DOCTYPE html>
<html>
<h1 style="text-align:center">Undergrad Sign-up</h1>
<style>
#body {background-color:#001b38;}
</style>
<body id="body" style="color:white;text-align:center" >
<form action="DBconnect.php" method="post" />
Name<br>
<input type="text" name="name" maxlength=20 required/>
<br>
Job<br>
<input type="text" name="job" maxlength=20 required/>
<br>
<button type="submit" value="submit">Submit</button>
</form>
</body>
</html>
Here's a pic of what i'm trying to achieve
http://oi60.tinypic.com/b9gf20.jpg
Heres all my code...
PHP FILE: contact_form.php
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($connection, DB_NAME);
if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}
echo 'Connected successfully';
if (isset($_POST['itemname'])){
$itm = $_POST['itemname'];
}else{
$itm = '';
}
if($_POST['mile']){
$mi = $_POST['mile'];
}else{
echo "Miles not received";
exit;
}
if($_POST['email']){
$email = $_POST['email'];
}else{
echo "email not received";
exit;
}
$sql = "INSERT INTO seguin_orders (itemname, mile, email) VALUES ('$itm', '$mi', '$email')";
if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
CONACT FORM: formz.php
<html>
<header>
</header>
<body>
<form action="/demoform/contact_form.php" class="well" id="contactForm" method="post" name="sendMsg" novalidate="">
<big>LOAD PAST ORDERS:</big>
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
</select>
</br>
<input type="text" required id="mile" name="mile" placeholder="Miles"/>
</br>
<input id="email" name="email" placeholder="Email" required="" type="text" value="demo#gmail.com" readonly="readonly"/>
</br>
<input id="name" name="itemname" placeholder="ITEM NAME 1" required="" type="text" />
</br>
<input type="reset" value="Reset" />
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>
Much thanks and appreciation for any time and help with this thanks.
<form>
<?php
while ($row = mysqli_fetch_assoc($result))
{
echo '<option value =" ' . $row['column'] . ' ">' . $row['column'] . '</option>';
};
?>
</form>
When you click select ,then send a ajax request to another php script to select database about the columns you want;then change it;like this:
<input name="email" />
<select onclick="ajaxFunc();" name="sel">
</select>
<script>
function ajaxFunc(){
var mail = $("input[name='email']").val();
$.post('anotherFile.php',{mail:mail},function(e){
$("select[name='sel']").append(e);
});
}
</script>
I have written a code for form which on submit opens the demo.php tag in the browser. I want the form to produce a (Thank you) alert on same page without redirecting to demo.php. Is it possible to embed a jquery notificaiton plugin? If yes then how?
<?php
error_reporting(0);
define('DB_NAME', 'form');
define('DB_USER', '******');
define('DB_PASSWORD', '*******');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo "<script type='text/javascript'>alert('It worked!')</script>";
exit();
$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['bio'];
$sql = "INSERT INTO form (name, email, bio) VALUES ('$value', '$value2','$value3')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>
<form method="post" action="demo.php" id="form" >
<center><legend><h1> Enter Your Details </h1></legend>
<div class="formelement">
<label for="name">Name: </label>
<input type="text" name="name" id="name" required/>
</div>
<div class="formelement">
<label for="email">Email-ID: </label>
<input type="email" name="email" id="email" required/>
</div>
<div class="formelement">
<label for="bio">Your Message: </label>
<textarea rows="1" cols="15" name="bio" id="bio"></textarea>
</div>
<div class="formelement">
<input type="submit" value="SUBMIT" class="submit"/>
</div>
</form>
try changing
<form method="post" action="demo.php" id="form" >
to
<form method="post" action="" id="form" >
it will post form to same file and so do not redirect to other page
and print alert inside-
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo "<script type='text/javascript'>alert('It worked!')</script>";
$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['bio'];
$sql = "INSERT INTO form (name, email, bio) VALUES ('$value', '$value2','$value3')";
$result=mysql_query($sql) or die('Error: ' . mysql_error());
if ($result)
{
echo "<script type='text/javascript'>alert('success!')</script>";
}
mysql_close();
You can use jquery ajax form submission .so that you can give alert using jquery
see here
I have a HTML form handled by php. The form has various text input fields and one of those asks the user to input a link containing http://.
The problem is when I submit a link like http://www.google.com/ the form fails badly by displaying random content from different php files on my website.
Here's a resume of my code:
<form action="add.php" method="post">
<input type="text" name="link" />
<input type="submit" value="submit" />
<?php
if (isset($_POST['link']))
{
$link = mysql_escape_string($_POST['link']);
mysql_query("INSERT INTO mytable(link) values($link)");
}
?>
Works just fine when not submitting a string that contains http://. Anyone has any idea why this happens?
Sidenote: You will find an mysqli_* based version further down.
Assuming you are connected to your DB, you did not wrap $link in VALUES with quotes.
Change this line:
mysql_query("INSERT INTO mytable(link) values( $link )");
^ ^ <- missing quotes
to:
mysql_query("INSERT INTO mytable(link) values('$link')");
Tested and working with this example: (form is set to self) and using http://www.google.com/ as an input and a few others containing http://
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASS', 'xxx');
define('DB_HOST', 'xxx');
$dbh = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$dbh)
{
die('Could not connect to database: ' . mysql_error());
}
$db_select = mysql_select_db(DB_NAME);
if(!$db_select)
{
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
if(isset($_POST['submit']) && !empty($_POST['link'])){
$link = mysql_real_escape_string($_POST['link']);
mysql_query("INSERT INTO mytable (link) values('$link')");
if(!mysql_query)
{
die("sorry");
}
else{ echo "Success"; }
mysql_close();
}
?>
<!DOCTYPE html>
<html>
<body>
<title></title>
<h3>Place your Link Below:</h3>
<form action="" method="post">
<input type="text" name="link" />
<input type="submit" name="submit" value="submit" />
</body>
</html>
Footnotes:
Do consider switching to mysqli_* functions with prepared statements or PDO. The mysql_* functions are deprecated and will be removed from future releases.
Here is an mysqli_* based version:
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASS', 'xxx');
define('DB_HOST', 'xxx');
$dbh = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
if(!$dbh)
{
die('Could not connect to database: ' . mysqli_error());
}
$db_select = mysqli_select_db($dbh,DB_NAME);
if(!$db_select)
{
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error());
}
if(isset($_POST['submit']) && !empty($_POST['link'])){
$link = mysqli_real_escape_string($dbh,$_POST['link']);
mysqli_query($dbh,"INSERT INTO mytable (link) values('$link')");
if(!mysqli_query)
{
die("sorry");
}
else{ echo "Success"; }
mysqli_close($dbh);
}
?>
<!DOCTYPE html>
<html>
<body>
<title>Home Page</title>
<h3>Please Place your Order Below:</h3>
<form action="" method="post">
<input type="text" name="link" />
<input type="submit" name="submit" value="submit" />
</body>
</html>