I am having problem in this following code.It says undefined index on line 83.The second problem is that there is a huge gap between the text fill the required form and the input textbox of the form during output.Please help me out.The code is posted below.
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$firstnameErr = $lastnameErr = $emailErr = "";
$firstname = $lastname = $email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["firstname"]))
{
$firstnameErr = "Name is required";
}
else
{
$firstname = test_input($_POST["firstname"]);
}
if (empty($_POST["lastname"]))
{
$lastnameErr = "Name is required";
}
else
{
$lastname = test_input($_POST["lastname"]);
}
if (empty($_POST["email"]))
{
$emailErr = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div text align =center><h1>Eventous Info</h1></div>
<h3>Fill the Required Form:</h3>
<p><span class="error">*required field</span></p>
<table>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<tr><?php// echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<td>Firstname:</td>
<td><input type="text" name="firstname" ></td>
<td><span class="error">* <?php echo $firstnameErr;?></span></td><br><br>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" ></td>
<td><span class="error">* <?php echo $lastnameErr;?></span></td><br><br>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
<td><span class="error">* <?php echo $emailErr;?></span></td><br><br>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="number"><td><br><br>
</tr>
</table>
<input type="submit" >
</form>
<?php
$con = mysql_connect("localhost","ashu123","bangalore");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("evantus", $con);
$sql="INSERT INTO employee (firstname, lastname, email, phone )
***LINE-83***
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[number]')";
$sql = "select * from employee";
$query = mysql_query( $sql );
echo "<table>";
echo "<tr><th>firstname</th>";
echo "<th>lastname</th>";
echo "<th>email</th>";
echo "<th>phone</th></tr>";
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[firstname]</td>";
echo "<td>$row[lastname]</td>";
echo "<td>$row[email]</td>";
echo "<td>$row[phone]</td></tr>";
}
echo "</table>";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
</body>
</html>
Your form has invalid html code. In short, just use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Firstname:</td>
<td><input type="text" name="firstname" ></td>
<td><span class="error">* <?php echo $firstnameErr;?></span><br /><br /></td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" ></td>
<td><span class="error">* <?php echo $lastnameErr;?></span><br /><br /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
<td><span class="error">* <?php echo $emailErr;?></span><br /><br /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="number"><br /><br /><td>
<td></td>
</tr>
</table>
<input type="submit" >
</form>
More about it:
1. After opening the <table> element, next one must be table row, so <form> must be a wrapper to your table.
2. You have placed breaks after closing the </td> tag, which is wrong - they should be inside table cell.
3. Ref: undefined index, guess it is only a warning that you are using the $_POST variable that does not exist.
I feel your insert query is not right, try the query below.
$sql="INSERT INTO employee (firstname, lastname, email, phone ) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."','".$_POST['number']."')";
Related
<?php include("header.php")?>
<?php include("menu.php")?>
<div id="registrationPage">
<div id="registrationDiv" ></div>
<fieldset id="registrationFieldPos">
<legend><h3>Register</h3></legend>
<form id="registrationForm" action="registrationaction.php" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>First Name :</label></td>
<td><input type="text" name="fname" /></td>
</tr>
<tr>
<td><label>Last Name :</label></td>
<td><input type="text" name="lname" /></td>
</tr>
<tr>
<td><label>Username :</label></td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td><label>Password :</label></td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><label>Confirm Password :</label></td>
<td><input type="password" name="passwordconfirm" /></td>
</tr>
<tr>
<td><label>Email :</label></td>
<td><input type="email" name="email" /></td>
</tr>
<tr>
<td><label>Image :</label></td>
<td><input type="file" name="fileUpload" /></td>
</tr>
<tr>
<td><label>Country :</label></td>
<td>
<select name="country">
<?php
$connection = mysqli_connect('localhost', 'root', '', 'mutetistore') or die('connection error'. mysql_error());
mysqli_select_db($connection, 'mutetistore');
$sql = "SELECT * FROM apps_countries" ;
$results = mysqli_query($connection, $sql);
while($result = mysqli_fetch_array($results)):;
?>
<option value=" <?php echo $result['country_name']; ?> "> <?php echo $result['country_name'];?> </option>
<?php endwhile; ?>
</select>
</td>
</tr>
<tr>
<td><label>Languages :</label></td>
<td>
<label>English <input type="checkbox" name="Languages[]" value = "English" /></label>
<label>French<input type="checkbox" name="Languages[]" value = "French" /></label>
<label>Swahili<input type="checkbox" name="Languages[]" value = "Swahili" /></label>
</td>
</tr>
<tr>
<td><label>Gender:</label></td>
<td>
<label>Male <input type="radio" name="gender" value = "male"/></label>
<label>Female</label><input type="radio" name="gender" value = "female"/>
</td>
</tr>
<tr>
<td><input type="submit" name="save" value = "registered"/></td>
</tr>
</table>
</form>
</fieldset>
<div id="divEnd">
</div>
</div>
<?php include("footer.php")?>
<?php
require('databaseconn.php');
if(isset($_POST['save']) ) {
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$username = $_POST['username'];
$password = $_POST['password'];
$passwordconfirm = $_POST['passwordconfirm'];
$country = $_POST['country'];
$gender = $_POST['gender'];
$Languages = $_POST['Languages'];
$imagename = $_FILES['fileUpload']['name'];
$imagesize = $_FILES['fileUpload']['size'];
$imagetmp = $_FILES['fileUpload']['tmp_name'];
if(empty( $firstname)) {
echo "please enter username";
}else if(empty( $lastname)) {
echo "please enter lastname";
}else if(empty( $username)) {
echo "please enter username";
}else if(empty( $password)) {
echo "please enter password";
}else if(empty( $password !== $passwordconfirm)) {
echo "password do not match";
}else if(empty( $country)) {
echo "please select your country ";
}else if(empty( $gender)) {
echo "please select your gender ";
}else if(empty( $imagename)) {
echo "please select image";
}else {
$uploadFolder = "Uploads/";
$filename = rand(1000,100000)."-".$imagename;
$filenameUpload = move_uploaded_file($imagetm, $uploadFolder, $filename);
$sql = "INSERT INTO `register` (`id`, `firstname`, `lastname`, `username`, `password`, `country`, `gender`, `language`, `imageName`, `imageSize`, `imageTemp`)
VALUES (NULL, '$firstname', '$lastname', '$username', '$password', '$country', '$gender', '$Languages', '$filenameUpload', '$imagesize', '$imagetmp')";
}
}
?>
<?php echo $_POST["fname"]; ?><br>
<?php echo $_POST["lname"]; ?><br>
<?php echo $_POST["username"]; ?><br>
<?php echo $_POST["password"]; ?><br>
<?php echo $_POST["passwordconfirm"]; ?><br>
<?php echo $_POST["country"]; ?><br>
<?php echo $_POST["gender"]; ?><br>
<?php echo $_POST["password"]; ?><br>
<?php echo $_POST["passwordconfirm"]; ?><br>
<?php echo $_POST["country"]; ?><br>
This code is giving me headache. Could someone spot the error? I have tried it for a day without a solution. I want it to submit data to database (image ,checkbox,radio,etc). I want it to put all the selected checkboxs to database. I will later learn about the implode, exlode, to add commas to the code.
First two lines:
<?php include("header.php")?> <---- End the statement with ;
<?php include("menu.php")?> <---- Here too!
On Other Lines:
while($result = mysqli_fetch_array($results)):; <---What is this? It should be { //Code here
and The closing of while loop should be this! ->} not <?php endwhile; ?>
Here:
}else if(empty( $password !== $passwordconfirm)) { // Your Operation should be != and not !==
Maybe your id should be declared as an Auto-increment. And not to be added as NULL.
Youre full of headaches.. hahaha
i am a beginner and am trying to make a form validation with PHP. i want to check if one of the inputs is empty, the form says that the empty input is required.
I show the php script behind.
<?php
$titleErr = $authorErr = $keywordsErr = $contentErr = "";
$title = $author = $keywords = $content = "";
if (empty($_POST["submit"])) {
if(empty($_POST["title"])){
$titleErr = "title is required";
}
if(empty($_POST["author"])){
$authorErr = "author name is required";
}
if(empty($_POST["keywords"])){
$keywordsErr = "keywords are required";
}
if(empty($_POST["content"])){
$contentErr = "This field is required";
}
}
?>
<form method="post" action="insert_post.php">
<table width="600" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6"><h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="38">
<span style="color:red;"><?php echo $titleErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author" size="38">
<span style="color:red;"><?php echo $authorErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="38">
<span style="color:red;"><?php echo $keywordsErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td align="right">Post Content</td>
<td><textarea name="content" cols="30" rows="15"></textarea>
<span style="color:red;"><?php echo $contentErr; ?></span>
</td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include("includes/connect.php");
if(isset($_POST['submit'])){
$title = $_POST['title'];
$date = date('d-m-Y');
$author = $_POST['author'];
$keywords = $_POST['keywords'];
$content = $_POST['content'];
$image = $_FILES['image'] ['name'];
$image_tmp = $_FILES['image'] ['tmp_name'];
move_uploaded_file($image_tmp, "../images/$image");
$query = "INSERT INTO posts (post_title, post_date, post_author, post_keywords, post_image, post_content) VALUES('$title', '$date', '$author', '$keywords', '$image', '$content')";
$result = mysqli_query($conn, $query);
if($query){
echo"<center><h1>Post Published Succesfully!</h1></center>";
}
}
?>
The problem is i want to stop the scrit if the inputs are empty but i can't use functions like: exit() and break;.
and if i submit, the form sends empty values to the database :C.
how can i solve this problem?
We make use of session variables which temporarily stores data locally. To use sessions we must always start sessions by session_start() at the start of every page to be able to access the variables. Now we can store data and pass it to whatever page we want to without having to send it - we just store it and then grab it.
index.php
<?php
session_start();
$titleErr = $authorErr = $keywordsErr = $contentErr = "";
$title = $author = $keywords = $content = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["title"])){
$titleErr = "title is required";
}
if(empty($_POST["author"])){
$authorErr = "author name is required";
}
if(empty($_POST["keywords"])){
$keywordsErr = "keywords are required";
}
if(empty($_POST["content"])){
$contentErr = "This field is required";
}
if(!empty($_POST["title"]) && !empty($_POST["author"]) && !empty($_POST["keywords"]) && !empty($_POST["content"])){
$_SESSION["title"] = $_POST["title"];
$_SESSION["author"] = $_POST["author"];
$_SESSION["keywords"] = $_POST["keywords"];
$_SESSION["content"] = $_POST["content"];
$_SESSION["image"] = $_FILES["image"];
header("location: insert_post.php");
exit();
}
}
?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="600" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6"><h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="38" value="<?php if(isset($_POST['title'])){ echo $_POST['title']; }; ?>">
<span style="color:red;"><?php echo $titleErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author" size="38" value="<?php if(isset($_POST['author'])){echo $_POST['author']; }; ?>">
<span style="color:red;"><?php echo $authorErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="38" value="<?php if(isset($_POST['keywords'])){echo $_POST['keywords']; }; ?>">
<span style="color:red;"><?php echo $keywordsErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td align="right">Post Content</td>
<td><textarea name="content" cols="30" rows="15" value="<?php if(isset($_POST['content'])){echo $_POST['content']; }; ?>"></textarea>
<span style="color:red;"><?php echo $contentErr; ?></span>
</td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
insert_post.php
<?php
session_start();
include("includes/connect.php");
$title = $_SESSION['title'];
$author = $_SESSION['author'];
$keywords = $_SESSION['keywords'];
$content = $_SESSION['content'];
$date = date('d-m-Y');
$image = $_SESSION['image']['name'];
$image_tmp = $_SESSION['image']['tmp_name'];
move_uploaded_file($image_tmp, "../images/$image");
$query = "INSERT INTO posts (post_title, post_date, post_author, post_keywords, post_image, post_content) VALUES('$title', '$date', '$author', '$keywords', '$image', '$content')";
$result = mysqli_query($conn, $query);
if($query){
echo"<center><h1>Post Published Succesfully!</h1></center>";
}
else{
echo "<center><h1>Error! Post was not published!</h1></center>";
}
echo $title . " " . $author . " " . $keywords . " " . $content;
?>
Btw, you should be using prepared statements for your database inserts to prevent SQL-injections. Read more about it here.
You don't hAve to Break the Script using the Exit Funktion. Just prove if the error variables aren't empty and only in the Case when they aren't empty send a dB-request.
Chenge these lines:
include("includes/connect.php");
if(isset($_POST['submit'])){
to
include("includes/connect.php");
if( isset($_POST['submit']) //Fix here, it will only insert if all error message
&& empty($titleErr) //are empty.
&& empty($authorErr)
&& empty($keywordsErr)
&& empty($contentErr) ){
Edit
And also change this line
if (empty($_POST["submit"])) {
To
if (isset($_POST["submit"])) {
On the init of your script. The empty function will return true if you did not submit the page, that's why you are hving the error messages.
when i click on delete the page get refresh but data is not deleted can you tell me what’s the problem.tell me also how should i edit the data using edit.help me out in this ... i tried my best.
this is my delete page
<?php
$database = "example";
$conn = mysql_connect("localhost","root","root");
$db_found = mysql_select_db($database, $conn);
$id=$_REQUEST['ID'];
// sending query
mysql_query("DELETE FROM my WHERE ID = '$id'")
or die(mysql_error());
header("Location: main.php");
?>
this is my main page
<!DOCTYPE html>
<html>
<head>
<title>Employee</title>
</head>
<body>
<center><b><font size=20>Employee Detail</font></b></center>
<?php
$database = "example";
$conn = mysql_connect("localhost","root","root");
$db_found = mysql_select_db($database, $conn);
$SQL = "SELECT * FROM my";
$result = mysql_query($SQL);
print "<table border='1'>";
print "<tr>";
print "<th>ID</th>";
print "<th>First name</th>";
print "<th>Last name</th>";
print "<th>Gender</th>";
print "<th>Address</th>";
print "<th>Contact_no</th>";
print "<th>Picture</th>";
print "<th>User_name</th>";
print "<th>Password</th>";
print "<th>Email_id</th>";
while ( $db_field = mysql_fetch_assoc($result) )
{
print "<tr>";
print "<td>".$db_field['ID']."</td>";
print "<td>".$db_field['F_name']."</td>";
print "<td>".$db_field['L_name']."</td>";
print "<td>".$db_field['Gender']."</td>";
print "<td>".$db_field['Address']."</td>";
print "<td>".$db_field['Contact_no']."</td>";
print "<td>".$db_field['Picture']."</td>";
print "<td>".$db_field['U_name']."</td>";
print "<td>".$db_field['Password']."</td>";
print "<td>".$db_field['Email_id']."</td>";
echo"<td> <a href ='edit.php?ID=$id'>Edit</a>";
echo"<td> <a href ='delete.php?ID=$id'><center>Delete</center></a>";
print "</tr>";
}
print "</table>";
mysql_close($conn);
?>
<form>
<a href="test1.php">
<input type="button" value="Add">
</a>
</form>
</body>
</html>
this is my add page
<?php
?>
<html>
<head>
<title>Sign up Form</title>
<script type="text/javascript">
<!--
function validation()
{
if (document.login.fname.value==null || document.login.fname.value=="")
{
alert("First name must be filled out");
document.login.fname.focus();
return false;
}
if((document.login.fname.value.length<3))
{
alert("First name is too short");
document.login.psw.focus();
return false;
}
if (document.login.lname.value==null || document.login.lname.value=="")
{
alert("Last name must be filled out");
document.login.lname.focus();
return false;
}
if((document.login.lname.value.length<3))
{
alert("Last name is too short");
document.login.psw.focus();
return false;
}
if( document.login.select.selectedIndex==0)
{
alert( "Gender must be filled out" );
document.login.select.focus();
return false;
}
if (document.login.address.value==null || document.login.address.value=="")
{
alert("Address must be filled out");
document.login.address.focus();
return false;
}
if((document.login.address.value.length < 20))
{
alert(" Your address must be 20 characters");
document.login.address.select();
return false;
}
if (document.login.contact_no.value==null || document.login.contact_no.value=="")
{
alert("Contact number must be filled out");
document.login.contact_no.focus();
return false;
}
if(isNaN(document.login.contact_no.value))
{
alert("You use charecter in contact number");
document.login.contact_no.focus();
return false;
}
if((document.login.contact_no.value.length < 1) || (document.login.contact_no.value.length > 10))
{
alert("you enter more than 10 digit in contact");
document.login.contact_no.focus();
return false;
}
if (document.login.picture.value==null || document.login.picture.value=="")
{
alert("You must select an Image or Images");
document.login.picture.focus();
return false;
}
if (document.login.uname.value==null || document.login.uname.value=="")
{
alert("Login name must be filled out");
document.login.uname.focus();
return false;
}
if((document.login.psw.value.length<4))
{
alert("Password is too short");
document.login.psw.focus();
return false;
}
if (document.login.psw.value==null || document.login.uname.value=="")
{
alert("Password must be filled out");
document.login.psw.focus();
return false;
}
var emailfilter=/^\w+[\+\.\w-]*#([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
var b=emailfilter.test(document.login.e_id.value);
if(b==false)
{
alert("Please Enter a valid Mail ID");
document.login.e_id.focus();
return false;
}
}
//-->
</script>
</head>
<body>
<form name="login" action="insert.php" onsubmit="return(validation())" method="post" enctype= multipart/form-data>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lname" /></td>
</tr>
<tr>
<td>Gender:</td>
<td><select name="select">
<option value="-1" selected>[Select option]</option>
<option value="male">Male</option>
<option value="female">Female</option>
</td>
</tr>
<tr>
<td>Address:</td>
<td><textarea name="address" col="60" row="10"></textarea></td>
</tr>
<tr>
<td>Contact no:</td>
<td><input type="number" name="contact_no"></td>
</tr>
<tr>
<td>Picture:</td>
<td><input type="file" name="picture"> </td>
</tr>
<tr>
<td>User name:</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="psw"></td>
</tr>
<tr>
<td>Email id:</td>
<td><input type="email" name="e_id"></td>
</tr>
<tr>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
this is my edit page
<?php
$database = "example";
$conn = mysql_connect("localhost","root","root");
$db_found = mysql_select_db($database, $conn);
$id =$_REQUEST['ID'];
$result = mysql_query("SELECT * FROM my WHERE ID = '$id'");
$db_field = mysql_fetch_array($result);
if (!$result)
{
die("Error: Data not found..");
}
$F_name=$db_field['F_name'];
$L_name=$db_field['L_name'];
$Gender=$db_field['Gender'];
$Address=$db_field['Address'];
$Contact_no=$db_field['Contact_no'];
$Picture=$db_field['Picture'];
$U_name=$db_field['U_name'];
$Password=$db_field['Password'];
$Email_id=$db_field['Email_id'];
if(isset($_POST['save']))
{
$fname_save = $_POST['fname'];
$lname_save = $_POST['lname'];
$gender_save = $_POST['select'];
$address_save = $_POST['address'];
$contactno_save = $_POST['contact_no'];
$picture_save = $_POST['picture'];
$uname_save = $_POST['u_name'];
$password_save = $_POST['psw'];
$emailid_save = $_POST['e_id'];
mysql_query("UPDATE my SET F_name='$fname_save', L_name='$lname_save', Gender='$gender_save', Address='$address_save', Contact_no='$contactno_save', Picture='$picture_save', U_name='$uname_save', Password='$password_save', Email_id='$emailid_save' WHERE ID = '$id'")
or die(mysql_error());
echo "Saved!";
header("Location: main.php");
}
mysql_close($conn);
?>
</head>
<body>
<form method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" value="<?php echo $F_name ?>" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lname" value="<?php echo $L_name ?>" /></td>
</tr>
<tr>
<td>Gender:</td>
<td><select name="select" value="<?php echo $Gender ?>">
<option value="-1" selected>[Select option]</option>
<option value="male">Male</option>
<option value="female">Female</option>
</td>
</tr>
<tr>
<td>Address:</td>
<td><textarea name="address" col="60" row="10" value="<?php echo $Address ?>"></textarea></td>
</tr>
<tr>
<td>Contact no:</td>
<td><input type="number" name="contact_no" value="<?php echo $Contact_no ?>"></td>
</tr>
<tr>
<td>Picture:</td>
<td><input type="file" name="picture" value="<?php echo $Picture ?>"> </td>
</tr>
<tr>
<td>User name:</td>
<td><input type="text" name="uname" value="<?php echo $U_name ?>"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="psw" value="<?php echo $Password ?>"></td>
</tr>
<tr>
<td>Email id:</td>
<td><input type="email" name="e_id" value="<?php echo $Email_id ?>"></td>
</tr>
<tr>
<td><input type="submit" name="save" value="save"></td>
</tr>
</table>
</form>
</body>
</html>
this is my database
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', 'root');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected Successfully';
$sql = "CREATE TABLE my(
ID INT NOT NULL AUTO_INCREMENT,
F_name VARCHAR(20) NOT NULL,
L_name VARCHAR(20) NOT NULL,
Gender VARCHAR(10) NOT NULL,
Address VARCHAR(80) NOT NULL,
Contact_no INT NOT NULL,
Picture BLOB NOT NULL,
U_name VARCHAR(20) NOT NULL,
Password VARCHAR(25) NOT NULL,
Email_id VARCHAR(30) NOT NULL,
primary key ( ID ))";
mysql_select_db('example');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
echo "Table my created successfully\n";
mysql_close($conn);
?>
echo ("<td>Delete</td></tr>")
Add:
$id = $db_field['ID'];
to the beginning of the while loop on the main page. You're using this variable in the URLs in your echo statements, but never setting it.
I'm trying to insert data to custom table from frontend as well as backend in wordpress.
Below is my code, its working if i insert the data from backend but its giving me Error 404 if i try to insert from frontend.
<?php
/*
Plugin Name: Custom Form
Description: Custom Plugin
Author: Bijay Luitel
*/
// Create the table if not exixts
?>
<style>
p {
display:block;
}
h3 {
height:20px;
padding:10px 5px;
}
</style>
<?php
//Short Codes
add_shortcode('form_bands','form_bands');
function form_bands(){
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$query1 = "SELECT * FROM grade";
$result1 = $wpdb->get_results($query1);
$query2 = "SELECT * FROM branch";
$result2 = $wpdb->get_results($query2);
if($_POST['action']==1 && $_POST['name'] != '' ){
$page_one_table = 'band';
$name =$_POST['name'];
$mailingAddress = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$url = $_POST['url'];
$telephone = $_POST['telephone'];
$gradeId = $_POST['grade'];
$branchId = $_POST['branch'];
$insertMe="INSERT INTO band ('Name', 'MailingAddress', 'City', 'State', 'Zip', 'Email', 'URL', 'Telephone', 'GradeID', 'BranchID') VALUES('$name', '$mailingAddress', '$city', '$state', '$zip', '$email', '$url', '$telephone', '$gradeId', '$branchId')";
$insert_page_one = $wpdb->query($insertMe);
//$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
$form_id = $wpdb->insert_id;
if($insert_page_one)
{
echo '<div id="successMsg" class="updated below-h2"><p>Operation Successful</p></div>';
}
else{
echo '<div id="successMsg" class="updated below-h2"><p>Error ! Recheck and tryagain.</p></div>';
}
}
elseif ($_POST['action']==1 && $_POST['name'] == ''){
echo '<div id="successMsg" class="updated below-h2"><p>Error ! Recheck and tryagain.</p></div>';
}
?>
<h2>Bands</h2>
<div class="postbox">
<form action="" method="post">
<div class="inside">
<table class="form-table">
<tr>
<th>Name :</th>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<th>Address :</th>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<th>City :</th>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<th>State :</th>
<td><input type="text" name="state" /></td>
</tr>
<tr>
<th>Zip :</th>
<td><input type="text" name="zip" /></td>
</tr>
<tr>
<th>Telephone :</th>
<td><input type="text" name="telephone" /></td>
</tr>
<tr>
<th>Email :</th>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<th>Url :</th>
<td><input type="text" name="url" /></td>
</tr>
<tr>
<th>Grade :</th>
<td><select name="grade">
<?php foreach($result1 as $row){
$value = $row->GradeID;
echo '<option value="'.$value.'">';
echo $row->Grade;
echo "</option>";
}?>
</select></td>
</tr>
<tr>
<th>Branch :</th>
<td><select name="branch">
<?php foreach($result2 as $row){
$value = $row->BranchID;
echo '<option value="'.$value.'">';
echo $row->Name;
echo "</option>";
}?>
</select></td>
</tr>
</table>
<p class="submit">
<input type="submit" name="add_form" class="button-primary" value="Submit" />
</p>
<input type="hidden" name="action" value="1" />
</form>
</div>
</div>
<?php
}
function myForm ()
{
add_menu_page('Forms', 'Forms', '','forms', '');
add_submenu_page("forms", "Bands", "Bands", 0, "Bands", "form_bands");
}
add_action('admin_menu','myForm');
How can i solve this problem? Please Help me
I expect the issue you're having relates to your use of a "reserved" post variable name, of 'name'.
The WordPress Codex page for Register_Taxonomy() contains the list of "reserved terms".
Further, your action attribute on your form tag is missing your URL. That's handled OK in current browsers, but may cause unexpected behavior in some older browsers, and isn't guaranteed to work in future.
Better practice is to remove this attribute altogether, if you're not going to use it, because the spec strongly discourages authors from leaving it empty:
The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.
(This info re the action attribute thanks to #mercator with this answer )
I wonder is it possible to keep the user input inside form field after form submitted, so that the user can update the entry. I've a html registration form [with some JS validation], then a php file to insert data to sql & meanwhile display back the inserted data in a table view. i also include the form's html code in php file so i can see the form after being submitted. but i couldn't keep the data in the field after form submitted! here is the form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
<!--
function validateNum(evt) {
var theEvent = evt;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]/;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
function validate(evt){
if( document.myForm.ic.value == ""){
alert( "IC Number cann't be empty!" );
document.myForm.ic.focus() ;
return false;}
else if(isNaN( document.myForm.ic.value ) || document.myForm.ic.value.length != 12){
evt.preventDefault();
alert( "Please provide your correct IC Number!" );
document.myForm.ic.focus() ;
return false;}
if( document.myForm.name.value == "") {
alert( "Name cann't be empty!" );
document.myForm.name.focus() ;
return false;
}
if( document.myForm.contact.value == ""){
alert( "Contact number cann't be empty!");
document.myForm.contact.focus() ;
return false;
} else if(isNaN( document.myForm.contact.value ))
{
evt.preventDefault();
alert( "Please provide your correct Contact Number!" );
document.myForm.contact.focus() ;
return false;
}
if( document.myForm.address.value == "" ){
alert( "Please provide your Address!" );
document.myForm.address.focus() ;
return false;
}
}
//-->
</script>
</head>
<style type="text/css">
h2 {
color: #06C;
}
body {
background-color: #FFC;
}
</style>
<body>
<form name="myForm" method="post" action="insert.php" onsubmit="return(validate(event));">
<div align="center"><br>
<table width="453" border="0">
<tr>
<th colspan="4" bgcolor="#99FFFF" scope="col">
<h3>Workshop Name: PHP! </h3></th>
</tr>
<tr bgcolor="#99FF99">
<td width="142"> IC Number</td>
<td width="15"><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="ic" type="text" id="ic" maxlength="12" size="45" onkeypress='validateNum(event)'/>
</div></td>
</tr>
<tr bgcolor="#99FFFF">
<td>Full Name</td>
<td><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="name" type="text" id="name" size="45"/>
</div></td>
</tr>
<tr bgcolor="#99FF99">
<td>Contact No.</td>
<td><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="contact" type="text" id="contact" size="45" onkeypress='validateNum(event)' />
</div></td>
</tr>
<tr bgcolor="#99FFFF">
<td>Email</td>
<td><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="mail" type="text" id="mail" size="45"/>
</div></td>
</tr>
<tr bgcolor="#99FF99">
<td height="60">Address</td>
<td><div align="center">:</div></td>
<td colspan="2">
<div align="right">
<textarea name="address" id="address" cols="35" rows="3"></textarea>
</div>
</td>
</tr>
<tr bgcolor="#99FFFF">
<td colspan="2"> </td>
<td width="231"><input type="reset" value="Clear" /></td>
<td width="47"><div align="right">
<input type="submit" value="Submit" />
</div></td>
</tr>
</table>
<br>
</div>
</form>
</body>
</html>
here is the insert.php file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
<!--
function validateNum(evt) {
var theEvent = evt;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]/;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
function validate(evt){
if( document.myForm.ic.value == ""){
alert( "IC Number cann't be empty!" );
document.myForm.ic.focus() ;
return false;}
else if(isNaN( document.myForm.ic.value ) || document.myForm.ic.value.length != 12){
evt.preventDefault();
alert( "Please provide your correct IC Number!" );
document.myForm.ic.focus() ;
return false;}
if( document.myForm.name.value == "") {
alert( "Name cann't be empty!" );
document.myForm.name.focus() ;
return false;
}
if( document.myForm.contact.value == ""){
alert( "Contact number cann't be empty!");
document.myForm.contact.focus() ;
return false;
} else if(isNaN( document.myForm.contact.value ))
{
evt.preventDefault();
alert( "Please provide your correct Contact Number!" );
document.myForm.contact.focus() ;
return false;
}
if( document.myForm.address.value == "" ){
alert( "Please provide your Address!" );
document.myForm.address.focus() ;
return false;
}
}
//-->
</script>
</head>
<style type="text/css">
h2 {
color: #06C;
}
body {
background-color: #FFC;
}
</style>
<body>
<form name="myForm" method="post" action="update.php" onsubmit="return(validate(event));">
<div align="center"><br>
<table width="453" border="0">
<tr>
<th colspan="4" bgcolor="#99FFFF" scope="col">
<h3>Workshop Name: PHP! </h3></th>
</tr>
<tr bgcolor="#99FF99">
<td width="142"> IC Number</td>
<td width="15"><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="ic" type="text" id="ic" maxlength="12" size="45" onkeypress='validateNum(event)'/>
</div></td>
</tr>
<tr bgcolor="#99FFFF">
<td>Full Name</td>
<td><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="name" type="text" id="name" size="45"/>
</div></td>
</tr>
<tr bgcolor="#99FF99">
<td>Contact No.</td>
<td><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="contact" type="text" id="contact" size="45" onkeypress='validateNum(event)' />
</div></td>
</tr>
<tr bgcolor="#99FFFF">
<td>Email</td>
<td><div align="center">:</div></td>
<td colspan="2"><div align="right">
<input
name="mail" type="text" id="mail" size="45"/>
</div></td>
</tr>
<tr bgcolor="#99FF99">
<td height="60">Address</td>
<td><div align="center">:</div></td>
<td colspan="2">
<div align="right">
<textarea name="address" id="address" cols="35" rows="3"></textarea>
</div>
</td>
</tr>
<tr bgcolor="#99FFFF">
<td colspan="2"> </td>
<td width="231"><input type="reset" value="Clear" /></td>
<td width="47"><div align="right">
<input type="submit" value="Update" />
</div></td>
</tr>
</table>
<br>
</div>
</form>
<br>
</div>
</form>
<div align="center">
<?php
if (!mysql_connect('localhost', 'root', '')) {
echo "Connected";
}
mysql_select_db("workshop");
// Get values from form
$ic = mysql_real_escape_string($_POST['ic']);
$name = mysql_real_escape_string($_POST['name']);
$contact = mysql_real_escape_string($_POST['contact']);
$mail = mysql_real_escape_string($_POST['mail']);
$address = mysql_real_escape_string($_POST['address']);
if (staff_detail_exist($ic) == "available") {
insert_staff_detail($ic, $name, $contact, $mail, $address, $paytype);
echo "<p style='text-align:center; color:green;'>" . "Workshop application successful! You will be notified shortly via E-mail after confirmation! Thank You!";
} else if (staff_detail_exist($ic) == "exist") {
echo "<p style='text-align:center; color:red;'>" . "Record already exists! Please enter another Staff ID. Thank You!" . "</p>";
}
function insert_staff_detail($ic, $name, $contact, $mail, $address, $paytype) {
$sql = "INSERT INTO apply (staffid, staffname, staffno, staffemail, staffaddress, paytype) VALUES ('$ic', '$name', '$contact', '$mail', '$address','$paytype')";
mysql_query($sql);
}
function staff_detail_exist($ic) {
$result = null;
$sql = "SELECT * FROM apply WHERE staffid = '$ic'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 0) {
$result = "available";
} else {
$result = "exist";
}
return $result;
}
$staffid = $_POST['ic'];
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("workshop", $con);
$result = mysql_query("SELECT * FROM apply where staffid = '$ic'");
echo "<table width=400 border=1 cellpadding=0 align=center>";
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<th>Staff/IC Number: </th><td>" . "<center>" . $row['staffid'] . "</center>" . "</td>";
echo "</tr>";
echo "<th>Name: </th><td>" . "<center>" . $row['staffname'] . "</center>" . "</td>";
echo "</tr>";
echo "<th>Email: </th><td>" . "<center>" . $row['staffemail'] . "</center>" . "</td>";
echo "</tr>";
echo "<th>Contact No.: </th><td>" . "<center>" . $row['staffno'] . "</center>" . "</td>";
echo "</tr>";
echo "<th>Address: </th><td>" . "<center>" . $row['staffaddress'] . "</center>" . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
I've tried to add like value="<? echo "$row['staffid']"?>" in the form's field at php file but no luck! I've only basic in php. So, any help? thank you!
thanks all, its finally working :) i've used value="<?php echo isset($_POST['myField']) ? $_POST['myField'] : 'myField_db' ?>" inside the input tag. so, its like: <input type="text" name="myField" value="<?php echo isset($_POST['myField']) ? $_POST['myField'] : 'myField_db' ?>" /> where myField is input name & myField_db is the column name from database.
Take the form posted values just above your html code like this
<?php
if (isset($_POST["submit"]) && $_POST["submit"]=='Submit') {
$name=$_POST["name"];
}
?>
And echo it in your html form.
<input name="name" type="text" id="name" size="45" value="<? echo $name?>"/>
I've used this function a few times; quite handy
function getPost($field){
return (isset($_POST[$field]) && $_POST[$field] != "" ? $_POST[$field] : "");
}
Usage
<input type="text" name="contact" value="<?php echo getPost("contact"); ?>" />
This is for the cases where a user submits information and is for some reason sent back to the form again - perhaps their entries didn't pass PHP validation, for example.