replacing text box with checkbox mysql php [closed] - php

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 2 years ago.
Improve this question
I am using php and mysql to update user data inside of the database as admin, i have a text box which allows the admin to input approved or not approved, now i want to try change the text box for the approved or not approved into a check box where the admin can just click on the check box approved and the data will be updated according to the user which was selected as im not really sure how to go about it. thanks
<?php
require('connection.php');
if (isset($_POST['submit'])) {
$prescription = $_FILES['prescription']['name'];
$image_tmp = $_FILES['prescription']['tmp_name'];
move_uploaded_file($image_tmp,"medical-prescription/$prescription");
$sql = $conn->prepare("UPDATE users SET approved=?,prescription=? WHERE id=?");
$approved=$_POST['approved'];
$sql->bind_param("ssi",$approved, $prescription,$_GET["id"]);
if($sql->execute()) {
$success_message = "Edited Successfully";
} else {
$error_message = "Problem in Editing Record";
}
}
$sql = $conn->prepare("SELECT * FROM users WHERE id=?");
$sql->bind_param("i",$_GET["id"]);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
}
$conn->close();
?>
<?php if(!empty($success_message)) { ?>
<div class="success message"><?php echo $success_message; ?></div>
<?php } if(!empty($error_message)) { ?>
<div class="error message"><?php echo $error_message; ?></div>
<?php } ?>
<form method="post" action="" enctype="multipart/form-data">
<label>approved</label>
<input type="text" name="approved" class="txtField" value="<?php echo $row["approved"]?>">
<label>Medical prescription</label>
<input type="file" name="prescription" id="prescription" required/><br>
<input type="submit" name="submit" value="Submit" class="demo-form-submit">
</form>

