I am trying to update the record in my database but after submiting form it does not go back to the page committee.php and records does not get updated. Is there any problem with my query?
<?php
include_once('connection.php');
if(isset($_GET['details_id']))
{
$id=$_GET['details_id'];
if(isset($_POST['submit']))
{
$title = $_POST['title'];
$pageno = $_POST['page'];
$author = $_POST['author'];
//$authorimg = $_POST['image'];
$article_status = $_POST['articlestatus'];
$sketch_status = $_POST['sketchstatus'];
$final_approval_person = $_POST['finalperson'];
$final_approval_date = $_POST['finaldate'];
$status = $_POST['status'];
$query = "update details set title='$title', page_no='$pageno', author='$author', article_status='$article_status', sketch_status='$sketch_status', final_approve_person='$final_approval_person', final_approve_date='$final_approval_date', status='$status', where id=$id";
$res = mysqli_query($DBCONNECT,$query);
if($res)
{
header('location:committee.php');
}
else
{
echo mysql_error();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body style="margin-left:30%;">
<div class="form-style-2">
<div class="form-style-2-heading">Provide Following Information</div>
<form action="" method="post">
<label for="field1"><span>Title <span class="required">*</span></span><input type="text" class="input-field" name="title" value="" /></label>
<label for="field1"><span>Page No. </span><input type="number" class="input-field" name="page"
min="0" max="100" step="10" value="30"></label>
<label for="field2"><span>Author <span class="required">*</span></span><input type="text" class="input-field" name="author" value="" /></label>
<label for="field1"><span>Author Image <span class="required">*</span></span><input type="file" class="input-field" name="image" value="" /></label>
<label for="field4"><span>Article Status</span><select name="articlestatus" class="select-field">
<option value="notrecieved">Not Recieved</option>
<option value="recieved">Recieved</option>
</select></label>
<label for="field4"><span>Sketch Status</span><select name="sketchstatus" class="select-field">
<option value="notapproved">Not Approved</option>
<option value="approved">Approved</option>
</select></label>
<label for="field2"><span>Final Approval Person <span class="required">*</span></span><input type="text" class="input-field" name="finalperson" value="" /></label>
<label for="field2"><span>Final Approval Date <span class="required">*</span></span><input type="date" class="input-field" name="finaldate" value="" /></label>
<label for="field2"><span>Status <span class="required">*</span></span><input type="text" class="input-field" name="status" value="" /></label>
<!--<label for="field4"><span>Regarding</span><select name="field4" class="select-field">
<option value="General Question">General</option>
<option value="Advertise">Advertisement</option>
<option value="Partnership">Partnership</option>
</select></label>
<label for="field5"><span>Message <span class="required">*</span></span><textarea name="field5" class="textarea-field"></textarea></label>
-->
<label><span> </span><input type="submit" value="submit" name="submit" /></label>
</form>
</div>
</body>
</html>
Your query is wrong. Use this
$query = "update details set title='$title', page_no='$pageno',
author='$author', article_status='$article_status',
sketch_status='$sketch_status', final_approve_person='$final_approval_person',
final_approve_date='$final_approval_date', status='$status' where id=$id";
No need comma here status='$status', where id=$id
Also add single quote for $id in the where clause.
Related
my code is not inserting any data on my php, im using a form that will display values but my code in update is not working. please help,
here is my code in php :
if (isset($_POST['update'])) {
$landowner_id = $_POST['landowner_id'];
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barnagay'];
$areacovered = $_POST['areacovered'];
$sex = $_POST['sex'];
mysqli_query($db, "UPDATE info SET firstname='$firstname', middlename='$middlename', lastnamename='$lastname', municipality='$municipality', barangay='$barangay', areacovered='$areacovered', sex='$sex' WHERE landowner_id=$landowner_id");
$_SESSION['message'] = "Address updated!";
}
here is my html
<div class="form-wrapper">
<input type="number" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id;?>" required>
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $firstname;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="middlename" placeholder="Middle Name" class="input-field" value="<?php echo $middlename;?>">
</div>
<div class="form-wrapper">
<input type="text" name="lastname" placeholder="Last Name" class="input-field" value="<?php echo $lastname;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="municipality" placeholder="Municipality" class="input-field" value="<?php echo $municipality;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="barangay" placeholder="Barangay" class="input-field" value="<?php echo $barangay;?>" >
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="areacovered" placeholder="Area Covered" class="input-field" value="<?php echo $areacovered;?>" required>
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex;?>" required>
<br>
<button class="btn" type="submit" name="update" >Update</button>
</div>
I don't see any "form" tag. Are you missing to wrap your "form-wrapper" into a tag? Something like this:
<form action="" method="post">
<div class="form-wrapper">
<input type="number" id="check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id; ?>" required>
</div>
<!-- Other inputs -->
<div class="form-wrapper">
<input type="text" id="check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex; ?>" required>
<br>
<button class="btn" type="submit" name="update">Update</button>
</div>
</form>
Other important things to consider:
Never EVER send to the DB plain inputs coming from the outside without cleaning them! Otherwise, you will be open to SQL injection. Use prepare-statements to solve this issue.
Instead of mysqli_query I recommend you to use PDO. You can prepare statement super easy. Here you can see an example of usage: https://stackoverflow.com/a/60988740/3454593
I try to make function edit data but it doesn't work. When a file/img is empty then data successfully inserted without file or image and but, when a file is already exist then replace it, it fails.
HTML code:
<form method="post" id="insert_form" enctype="multipart/form-data">
<label>Enter Employee Name</label>
<input type="text" name="name" id="name" class="form-control" />
<br />
<label>Enter Employee Address</label>
<textarea name="address" id="address" class="form-control"></textarea>
<br />
<label>Select Gender</label>
<select name="gender" id="gender" class="form-control">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br />
<label>Enter Designation</label>
<input type="text" name="designation" id="designation" class="form-control" />
<br />
<label>Enter Age</label>
<input type="text" name="age" id="age" class="form-control" />
<br />
<label>File</label>
<br>
<div>
<img src="" id="pict" width="100px" class="img-thumbnail">
</div>
<br>
<input type="file" name="image" id="image">
<span id="errMess"></span>
<br/>
<br>
<input type="hidden" name="employee_id" id="employee_id" />
<input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />
</form>
Php code:
$conn = mysqli_connect('localhost','root','','test');
$name = mysqli_real_escape_string($conn,$_POST['name']);
$address = mysqli_real_escape_string($conn,$_POST['address']);
$gender = mysqli_real_escape_string($conn,$_POST['gender']);
$designation = mysqli_real_escape_string($conn,$_POST['designation']);
$age = mysqli_real_escape_string($conn,$_POST['age']);
$extensi = explode(".", $_FILES['image']['name']);
$img = "ND-".round(microtime(true)).".".end($extensi);
$temp = $_FILES['image']['tmp_name'];
move_uploaded_file($tmp, "assets/img/myfolder/".$img);
$id = $_POST['employee_id'];
if($id != '')
{
if($_FILES['image']['name'] == "")
{
$query = "UPDATE tbl_employee SET name='$name', address='$address',
gender='$gender', designation='$designation', age='$age' WHERE id = '$id' ";
$message = "data Update";
}
else
{
$sqlQ = "SELECT * FROM tbl_employee WHERE id = '$id'";
unlink("assets/img/myfolder/".**??????**); // confuse about this
move_uploaded_file($temp, "assets/img/myfolder/".$img);
$query = "UPDATE tbl_employee SET name='$name', address='$address',gender='$gender', designation='$designation', age='$age',image='$img' WHERE id = '$id' ";
$message = "data Update";
}
}
$sql = $conn->query($query);
The problem solved, I try to add ** # ** in front of unlink and it works:
#unlink($folder);
move_uploaded_file($sumber, "assets/img/product/".$img);
$query = "UPDATE tbl_employee SET name='$name', address='$address',
gender='$gender', designation='$designation', age='$age', image='$img' WHERE id = '$id' ";
I am currently trying to get form data to insert to a MySQL database using a form and php. The form is not submitting the data and I am not sure if there is an issue with my code or there is something in my database. I have checked numerous times that all the code matches correctly in the database as well as validating my code with no errors. Is there something simple that i have missed?
<?php
$mysqli = new mysqli("localhost", "root", "", "etrading");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['submit'])) {
$key=$_POST['ItemID'];
$name= $_POST['Name'];
$description= $_POST['Description'];
$img_path= $_POST['img_path'];
$quantity= $_POST['Quantity'];
$category= $_POST['Category'];
$location= $_POST['Location'];
$saletype= $_POST['Saletype'];
$price= $_POST['Price'];
$duration= $_POST['Duration'];
$payment= $_POST['Payment'];
$query = "INSERT INTO item (ItemID, Name, Description,img_path, Quantity, Category, Location, Saletype, Price,Duration,Payment) VALUES ('$key','$name','$description','$img_path','$quantity','$category','$location','$saletype','$price','$duration','$payment',)";
if (mysqli_query($mysqli, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($mysqli);
}
}
/* close connection */
$mysqli->close();
?>
I have also set the ItemID to auto increment in the database
And this is my form code that i am using.
<form id="sellitem" action="sellitem.php" method="POST" >
<fieldset>
<h4>Sell Your Item</h4>
<p><label class="title" for="Name">Name:</label>
<input type="text" placeholder="Enter item name" name="Name" id="Name" title="Please enter item name"
><br />
<label class="title" for="Description">Description:</label>
<textarea name="Description" rows="5" cols="33" placeholder="Please describe your item" id="Description" title="Please describe your item" ></textarea><br />
Select image to upload:
<input type="file" name="img_path" id="img_path" ><br>
<label class="title" for="Quantity">Quantity:</label>
<input type="text" placeholder="Number of items" name="Quantity" id="Quantity" title="Number of items" ><br />
<label class="title" for="Category">Category:</label>
<select name="Category" id="Category">
<option value="clothes">Clothes</option>
<option value="books">Books</option>
<option value="electronics">Electronics</option>
<option value="sport">Sport</option>
</select></p>
<label class="title" for="Location">Location:</label>
<input type="text" placeholder="Item Location" name="Location" id="Location" title="Enter item location" ><br />
<label class="title" for="Saletype">Sale Type:</label>
<select name="Saletype" id="Saletype" >
<option value="Auction">Auction</option>
<option value="BuyNow">Buy Now</option>
</select>
<label class="title" for="Price">Price: $</label>
<input type="text" placeholder="00.00" name="Price" id="Price" title="Please enter your name" ><br />
<label class="title" for="Duration">Duration:</label>
<input type="text" placeholder="End date" name="Duration" id="Duration" title="End Date" ><br />
<label class="title" for="Payment">Payment Type:</label>
<select name="Payment" id="Payment" >
<option value="PayPal">PayPal</option>
<option value="Bank Deposit">Bank Deposit</option>
<option value="Card">Credit Card</option>
</select><br>
<div class="submit"><input type="submit" value="submit" /></div>
<div class="reset"><input type="reset" /></div>
</fieldset>
</form>
Change this line of your code from
<input type="submit" value="submit" />
to
<input type="submit" value="submit" name="submit" />
You are not entering if(isset($_POST['submit'])) {
Add the name attribute to the submit button
<input type="submit" value="submit" name="submit" />
1.Every input field should have name attribute to POST/GET data.
<input type="submit" value="submit" />
to
<input type="submit" value="submit" name="submit" />
2.Due to obscene of name attribute of type="submit" field form posting only
Array
(
[Name] => dsfdsf
[Description] => dfdsf
[img_path] =>
[Quantity] =>
[Category] => clothes
[Location] =>
[Saletype] => Auction
[Price] =>
[Duration] =>
[Payment] => PayPal
)
3.Below query added one extra comma
$query = "INSERT INTO item (ItemID, Name, Description,img_path, Quantity, Category, Location, Saletype, Price,Duration,Payment) VALUES ('$key','$name','$description','$img_path','$quantity','$category','$location','$saletype','$price','$duration','$payment',)";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to insert data to my database from a form and when i select a type (doctor or patient) to send the data to the appropriate table.
HERE IS THE CODE
<?php
include_once 'header.php';
if (isset($_SESSION['username'])) destroySession();
if(isset($_POST['register']))
{
if (isset($_POST['username']))
{
$fname = sanitizeString($_POST['fname']);
$lname = sanitizeString($_POST['lname']);
$username = sanitizeString($_POST['username']);
$email = sanitizeString($_POST['email']);
$password = sanitizeString($_POST['password']);
if($_POST["answer" === "Doctor"])
{
$DoctorG = sanitizeString($_POST['DoctorG']);
$DoctorAge = ($_POST['DoctorAge']);
$specialty = sanitizeString($_POST['specialty']);
$Doctor_ID = ($_POST['Doctor_ID']);
if (mysql_num_rows(queryMysql("SELECT * FROM doctor
WHERE username='$username'")))
$error = "That username already exists<br /><br />";
else
{
queryMysql("INSERT INTO doctor (fname,lname,username,email,password,gender,age,specialty,doctorID) VALUES('$fname','$lname','$username','$email', '$password','$DoctorG','$DoctorAge','$specialty','$DoctorID')");
die("<h4>Account created</h4>Please Log in.<br /><br />");
}
}
}
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form method="post" action="signup.php">
<fieldset>
<legend>Registration Form</legend>
<label>First Name
<input type="text" name="fname" required="required" />
</label>
<br/>
<br/>
<label>Last Name
<input type="text" name="lname" required="required" />
</label>
<br />
<br />
<label>Username
<input type="text" name="username" required="required" />
</label>
<br />
<br />
<label>Email
<input type="text" name="email" required="required" />
</label>
<br />
<br />
<label>Password
<input type="text" name="password" required="required" />
</label>
<br/><br/>
User Type:
<br/>Doctor
<input type="radio" name="answer" value="Doctor" />
Patient
<input type="radio" name="answer" value="Patient" />
<!--DOCTOR OPTIONS -->
<div id="expandDoctor" style="display:none;">
<label id="Male">Male</label>
<input type="radio" name="DoctorG" value="male" id="DoctorG">
<label id="Female">Female</label>
<input type="radio" name="DoctorG" value="female" id="DoctorG">
<br/>
<br/>
<label id="Age">Age:</label>
<input type="text" name="DoctorAge" id="DoctorAge" />
<br/>
<br/>
<label id="Specialty">Specialty:</label>
<select id="SelectSpecialty" name="specialty">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<br/>
<br/>
<label id="ID">Doctor ID:</label>
<input type="text" name="Doctor_ID" id="Doctor_ID" />
</div>
<!--PATIENT OPTIONS -->
<div id="expandPatient" style="display:none;">
<label id="Male">Male</label>
<input type="radio" name="PatientG" value="male" id="PatientGM">
<label id="Female">Female</label>
<input type="radio" name="PatientG" value="female" id="PatientGF">
<br/>
<br/>
<label id="Age">Age:</label>
<input type="text" name="PatientAge" id="PatientAge" />
<br/>
<br/>
<label id="Disease">Disease:</label>
<select id="SelectDisease" name="specialty">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<br/>
<br/>
<label id="SPID">SPID:</label>
<input type="text" name="PatientSPID" id="PatientSPID" />
</div>
</fieldset>
<input type="submit" value="Register" name="register"/>
</form>
</body>
<script>
$("input[type='radio'][name='answer']").change(function() {
$("[id^=expand]").hide();
$("#expand" + $(this).val()).show();
});</script>
</body>
</html>
I get this error Notice: Undefined offset: 0 in E:\xampp\htdocs\ptixiaki\signup.php on line 21
PS: I dont have all the needed inserts , I just want to test it for the Doctor and if it works for them I will do for the patient also
The error was that i should have if( $_POST["answer"] === "Doctor" ) instead of if($_POST["answer" === "Doctor"]) and then it shows a second error in the INSERT INTO which was an underscore in the Doctor_ID
HERE IS THE NEW CODE....
<?php
/**
* #author Nick Bourlai
* #copyright 2014
*/
include_once 'header.php';
if (isset($_SESSION['username'])) destroySession();
if(isset($_POST['register']))
{
if (isset($_POST['username']))
{
$fname = sanitizeString($_POST['fname']);
$lname = sanitizeString($_POST['lname']);
$username = sanitizeString($_POST['username']);
$email = sanitizeString($_POST['email']);
$password = sanitizeString($_POST['password']);
if( $_POST["answer"] === "Doctor" )
{
$DoctorG = sanitizeString($_POST['DoctorG']);
$DoctorAge = ($_POST['DoctorAge']);
$specialty = sanitizeString($_POST['specialty']);
$Doctor_ID = ($_POST['Doctor_ID']);
if (mysql_num_rows(queryMysql("SELECT * FROM doctor
WHERE username='$username'")))
$error = "That username already exists<br /><br />";
else
{
queryMysql("INSERT INTO doctor (fname,lname,username,email,password,gender,age,specialty,doctorID) VALUES('$fname','$lname','$username','$email', '$password','$DoctorG','$DoctorAge','$specialty','$Doctor_ID')");
die("<h4>Account created</h4>Please Log in.<br /><br />");
}
}
}
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form method="post" action="signup.php">
<fieldset>
<legend>Registration Form</legend>
<label>First Name
<input type="text" name="fname" required="required" />
</label>
<br/>
<br/>
<label>Last Name
<input type="text" name="lname" required="required" />
</label>
<br />
<br />
<label>Username
<input type="text" name="username" required="required" />
</label>
<br />
<br />
<label>Email
<input type="text" name="email" required="required" />
</label>
<br />
<br />
<label>Password
<input type="text" name="password" required="required" />
</label>
<br/><br/>
User Type:
<br/>Doctor
<input type="radio" name="answer" value="Doctor" />
Patient
<input type="radio" name="answer" value="Patient" />
<!--DOCTOR OPTIONS -->
<div id="expandDoctor" style="display:none;">
<label id="Male">Male</label>
<input type="radio" name="DoctorG" value="male" id="DoctorG">
<label id="Female">Female</label>
<input type="radio" name="DoctorG" value="female" id="DoctorG">
<br/>
<br/>
<label id="Age">Age:</label>
<input type="text" name="DoctorAge" id="DoctorAge" />
<br/>
<br/>
<label id="Specialty">Specialty:</label>
<select id="SelectSpecialty" name="specialty">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<br/>
<br/>
<label id="ID">Doctor ID:</label>
<input type="text" name="Doctor_ID" id="Doctor_ID" />
</div>
<!--PATIENT OPTIONS -->
<div id="expandPatient" style="display:none;">
<label id="Male">Male</label>
<input type="radio" name="PatientG" value="male" id="PatientGM">
<label id="Female">Female</label>
<input type="radio" name="PatientG" value="female" id="PatientGF">
<br/>
<br/>
<label id="Age">Age:</label>
<input type="text" name="PatientAge" id="PatientAge" />
<br/>
<br/>
<label id="Disease">Disease:</label>
<select id="SelectDisease" name="specialty">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<br/>
<br/>
<label id="SPID">SPID:</label>
<input type="text" name="PatientSPID" id="PatientSPID" />
</div>
</fieldset>
<input type="submit" value="Register" name="register"/>
</form>
</body>
<script>
$("input[type='radio'][name='answer']").change(function() {
$("[id^=expand]").hide();
$("#expand" + $(this).val()).show();
});</script>
</body>
</html>
I am trying to save the value of the radio buttons in my database table choice. I get the message Data saved successfully but no value is stored in the table.
Form:
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<center<legend>Choose in which category you'd like to be included</legend></center>
<p><input type="radio" name="choice[]" value="player" id="player" class="custom" />
<label for="player">Player</label>
<input type="radio" name="choice[]" value="coach" id="coach" class="custom" />
<label for="coach">Coach</label>
<input type="radio" name="choice[]" value="supporter" id="supporter" class="custom" />
<label for="supporter">Supporter</label>
<input type="radio" name="choice[]" value="sponsor" id="sponsor" class="custom" />
<label for="sponsor">Sponsor</label>
<input type="radio" name="choice[]" value="alumni" id="alumni" class="custom" />
<label for="alumni">Alumni</label>
<input type="radio" name="choice[]" value="other" id="o" class="custom" />
<label for="o">Other</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
</body>
</html>
PHP:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$choice = $mysqli->real_escape_string($_POST['choice']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`,`choice`) VALUES ('".$name."','".$email."','".$phone."','".$other."','".$choice."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
var_dump($_POST) to sere what data flooding in.
One thing more to check if its save or submit ? in $_POST['save']
EDIT
After getting your full form - the error lies in your center tag
Change <center<legend> TO <center><legend>
The error - ↑ in this tag