Complete Newbie to PHP, but trying to do a simple form submit to a MySQL Database. I keep getting the Notice: Undefined index: warning on my form input variables. What could be issue here. The form is on a seperate PHP page submitting to the process.php file.
Form Code:
<form name="example" id="example" method="POST" action="process.php" enctype="multipart/form-data">
<p>
<label for="first_name">First Name:</label>
<input size="20" name="first_name" id="first_name" />
</p>
<p>
<label for="city">city</label>
<input size="20" name="city" id="city" />
</p>
<p>
<label for="state">state</label>
<input size="20" name="state" id="state" />
</p>
<p>
<label for="zip">zip</label>
<input size="20" name="zip" id="zip" />
</p>
<p>
<label for="email">e-mail</label>
<input size="20" name="email" id="email" />
</p>
<p>
<label for="form_upload">file</label>
<input size="40" type="file" name="form_data" id="form_data" />
</p>
<p>
<input type="submit" value="Submit form" name="submit" />
</p>
</form>
Process.php
<?php
mysql_connect("localhost", 'root', '') or die('Could not connect:' . mysql_error());
mysql_select_db("practice") or die(mysql_error());
mysql_query("CREATE TABLE user (id INT(5)NOT NULL AUTO, name VARCHAR(30), email VARCHAR(40), city VARCHAR(40), state VARCHAR(3), zip INT(5) )");
if (isset($_POST)) {
$name = $_POST['first_name'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
}
mysql_query("INSERT INTO user VALUES('$name', '$email', '$city', '$state', '$zip')");
?>
i guess you should change this
if (isset($_POST)){
and give an index like that
if (isset($_POST['submit'])){
and also
change the query to
mysql_query("INSERT INTO user (name , email, city,state, zip)
VALUES('$name', '$email', '$city', '$state', '$zip')" );
and you should escape your variables like that before inserting them.
$name = mysql_real_escape_string($name) ;
// and so on
last thing is why you create table everytime you submit ? you should check if exist .
Related
<!--This is the html form code-->
<div class="modal-body">
<form id="modal-form" accept-charset="UTF-8" method="POST" action="signUp.php" data-remote="true" >
<p><label for="firstName"><small>First Name:</small></label><br />
<input type="text" id="firstName" name="firstName" required="required" /></p><br />
<p><label for ="lastName"><small>Last Name:</small></label><br />
<input type="text" id="lastName" name="lastName" required="required" /></p><br />
<p><label for="email"><small>Email: </small></label><br />
<input type="email" name="email" required="required"/></p><br />
<input id="modal-form-submit" type="submit" name="submit" class="btn btn-success"/>
</form>
`
<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
require("includes/config.php");
require("includes/connect.php");
if(isset($_POST['submit'])){
$query="INSERT INTO mb_emaillist (ID, firstName, lastName, email) VALUES (null, '$firstName', '$lastName', '$email')";
$result=mysqli_query($db, $query);
if(!$result)
die("SELECT error: " .mysqli_error($db));
if($result){
print"Thank you $firstName $lastName for signing up with MyBrunch!";
}
}
mysqli_close($db);
?>`
I am looking to get a success message delivered in a modal after my form was successfully sent to my database. Right now the information is sent but, nothing happens.
Wow, just pick a PHP Framework! Don't work like this even for a small project.
Among so many options, I recommend Laravel...
Pick the 4.2 release, it's the simplest!
BTW: Your print call at line 16, lack of parenthesis.
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 8 years ago.
I am using wamp server and trying to create simple sign up page with html forms and php script; but the problem is whenever I am hitting on submit button on html page, it is directly showing the entire content of php file in next browser instead of executing the php file.
the following is my form code(index.php)
<form id="login" action="register.php" method="post">
<p>
<label for="first name" >First Name</label>
<input type="text" name="fname" value="" />
</p>
<p>
<label for="last name" >Last Name</label>
<input type="text" name="lname" value="" class="radius2" />
</p>
<p>
<label for="gender" >Gender</label>
<input type="text" name="gender" />
</p>
<p>
<label for="username" >Email</label>
<input type="text" name="username" />
</p>
<p>
<label for="password" >Password</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" name="submit">Login</input>
</p>
</form>
the following is my php code
$bd = mysqli_connect("localhost", "root","yash1991","shaunak") or die("Could not connect database");
echo" hello1";
mysqli_query($bd,"INSERT INTO users (fname, lname, gender, email_id, username, password) VALUES ("$fname", "$lname", "$gender", "$username", "$password")");
mysqli_close($bd);
?>
I suppose that you running WAMP server, and stored your files (index.php & register.php) in folder inside "C:\wamp\www\" and you access your index page using url "127.0.0.1/stack/index.php"
Check your index.php file with the following code:
<form id="login" action="register.php" method="post">
<p>
<label for="First Name">First Name</label>
<input type="text" name="fname" value="" />
</p>
<p>
<label for="last Name">Last Name</label>
<input type="text" name="lname" value="" class="radius2" />
</p>
<p>
<label for="gender">Gender</label>
<input type="text" name="gender" />
</p>
<p>
<label for="username">Email</label>
<input type="text" name="username" />
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password" />
</p>
<p>
<input type="submit" name="submit" value="Login" />
</p>
</form>
Check your register.php file with the following code:
<?php
session_start();
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$gender=$_POST['gender'];
$username=$_POST['username'];
$password=$_POST['password'];
echo "hello ";
$bd= mysqli_connect("localhost","root","yash1991","shaunak") or die ("Could not connect DB");
$try = mysqli_query($bd, "INSERT INTO users (`fname`, `lname`, `gender`, `email_id`, `password`) VALUES ('$fname', '$lname', '$gender', '$username', '$password')");
if($try === false)
{
echo '<br />error - ';
echo mysqli_error($bd);
} else {
echo '<br />all good';
}
mysqli_close($bd);
?>
I am wondering which one are errors. I've tried to check mysql and nothing inserted into my database.
First of all, my HTML code are like this
<form action="registerAction" method="POST">
<p class="titleRegister"> Login Details </p>
<!-- login details -->
<p> <label for="emailAddress" class="inputField" > Email Address : </label> </p>
<p> <input id="emailAddress" class="registerField" name="ename" required="required" type="text" placeholder="Your email address"/> </p>
<p> <label for="password" class="inputField" > Password : </label> </p>
<p> <input id="password" class="registerField" name="pwd" required="required" type="password" placeholder="Your password"/> </p>
<p> <label for="password" class="inputField" > Confirmation Password : </label> </p>
<p> <input id="password" class="registerField" name="mpwd" required="required" type="password" placeholder="Confirmation password" onBlur="pwdCompare()"/> </p>
<!-- personal details -->
<p class="titleRegister"> Personal Details </p>
<!-- hidden to insert db -->
<input name="registerID" type="hidden"/>
<input name="pic" type="hidden"/>
<p>
<label for="socialTitle" class="inputField" > Title : </label>
<div class="radio">
<input type="radio" name="sTitle" value="mr"> Mr
<input type="radio" name="sTitle" value="mrs"> Mrs
<input type="radio" name="sTitle" value="ms"> Ms
</div>
</p>
<p> <label for="firstName" class="inputField" > First Name : </label> </p>
<p> <input id="firstName" class="registerField" name="fname" required="required" type="text" placeholder="Your first name"/> </p>
<p> <label for="lastName" class="inputField" > Last Name : </label> </p>
<p> <input id="lastName" class="registerField" name="lname" required="required" type="text" placeholder="Your last name"/></p>
<p> <label for="mainAddress" class="inputField" > Main Address : </label> </p>
<p> <input id="mainAddress" class="registerField" name="address" required="required" type="text" placeholder="Your main address"/> </p>
<p> <label for="countryName" class="inputField" > Country : </label> </p>
<?php
include 'dbconnect.php';
echo "<select class=\"selectCSS\" name=\"country\">";
$country = "SELECT DISTINCT * FROM geo_country ORDER BY country";
$showCountry = mysqli_query($mysqli, $country);
while($countryRow = mysqli_fetch_assoc($showCountry))
{
$country = htmlspecialchars ($countryRow['country']);
$countryCode = $countryRow['countryCode'];
echo "<option value=\"$country\">$country</option>\n";
}
echo "</select>";
?>
<p> <label for="cityName" class="inputField" > City : </label> </p>
<?php
include 'dbconnect.php';
echo "<select class=\"selectCSS\" name=\"city\">";
$city = "SELECT DISTINCT * FROM geo_country INNER JOIN geo_city ORDER BY city WHERE geo_country.countryCode = geo_city.countryCode";
$showCities = mysqli_query($mysqli, $city);
while($cityRow = mysqli_fetch_assoc($showCities))
{
$city = htmlspecialchars ($cityRow['city']);
$countryCode = $cityRow['countryCode'];
echo "<option value=\"$city\">$city</option>\n";
}
echo "</select>";
?>
<p> <label for="postalCode" class="inputField" > Postal Code : </label> </p>
<p> <input id="postalCode" class="registerField" name="pcode" required="required" type="text" placeholder="Your postal code"/> </p>
<p> <input class="registerButton" type="submit" value="REGISTER"> </p>
</form>
and my php action come here:
<?php
include 'dbconnect.php';
if ($_POST['pwd']!= $_POST['mpwd']) {
echo("Oops! Password did not match! Try again. ");
}
$register_ID = $_POST['registerID'];
$socialTitle = $_POST['sTitle'];
$firstName = ucfirst(strtoupper($_POST['fname']));
$lastName = ucfirst(strtoupper($_POST['lname']));
$emailAddress = htmlspecialchars($_POST['ename']);
$mainAddress = htmlspecialchars($_POST['address']);
$registerCity = $_POST['city'];
$registerCountry = $_POST['country'];
$postalCode = htmlspecialchars($_POST['pcode']);
$profilePic = $_POST['pic'];
$registerPassword = $_POST['pwd'];
$check = "SELECT * FROM register_user where emailAddress = '$emailAddress'";
$checkTitle = mysqli_query($mysqli,$check);
if (mysqli_num_rows($checkTitle) > 0) {
header("Location: register?error=The name of email has already been taken");
} else {
$insertSQL =
"INSERT INTO register_user ('registerID', 'socialTitle', 'firstName', 'lastName', 'emailAddress', 'mainAddress', 'registerCity', 'registerCountry', 'postalCode', 'profilePic', 'registerPassword')
VALUES ('$register_ID', '$socialTitle', '$firstName', '$lastName', '$emailAddress', '$mainAddress', '$registerCity', '$registerCountry', '$postalCode', '$profilePic', '$registerPassword')";
$queryResult = mysqli_query($mysqli,$insertSQL);
if($queryResult) {
echo "SUCCESS";
echo "<p> Name : $emailAddress </p>";
echo "<p> Detail : $fname </p>";
echo "<p> BACK </p>";
}
}
?>
The results are nothing come out on the new html page and neither in DB. Can you check it out please? Thanks.
You're using the wrong identifiers for your columns, being (single) quotes '.
('registerID', 'socialTitle', 'firstName', 'lastName', 'emailAddress', 'mainAddress', 'registerCity', 'registerCountry', 'postalCode', 'profilePic', 'registerPassword')
change that to:
(registerID, socialTitle, firstName, lastName, emailAddress, mainAddress, registerCity, registerCountry, postalCode, profilePic, registerPassword)
or use backticks.
(`registerID`, `socialTitle`, `firstName`, `lastName`, `emailAddress`, `mainAddress`, `registerCity`, `registerCountry`, `postalCode`, `profilePic`, `registerPassword`)
Using or die(mysqli_error($mysqli)) to mysqli_query() would have shown you the error.
Plus, unless the form action is an index file in a folder called registerAction or a mod rewrite:
it would need to be
<form action="registerAction.php" method="POST">
so, check that. Just an insight.
I would also like to note that your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements, they are much safer.
Not 100% sure about it, but try changing your html.
This:
<form action="registerAction" method="POST">
To:
<form action="registerAction.php" method="POST">
Assuming registerAction is the name of you php file..
I am trying to insert data into a database mysql phpAdmin.
My webhost is 000webhost.
My connection to mysql database code:
<?PHP
$mysql_host = "mysql2.000webhost.com";
$mysql_database = "*********";
$mysql_user = "********";
$mysql_password = "**********";
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
if (!$dbcon) {
die('error connecting to database');
}
echo ('You have connected successfully');
?>
My insert data code:
<?PHP
if (isset($_POST['submitted'])) {
include('connect_mysql.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
if (!mysql_query($dbcon, $sqlinsert)) {
die('error inserting new record');
} // end of nested if statement
$newrecord = "1 record added to the database";
}
?>
<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<h1>Insert Data into DB</h1>
<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>
Instead of letting me put it into the database it brings me to this page
http://error404.000webhost.com/?
try change
<label>First Name: <input type="text name="fname" /></label>
<label>Last Name: <input type="text name="lname" /></label>
to
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
and also insert query and connection to
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
First change below code:
$dbcon = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
To :
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
Then change below code:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('fname', 'lname')";
To:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
can u please change this line?
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text name="fname" /></label>
<label>Last Name: <input type="text name="lname" /></label>
</fieldset>
to
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
you are setting wrong attributes so its not getting the values..
after that, u have to update your sql query:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
It DID have to be mysqli
my problem was the - had to be changed to an _ on <form method="post" action="insert-data.php">
working code:
<?PHP
if (isset($_POST['submitted'])) {
include('connect_mysql.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
if (!mysqli_query($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "1 new record added to the database";
}
?>
<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<h1>Insert Data into DB</h1>
<form method="post" action="insert_data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>
I would like to get some user data using a php form and store it in a mysql database , trouble is, the page simply refreshes when I submit the form.
Here is my php form:
<form id="companyform" name="companyform" method="post" action="index3.php" data-ajax="false">
<b>To enlist your business fill in the form below:</b>
<p>
<label for="name">Company Name:</label>
<input type="text" name="companyname" id="companyname" data-mini="true"/>
</p>
<p>
<label for="name">Company Address:</label>
<input type="text" name="companynaddress" id="companynaddress" data-mini="true"/>
</p>
<p>
<label for="textfield">Tel No.:</label>
<input type="text" name="tel" id="tel" data-mini="true"/>
</p>
<p>
<label for="textfield">Fax No.:</label>
<input type="text" name="fax" id="fax" data-mini="true"/>
</p>
<p>
<label for="textfield">Email:</label>
<input type="text" name="email" id="email" data-mini="true"/>
</p>
<p>
<label for="textfield">Website Address:</label>
<input type="text" name="website" id="website" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Name:</label>
<input type="text" name="contactname" id="contactname" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Number:</label>
<input type="text" name="contactnumber" id="contactnumber" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Email:</label>
<input type="text" name="contactemail" id="contactemail" data-mini="true"/>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Submit" />
</p>
</form>
and here is my database connection code:
<?php
if (array_key_exists('submit', $_POST)) {
$con = mysql_connect("host", "user", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("botswanasearchdb", $con);
$companyname = $_POST['companyname'];
$companyaddress = $_POST['companyaddress'];
$tel = $_POST['tel'];
$fax = $_POST['fax'];
$emailid = $_POST['emailid'];
$website = $_POST['website'];
$contactname = $_POST['contactname'];
$contactnumber = $_POST['contactnumber'];
$contactemail = $_POST['contactemail'];
// prepare the SQL query
$sql = "INSERT INTO businessuser (companyname, companyaddress, tel, fax, emailid, website, contactname, contactnumber, contactemail) VALUES ('$companyname', '$companyaddress', '$tel', '$fax', '$emailid', '$website', '$contactname', '$contactnumber', '$contactemail')";
mysql_close($con);
}
?>
This is your form tag:
<form id="companyform" name="companyform" method="post" action="index3.php" data-ajax="false">
So you are submitting the form to index3.php (the action attribute). According to your comment index3.php contains your form and that is why the form refreshes when you submit it. You are basically reloading your form on form submit.
You need to submit the form to your php script that contains the php code you posted.
Edit: If everything is on the same page, you can do something like:
if (array_key_exists('submit', $_POST))
{
// your code
// show thank you message
}
else
{
// show form
}
Another edit: As you are using the deprecated mysql_* functions and not escaping the data, you have an sql injection whole and a ' character in your data will break your query. You should switch to PDO / mysqli and prepared statements. And always add error handling.