Updation query executes but value is not updating in database

I’m having the following code

<?php
 include 'db_config.php';
 include 'index.php';
 $supp_name=$supp_location=$supp_contact=$supp_mail=$supp_bank=$supp_id="";
 $update=false;
 if (isset($_GET['supplier_id'])) 
 {
 	$update=true;
 	$id=$_GET['supplier_id'];
 	$supp_select="SELECT * FROM supplier_details WHERE supplier_id='$id' ";
 	$supp_query=mysqli_query($con,$supp_select);
 	$supp_fetch=mysqli_fetch_array($supp_query);
 	$supp_id=$supp_fetch['supplier_id'];
 	$supp_name=$supp_fetch['supplier_name'];
 	$supp_location=$supp_fetch['supplier_location'];
 	$supp_contact=$supp_fetch['supplier_contact'];
 	$supp_mail=$supp_fetch['supplier_mail'];
 	$supp_bank=$supp_fetch['supplier_bank']; 
 }
if (isset($_POST['submit']))  
 	{
 	$id=$_POST['supplier_id'];
 	$s_name=$_POST['supplier_name'];
 	$s_location=$_POST['supplier_location'];
 	$s_contact=$_POST['supplier_contact'];
 	$s_mail=$_POST['supplier_mail'];
 	$s_bank=$_POST['supplier_bank'];

	 	if ($update==false) 
	 	{
	 	$insert="INSERT INTO supplier_details(supplier_name,supplier_location,supplier_contact,supplier_mail,supplier_bank) VALUES('$s_name','$s_location','$s_contact','$s_mail','$s_bank')";
	 	$query=mysqli_query($con,$insert);
		 	if ($query) 
		 	{
		 		echo "<script>alert('Supplier added successfully!');window.location=add_supplier.php;</script>";
		 	}
 		}
 		else
 		{
 			$supp_update="UPDATE supplier_details SET supplier_name='$supp_name',supplier_location='$supp_location',supplier_contact='$supp_contact',supplier_mail='$supp_mail',supplier_bank='$supp_bank' WHERE supplier_id='$id'";
 			$supp_updt_query=mysqli_query($con,$supp_update);
 			if ($supp_updt_query) 
 			{
 				echo "updated successfully";
 			}
 		}
	}
?>
<div class="container">
	<div class="page-content">
		<div class="page-content-inner">
			<div class="page-main-header">
				<h3>Add new supplier</h3>
			</div>
			<div class="page-main-content">
				<form name="add_supplier" method="post" enctype="multipart/form-data">
					<input type="hidden" name="supplier_id" value="<?php echo $id;?>">
					<div class="form-group row">
						<label for="supplier_name" class="col-sm-2 col-form-label col-form-label-sm">Supplier name</label>
						<div class="col-sm-5 ">
							<input type="text" name="supplier_name" class="form-control form-control-sm" id="colFormLabelSm" value="<?php echo $supp_name;?>">
						</div>
					</div>

					<div class="form-group row">
						<label for="supplier_location" class="col-sm-2 col-form-label col-form-label-sm">Supplier location</label>
						<div class="col-sm-5">
							<input type="text" name="supplier_location" class="form-control form-control-sm" id="colFormLabelSm" value="<?php echo $supp_location;?>">
						</div>
					</div>

					<div class="form-group row">
						<label for="supplier_contact" class="col-sm-2 col-form-label col-form-label-sm">Contact number</label>
						<div class="col-sm-5">
							<input type="text" name="supplier_contact" class="form-control form-control-sm" id="colFormLabelSm" value="<?php echo $supp_contact;?>">
						</div>
					</div>

					<div class="form-group row">
						<label for="supplier_mail" class="col-sm-2 col-form-label col-form-label-sm">Email Id</label>
						<div class="col-sm-5">
							<input type="email" name="supplier_mail" class="form-control form-control-sm" id="colFormLabelSm" value="<?php echo $supp_mail;?>">
						</div>
					</div>

					<div class="form-group row">
						<label for="supplier_bank" class="col-sm-2 col-form-label col-form-label-sm">Bank details</label>
						<div class="col-sm-5">
							<textarea name="supplier_bank" class="form-control form-control-sm" id="colFormLabelSm"><?php echo $supp_bank;?>
							</textarea>
						</div>
					</div>
					<?php
					if ($update==false) 
					{
						$btnValue="Add details";
					}
					else
					{
						$btnValue="Update";
					}
					?>
					<div class="form-group row" style="margin-left: 17%;">
						<input type="submit" name="submit" class="btn btn-success col-sm-2" value="<?php echo $btnValue;?>">
						<input type="reset" name="reset" class="col-sm-2 btn btn-danger" value="Reset" style="margin-left:5px;">
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

The code for updation is executing but the datavalue is not updating in the database.I couldn’t figure out the issue.Can anyone help me please

echo out your statement and run it in an external tool; check if all conditions match.

The PHP variable names that you use to copy the form variables are not the same as the names that you use to insert those variables, without any validation, into the query.

$id=$_POST['supplier_id'];
$s_name=$_POST['supplier_name'];
$s_location=$_POST['supplier_location'];
$s_contact=$_POST['supplier_contact'];
$s_mail=$_POST['supplier_mail'];
$s_bank=$_POST['supplier_bank'];

and

$supp_update="UPDATE supplier_details SET supplier_name='$supp_name',
   supplier_location='$supp_location',supplier_contact='$supp_contact',
   supplier_mail='$supp_mail',supplier_bank='$supp_bank' WHERE supplier_id='$id'";

You should also look at prepared statements rather than concatenating strings into the query. What happens when your supplier name is O'Grady Products ?

1 Like

Why are you looking in the GET array for a value you’re POST’ing?

Additional: Simplify your life.
INSERT...ON DUPLICATE KEY UPDATE

I wondered about that, and figured that the page is initially opened with the supplier ID in the URL, that information is used to populate the form, and then the form is submitted with method="post" whereupon the $_POST array is filled, and the $_GET array is not.

Yeah, but the update query is triggered by the presence of the $_GET variable.

No, the update query only executes if the $_POST variable was set, and if $update==true (which means the supplier-id came in as part of the URL) - if the $_POST variable was set and $update == false, then it must be a new supplier, so probably called without the $_GET variable set.

Or it would be, except that the value of $update is only passed through as the value of the submit button text, which doesn’t seem to be looked at anywhere.

OP said the update query is actually executing, though, but not changing the values. Not sure how, as the logic suggests it can only do that if $update==true, and I can’t see how it can be.

Thankyou @droopsnoot.It was resolved.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.