1

Hi Guys could please check my code I have written, the problem is it won't update the data in DB, it just reloading and not giving any error. Here is my update.php

if (isset($_POST['update']) && isset($_POST['update']) != "") {
    $name = $_POST['c_name'];
    $mobile = $_POST['c_mob'];
    $dDate = $_POST['d_date'];
    $frame = $_POST['frame'];
    $size = $_POST['size'];
    $lense = $_POST['lense'];
    $descr = $_POST['descr'];
    $paid = $_POST['paid'];
    $remains = $_POST['remains'];
    $refer = $_POST['c_refer'];
}
$setquery = "UPDATE `dep_sale` SET c_name='$name', c_mob='$mobile', d_date='$dDate', frame='$frame', size='$size', lense='$lense', descr='$descr',c_refer='$refer', paid='$paid', remains='$remains' WHERE id='".$_POST["id"]."'";
mysqli_query($link, $setquery); 
header("location: reports.php");

And here is the HTML

<form method="POST" action="update.php">
    <div class="row">
      <div class="col-lg-4 col-md-4">
        <input type="hidden" name="id" />
        <input type="text" class="form-control" value="<?php echo $row['c_name']; ?>" required id="c_name" name="c_name" placeholder="Name..."/>
      </div>
      <div class="col-lg-4 col-md-4">
        <input type="phone" class="form-control" value="<?php echo $row['c_mob']; ?>" required id="c_mob" name="c_mob" placeholder="Mobile..."/>
      </div>
      <div class="col-lg-4 col-md-4">
        <input type="text" class="form-control" value="<?php echo $row['d_date']; ?>" required id="d_date" name="d_date"  onfocus="(this.type='date')" placeholder="Delivery Date..."/>
      </div>
    </div>
    <br>
    <div class="row">
      <div class="col-lg-4 col-md-4">
        <input type="text" class="form-control" value="<?php echo $row['frame']; ?>" required id="frame" name="frame" placeholder="Frame..."/>
      </div>
      <div class="col-lg-4 col-md-4">
        <input type="text" class="form-control" value="<?php echo $row['size']; ?>" required id="size" name="size" placeholder="Size..."/>
      </div>
      <div class="col-lg-4 col-md-4">
        <input type="text" class="form-control" value="<?php echo $row['lense']; ?>" required id="lense" name="lense" placeholder="Lense..."/>
      </div>
    </div>
    <br>
</form>

There is no problem in connection because i am fetching and inserting data perfectly.Please help me guys.Thanks

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Hamza Ali
  • 25
  • 5
  • `&& isset($_POST['update']) != "")` that doesn't do what you'd like it to do. – Funk Forty Niner May 04 '18 at 18:04
  • 1
    I also don't see a submit button – Funk Forty Niner May 04 '18 at 18:05
  • 1
    Remove the `header("location: reports.php");` and add `echo mysqli_error($link);` and you will be able to see the error. – Joas May 04 '18 at 18:07
  • @Joas That's only for connection errors. The OP would also need to check the for the rest of the [mysqli errors](http://php.net/manual/en/mysqli.error.php). – aynber May 04 '18 at 18:08
  • 1
    Your using `$_POST["id"]`, but in your form this is a hidden field without a value. – Nigel Ren May 04 '18 at 18:09
  • 3
    Do not use mysqli_query to execute a sql statement with parameters. Use prepared statements http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – Deadron May 04 '18 at 18:10
  • @aynber Thanks for the heads up, I copied the wrong function – Joas May 04 '18 at 18:11
  • 1
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman May 04 '18 at 18:13
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman May 04 '18 at 18:13
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman May 04 '18 at 18:13
  • @NigelRen so what should i do with this field or code – Hamza Ali May 04 '18 at 18:23
  • If the table is being built up from a result set, you could set the value from the result set - `$row['id']`? – Nigel Ren May 04 '18 at 18:33
  • Thanks, @NigelRen for fixing my problem.And also thanks all – Hamza Ali May 04 '18 at 18:57

1 Answers1

0

To see the errors set the following at first lines:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Your if is wrong. Try this:

if (isset($_POST['update']) && $_POST['update'] != "") {

Or more shorter version

if (!empty($_POST['update'])) {

Hope this helped you!

JohnnyDevNull
  • 951
  • 1
  • 8
  • 20