1

I'm going to build a mini system with login,add,edit,delete function. Login, add and delete was functioning without a problem but not on Edit. This is my update.php code. FYI, this code doesn't have any error displayed.

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_NOTICE);
require_once "conn.php";

$conn=connect();
$db=connectdb();

$ic= "";
$parcelno = "";
$items = "";
if(isset($_REQUEST['ic'])){ $ic= $_REQUEST['ic']; }
if(isset($_REQUEST['parcel'])){ $parcel = $_REQUEST['parcel']; }
if(isset($_REQUEST['items'])){ $items = $_REQUEST['items']; }


mysqli_select_db($conn, $db) or die (mysqli_error($conn)."\n");
$sql="UPDATE parcel SET parcelno='".$parcel."', items='".$items."' where ic='".$ic."'";
$result=mysqli_query($conn,$sql) or die(mysqli_error($conn)."\n");

}
  • 2
    ok then what? whats the question? – avisheks Aug 02 '14 at 11:41
  • @avisheks cant update data from ysql database, whats wrong with my update.php code?? – Amirul Omar Aug 02 '14 at 13:55
  • How do you mean by not updating? Is it end up with any error? Try printing the raw query? – avisheks Aug 02 '14 at 16:57
  • @avisheks no error, but after clicking submit button on update page, the data is not updated in mysql database – Amirul Omar Aug 03 '14 at 02:50
  • get the raw query and put it directly to the database thorough some mysql client[phpmyadmin/mysql] – avisheks Aug 03 '14 at 07:35
  • Try `print_r $result`. Maybe the query doesn't satisfy the `WHERE` condition of the update, so it may be updating nothing. That won't give an error because it simply did not see anything to update or rather, it update 0 things. The `print_r` will give you more info about `$result` so you will see its content. – afaolek Aug 04 '14 at 18:32
  • Better still, try `mysqli_affected_rows()`. – afaolek Aug 04 '14 at 18:35
  • possible duplicate of [Can't update data from MySQL database](http://stackoverflow.com/questions/25091448/cant-update-data-from-mysql-database) – Jad Oct 30 '14 at 17:07

1 Answers1

0

your code didn't show errors, but it won't work..
you should define tables' and columns' names in your DB & your form fields' names as well..
In your code, they aren't clear (sometimes you are using parcel and sometimes parcelno as variables, those two are different. Both variables MUST match)

Now, let's consider you have a form where the user inserts new values to update the table in the database, after including the connections.php...

  1. table name: tname
  2. form fields: Ic Parcel Items
  3. table columns: ic parcel items

When the user click Save:

    if(isset($_POST['Save']))
    {
        $ic= $_POST['ic'];
        $parcel= $_POST['parcel'];
        $items= $_POST['items'];

    $query= "UPDATE tname SET Ic='{$ic}', Parcel='{$parcel}', Items='{$items}'", 
    $res= mysql_query($query);
    if (!$res){echo mysql_error()};
    }

you can then return $res and display the fields updated.

This is the code for updating in MySQL.

But it is better to use mysqli_affected_rows().

Jad
  • 479
  • 2
  • 10
  • 23