You can do it this way:
Change HTML:
<input type="checkbox" name="approved" class="txtField" value="<?php echo $row["approved"]?>"> Approved?
php:
Replace this:
...
$approved=$_POST['approved'];
...
with:
...
$approved=(isset($_POST['approved'])) ? $_POST['approved']: "Not approved"; // Or any other text value what you use
...
Here is formatted code:
<?php
require('connection.php');
if (isset($_POST['submit'])) {
$prescription = $_FILES['prescription']['name'];
$image_tmp = $_FILES['prescription']['tmp_name'];
move_uploaded_file($image_tmp,"medical-prescription/$prescription");
$sql = $conn->prepare("UPDATE users SET approved=?,prescription=? WHERE id=?");
//HERE WE CHANGE
$approved=(isset($_POST['approved'])) ? $_POST['approved']: "Not approved";
$sql->bind_param("ssi",$approved, $prescription,$_GET["id"]);
if($sql->execute()) {
$success_message = "Edited Successfully";
} else {
$error_message = "Problem in Editing Record";
}
}
$sql = $conn->prepare("SELECT * FROM users WHERE id=?");
$sql->bind_param("i",$_GET["id"]);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
}
$conn->close();
?>
<?php if(!empty($success_message)) { ?>
<div class="success message"><?php echo $success_message; ?></div>
<?php } if(!empty($error_message)) { ?>
<div class="error message"><?php echo $error_message; ?></div>
<?php } ?>
<form method="post" action="" enctype="multipart/form-data">
<label>approved</label>
<input type="checkbox" name="approved" class="txtField" value="<?php echo $row["approved"]?>"> Approve?
<label>Medical prescription</label>
<input type="file" name="prescription" id="prescription" required/><br>
<input type="submit" name="submit" value="Submit" class="demo-form-submit">
</form>
Here is the test:
$('form').on('submit', function(e){
e.preventDefault();
var data = $(this).serialize();
alert("Submitted data: {" + data + '}');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="checkbox" name="approved" class="txtField" value="Yes"> Approved?
<button type="submit" name="submit" value="submit">Submit</button>
</form>

Related

Hiding the form if the field in database is null in php

Hi i have a registration form in my website.if the particular field in the db is null then the form should not be displayed to the user.Here if the payment_category_upload field is empty then the form should not displayed to the user otherwise the form should be displayed.
<?php include 'includes/db.php';
$sql = "SELECT * FROM users WHERE username = '$_SESSION[user]' AND user_password = '$_SESSION[password]' AND payment_category_upload!='' ";
$oppointArr =array();
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0)
{
if(isset($_POST['submit_user'])|| isset($_POST['save_users']))
{
$formsubmitstatus = isset($_POST['submit_user'])?1:0;
if($_FILES["affidavits_upload"]["tmp_name"]!="")
{
$pname = rand(1000,10000)."-".str_replace("-"," ",$_FILES["affidavits_upload"]["name"]);
$affidavits_upload = $_FILES["affidavits_upload"]["tmp_name"];
$uploads_dir = '../admin/images/uploads';
move_uploaded_file($affidavits_upload, $uploads_dir.'/'.$pname);
}
else
{
$pname = $_POST['hid_affidavits_upload'];
}
$id= $_POST['users_id'];
$ins_sql = "UPDATE users set affidavits_upload='$pname',status='3',affidavitsupload_submit_status='$formsubmitstatus' WHERE users_id = $id";
$run_sql = mysqli_query($conn,$ins_sql);
$msg = 'Your Application successfully submitted. ';
$msgclass = 'bg-success';
}
else
{
$msg = 'Record Not Updated';
$msgclass = 'bg-danger';
}
}
else
{
echo "Please make the payment to enable Affidavits";
}
?>
FORM :
<form class="form-horizontal" action="affidavits.php" method="post" role="form" enctype="multipart/form-data" id="employeeeditform">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='users_id'>
<div class="form-group">
<label for="affidavits_upload" class="col-sm-4 control-label">Affidavits Upload</label>
<div class="col-sm-8">
<input type="hidden" value="<?php echo $oppointArr['affidavits_upload'];?>" name="hid_payment_category_upload">
<input type="file" name="affidavits_upload" id="affidavits_upload">
<?php if(!empty($oppointArr['affidavits_upload'])){?>
<div>
<?php echo $oppointArr['affidavits_upload'];?>
</div>
<?php }?>
<span class="text" style="color:red;">Please upload PDF Format Only</span>
</div>
</div>
<div class="col-sm-offset-2">
<?php if($oppointArr['affidavitsupload_submit_status'] == 0){ ?>
<button type="submit" class="btn btn-default" name="save_users" id="save_users">Save</button>
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit Application</button>
<?php } ?>
</div>
</form>
//Add this Css class
.hiddenBlock {
display:none
}
<div class="<?php echo isset(test_field)?"":"hiddenBlock"; ?>">
<form>...<form>
</div>
You can do it like this for your field.

Problem: Can't update the db value type text when i change the content of text area

I'm trying to update the variable description with the value of the textarea.
Here is my html:
<form action="index.php" method="post">
<div class="search">
<label for="animalId">Search for Id</label><br>
<input type="text" name="animalId" value="<?php echo $animalId; ?>">
</div>
<div class="animal_description">
<label for="animalDescription">Animal's description:</label><br>
<textarea name="animalDescription" id="animal-Description" cols="30" rows="10"><?php echo
$animalDescription; ?></textarea>
</div>
<button type="submit" name="Update">Update</button>
</form>
Here is my php code to update the description variable:
//if the update button is clicked
if (isset($_POST['update'])) {
//getting variable
$animalId = $_POST['animalId'];
$animalDescription = $_POST['animalDescription'];
//checking if any empty field
if(empty($animalId)){
$ERRORS['animal-description'] = "The id field is requiered";
}
else {
$idQuery = "UPDATE animals SET description='$animalDesription' WHERE id_num='$id'";
$stmt = $conn->prepare($idQuery);
if ($stmt->execute()) {
$ERRORS['final-message'] = "Successfully updated the database";
}
else {
$ERRORS['final-message'] = "Failed to connect";
}
}
}
When I enter an existent Id and some text in the textarea, it does nothing just refresh the page.
Try:
<form action="index.php" method="post">
<div class="search">
<label for="animalId">Search for Id</label><br>
<input type="text" name="animalId" id = "animalId" value="<?php echo
$animalId; ?>">
</div>
<div class="animal_description">
<label for="animalDescription">Animal's description:</label><br>
<textarea name="animalDescription" id="animalDescription" cols="30"
rows="10"><?php echo
$animalDescription; ?></textarea>
</div>
<button type="submit" name="Update">Update</button>
</form>
//if the update button is clicked
if (isset($_POST['Update'])) {
//getting variable
$animalId = $_POST['animalId'];
$animalDescription = $_POST['animalDescription'];
//checking if any empty field
if(empty($animalId)){
$ERRORS['animal-description'] = "The id field is requiered";
} else {
$idQuery = "UPDATE animals SET description=? WHERE
id_num=?";
$stmt = $conn->prepare($idQuery);
$stmt->bind_param("si", $animalDescription, $animalId);
if ($stmt->execute()) {
$ERRORS['final-message'] = "Successfully updated the database";
}
else {
$ERRORS['final-message'] = "Failed to connect";
}
}
}

$_GET variable not seen inside if statement

I have passed a variable using GET to a new page and will like to insert the value into a table in my database but it is saying undefined variable $exex. I tried to echo it inside the 'submit' if statement but it is not seen there either. It only echo the variable inside the isset GET if statement.
Below is the code:
<?php
session_start();
include "includes/connec.inc.php";
if (isset($_GET['exid'])){
$exex=$_GET['exid'];
}
$sql = "SELECT id FROM eaouser WHERE email = '" . $_SESSION['email'] . "'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$eaofid=$row['id'];
if (isset($_POST['submit'])){
if (isset($_POST['feedbacks'])){
$feedback = $_POST['feedbacks'];
if (!empty($feedback)){
$INSERT = "INSERT Into feedback (exfid,eaofid,comment) values(?,?,?)";
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("iis", $exex, $eaofid, $feedback);
$stmt->execute();
if ($stmt==TRUE){
echo "<script> alert('Feedback sent!');
window.location='feedback.php'
</script>";
}else{
echo "<script> alert('Error sending feedback!');
window.location='feedback.php'
</script>";
}
}else{
echo "<script> alert('Feedback form is empty!');
window.location='feedback.php'
</script>";
}
}
}
HTML:
<div class="panel-body">
<form method="POST" action="feedback.php">
<div class="form-group">
<label for="message-text" class="col-form-label">Comment on the experiment:</label>
<textarea class="form-control" name="feedbacks" cols="142" rows="5" placeholder="Type here..."></textarea>
<br>
<button type="submit" name="submit" class="btn" style="background-color: purple;color: white">Send feedback</button>
</div>
</form>
</div>

Data ain't changed after submitted to mysql

i have a code for updating data to myql. It looks doesn't have a problem but it ain't changed
my update code :
//previous data//
....
if (isset($_POST['update'])) {
$nim = mysqli_real_escape_string($connection, ($_POST['nim']));
$name = mysqli_real_escape_string($connection, ($_POST['name']));
$class1 = mysqli_real_escape_string($connection, ($_POST['class2']));
$class2 = mysqli_real_escape_string($connection, ($_POST['class1']));
if (!preg_match("/^[1-9][0-9]*$/",$nim)) {
$error = true;
$nim_error = "NIM only contain numbers";
}
if (!preg_match("/[^a-zA-Z]/",$name)) {
$error = true;
$name_error = "NIM only contain numbers";
}
if (!preg_match("/^[1-9][0-9]*$/",$class1)) {
$error = true;
$class1_error = "Class only contain numbers";
}
if (!preg_match("/^[1-9][0-9]*$/",$class1)) {
$error = true;
$class2_error = "Class only contain numbers";
}
$result = "UPDATE users SET nim='$nim', name='$name', class1='$class1', class1='$class1' WHERE id='$id'";
mysqli_query($connection, $result);
}
?>
and this is my html code :
<div id="popup2" class="overlay">
<div class="popup">
<h2 class="range2">Edit</h2>
<a class="close" href="#">×</a>
<div class="content">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input class="input" type="text" name="nim" placeholder="NIM" required/>
<input class="input" type="text" name="name" placeholder="Name" required/>
<i>SK</i>
<input class="input1" type="text" name="class1" placeholder="00" required/>
<i>-</i>
<input class="input1" type="text" name="class2" placeholder="00" required/>
<input name="update" type="submit" class="button" id="submit" value="Submit">
</form>
</div>
</div>
</div>
is there any wrong code ? Thank you..
It is really hard to explain: Take a look.
If you want to update a single data you will need a identity(Primary
key). That mean which data you want to update.
Below Example: check index.php file
In file index.php change dbname to your database name in connection.
browse project_url/index.php?id=1 [here use any id from your database]
Then update your data.
index.php
//Show existed data againist id
if(isset($_GET['id'])){
$id = $_GET['id'];
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->execute(array('id'=>$id));
$data = $stmt->fetch();
if (empty($data)) {
echo "No data found in user table. Use proper ID.";
}
}
//Update query
$msg = array();
if (isset($_POST['id']) && $_POST['id']!='') { //operation is update, because id exist
if($_POST['nim']!=0 && is_numeric($_POST['nim'])){
$nim = $_POST['nim'];
}else{
$msg[]="Nim only can be number";
}
if($_POST['name']!=''){
$name = $_POST['name'];
}else{
$msg[]="came only can not be empty";
}
if(is_numeric($_POST['class1'])){
$class1 = $_POST['class1'];
}else{
$msg[]="Class1 only can be number";
}
if(is_numeric($_POST['class2'])){
$class2 = $_POST['class2'];
}else{
$msg[]="Class1 only can be number";
}
$id = $_POST['id'];
if(count($msg)==0){
$stmt = $pdo->prepare('UPDATE users SET nim=:nim, name=:name, class1=:class1, class2=:class2 WHERE id=:id');
$result = $stmt->execute(array(
'nim' => $nim,
'name' => $name,
'class1'=> $class1,
'class2'=> $class2,
'id' => $id,
));
if($result){
echo "successfully updated.";
}else{
echo "update failed";
}
}
}else{
//You can run here insert operation because id not exist.
echo "Id not set";
}
?>
<div id="popup2" class="overlay">
<div class="popup">
<h2 class="range2">Edit</h2>
<a class="close" href="#">×</a>
<div class="content">
<?php foreach ($msg as $value) {
echo $value."<br>";
}?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php if(isset($data)){?>
<input class="input" type="hidden" name="id" value="<?php echo $data['id']; ?>" />
<?php } ?>
<input class="input" type="text" name="nim" value="<?php echo isset($data)?$data['nim']:''?>" placeholder="NIM" required/>
<input class="input" type="text" name="name" value="<?php echo isset($data)?$data['name']:''?>" placeholder="Name" required/>
<i>SK</i>
<input class="input1" type="text" name="class1" value="<?php echo isset($data)?$data['class1']:''?>" placeholder="00" required/>
<i>-</i>
<input class="input1" type="text" name="class2" value="<?php echo isset($data)?$data['class2']:''?>" placeholder="00" required/>
<input name="update" type="submit" class="button" id="submit" value="Submit">
</form>
</div>
</div>
</div>
My friend,
only do one thing to resolve this
echo $result = "UPDATE users SET nim='$nim', name='$name', class1='$class1', class1='$class1' WHERE id='$id'";
die;
then submit your form again and you will get your static query into your page then just copy that query and try to run into phpmyadmin then you will get your actual error.

keep the url same after form submission in php

I have this simple form that updates the values in database, the url of the page is
www.example.com?id=1
After i submit the form the values get updated and i get a success message but the url gets changed, it becomes
www.example.com
Can anyone tell how i can keep the url same i.e: www.example.com?id=1
The code is as follows
<?
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$text = mysqli_real_escape_string($con, $_POST['textvalue']);
$title = mysqli_real_escape_string($con, $_POST['title']);
$blogid = mysqli_real_escape_string($con, $_POST['blogid']);
$sql = "UPDATE blog SET text='".$text."', title='".$title."' WHERE id='".$blogid."'";
if (mysqli_query($con, $sql))
{
$msg = "Blog updated";
}
else
{
echo "There was an error";
}
}
?>
<div>
<?
if($msg!="")
{
echo $msg;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" >
<input type="text" name="title" value="<? echo $title; ?>" style="width:100%;"/>
<textarea name="textvalue" ><? echo $text; ?></textarea>
<input type="hidden" name="blogid" value="<? echo $blogid; ?>"/>
<input type="submit" name="edit" alt="edit" value="Edit"/>
</form>
</div>

Categories