2

I have a script that updates MySQL tables with the values from an HTML and processed by PHP. When I click on the edit link which is on the table it redirects me to the edit page, where it shows the records fetched from the MySQL database - but it does not update the record.

Here is my link code:

echo "<td><a href=\"cityproc.php?accode=$row[accode]\"><img src='images/edit.png'></a></td>";

Here is my edit page code:

<?php
session_start();
if (!isset($_SESSION["username"])) {
    header("Location: unauthorize_access.php");
}
mysql_connect("localhost", "root", '')or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());

$accode = mysql_real_escape_string($_REQUEST['accode']); // is used for both $_GET/$_POST variables

if(isset($_POST['submit']))
{

          $city = mysql_real_escape_string($_POST['city']);
          $result = mysql_query("UPDATE `city` SET `name`='$city' WHERE accode='$accode'") or die(mysql_error());

          echo "<b>Thank you! Record UPDATED Successfully!<br>You'll be redirected to Home Page after (1) Seconds";
          echo "<meta http-equiv=Refresh content=1;url=table.php>";
}
elseif($accode)
{

        $result = mysql_query("SELECT * FROM city WHERE accode='$accode' ");
        $myrow = mysql_fetch_assoc($result);

                $code = $myrow["code"];
                $city = $myrow["name"];
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link rel="stylesheet" type="text/css" href="form2/view.css" media="all">
<script type="text/javascript" src="form2/view.js"></script>
<script type="text/javascript" src="form2/calendar.js"></script>
</head>
<body id="main_body" >

    <img id="top" src="form2/top.png" alt="" />
    <div id="form_container">

        <h1><a>City</a></h1>
        <form id="city" class="appnitro" enctype="multipart/form-data" method="post" action="cityproc.php">
                    <div class="form_description">
        <h2>City</h2>
                <table border="0" width="100%">
                    <tr>
                        <td><?php echo $accode; ?></td>
                    </tr>
            </table>

        </div>
                    <table border ="0px" width="100%">
                        <input type="hidden" value="<? echo $myrow['accode']?>" name="accode"></input> 
                        <tr>
                            <td><label class="description" for="element_1">Code</label></td><td><input  name="code"  type="text" maxlength="6"  Placeholder="Please enter a code" value="<?php echo $code; ?>" disabled="disabled" /></td>
                        </tr>
                        <tr>
                            <td><label class="description" for="element_1">Name</label></td><td><input  name="city" size="40" type="text" maxlength="40" Placeholder="Please enter a name" value="<?php echo $city; ?>"/></td>

                        </tr>
                        <tr>
                            <td></td><td colspan="2" align="center"><input type="submit" name="submit" value="Save" /></td>
                        </tr>
                    </table>
                </form>
                </body>
    </html>
<?php
}
?>
2
  • What is the error you're receiving when you run that query? Commented Jan 18, 2013 at 6:53
  • use print_r($_POST); die('in form submit'); inside the if clause, and check the data.. Commented Jan 18, 2013 at 7:12

4 Answers 4

1
        $city = mysql_real_escape_string($_POST['city']);
        if(!empty($city)) {
            try {
                $result = mysql_query("UPDATE `city` SET `name`= '$city' WHERE accode='$accode'");
            } catch (Exception $e) {
                var_dump($e->getMessage()); // see what's the error.
            }

            if ($result) {
                echo $result;
            } else {
                echo $result;
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

1

As you have written all the code in the same page it is not required to write the form action here. On submit your page will be automatically refreshed. Try doing this once and try to debug the code by printing print_r($_POST) and see whether you get your data or not.

Also the query you have written is not correct. You have to write it as :

$result = mysql_query("UPDATE `city` SET `name`='" . $city . "' WHERE accode='" . $accode . "') or die(mysql_error());

2 Comments

yes i am getting my data on the form but after clicking the submit button its not updating the record
Ya but have you changed the line of query as I had written above? You need to change it like the above one may be then you get your updates correctly.
0

Write your PHP code in cityproc.php

and html code in another file say cityproc.html

then check and tell what is the error

Comments

0

Try this.... $link = mysql_connect("localhost", "root", '')or die(mysql_error()); mysql_select_db("webapp",$link) or die(mysql_error());

Or

mysql_query("UPDATE city SET name='$city' WHERE accode='$accode'", $link)

Echo the update query and run it manually in phpmyadmin.

4 Comments

there isn't any sort of error itsjust that after clicking on submit button the update query doesn't seems to update the record in my database
u need to write update code on file cityproc.php, which is the action of your form , or change the action of form and submit the form on same page
the form running itself is the action form nad its also showing the message echo "<b>Thank you! Record UPDATED Successfully!<br>You'll be redirected to Home Page after (1) Seconds"; but still its not updating the records]
First echo the update query, and run the query manually in phpmyadmin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.