-3

HTML CODE:

<div class = "editprogramdetails" id = "editprogramblock" hidden = "true">
<?php
    $programid_query = @mysql_query("select id,program_name,company,date_prog from program_details");
    $row = @mysql_fetch_assoc($programid_query);
?>
ID: <input type = "textbox" id = "programnum" value = "<?php echo $row["id"] ?>" readonly/><br>
Program Name: <input type = "textbox" id = "prognameedit" placeholder = "<?php echo $row["program_name"] ?>" value = ""/><br>
Company Name: <input type = "textbox" id = "compnameedit" placeholder = "<?php echo $row["company"] ?>" value = ""/><br>
Date: <input type = "date" id = "dateedit" placeholder = "<?php echo $row["date_prog"] ?>" value = ""/><br>
<input type="button" class = "btn btn-default" id = "updatebutton" value ="Update"></input>

JQUERY CODE:

 <script>

    $("#updatebutton").click(function(){
        var programidphp = $("#programnum").val();
        var programnamephp = $("#prognameedit").val();
        var companynamephp = $("#compnameedit").val();
        var datephp = $("#dateedit").val();

        var updaterequest = {
            upprogid = programidphp;
            upprognam = programnamephp;
            upcompnam = companynamephp;
            uppdate = datephp;
        };
        $.post("/TrainerApp/update_program.php", updaterequest).done(function(data){
            alert(data);
        }).fail(function(){
            alert("Failed");
        });

    });
    </script>

update_program.php:

<?php


$username = "trainerapp";
$password = "password";
$hostname = "localhost";

$link = @mysql_connect($hostname, $username, $password);
//echo $link;

if(@mysql_select_db("trainer_registration"))
{
    echo "Connected successfully";
}
else
{
    echo "Connection Error";
}

$upprogid = $_POST["upprogid"]
$upprognam = $_POST["upprognam"];
$upcompnam = $_POST["upcompnam"];
$uppdate = $_POST["uppdate"];

$upd_query = @mysql_query("UPDATE program_details SET program_name = '$upprognam', company = '$upcompnam', date_prog = '$uppdate' where id = '$upprogid'");

echo "Updated Successully";
?>

I am trying to update the values in the database. I don't know what mistake I am doing but I dont see any updates in the database. The username, password for the database is perfect because insertion works whereas update doesnt work. Please help.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Vignesh Anandakumar
  • 167
  • 1
  • 3
  • 12
  • 1
    What error you are getting? As i see in your update programe file you have written insertion code. – Manish Shukla Apr 28 '15 at 11:36
  • 1
    Where is your update query? – Naruto Apr 28 '15 at 11:37
  • 4
    [You need to prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 28 '15 at 11:37
  • 1
    'upcompanynam' does not exist in your POST. it's 'upcompnam' – Dan Bizdadea Apr 28 '15 at 11:38
  • Add error checking, such as `or die(mysql_error())` to your queries. *Stop suppressing errors from your database interactions.* – Jay Blanchard Apr 28 '15 at 11:41
  • you write insert query and asking for update why? – Saty Apr 28 '15 at 11:41
  • is this a typo `$row = @mysql_fetch_assoc($programid_query)` <= missing a semi-colon at the end – Funk Forty Niner Apr 28 '15 at 11:57
  • Please don't dump code in comments. Edit your original post to add any new information @VigneshAnandakumar – Jay Blanchard Apr 28 '15 at 12:02
  • @JayBlanchard I'm new to this.. I've made changes in the original post and I get no errors when I run this but still I dont see any update in the database.. – Vignesh Anandakumar Apr 28 '15 at 12:05
  • `type = "textbox"` that's invalid. too many things wrong here. – Funk Forty Niner Apr 28 '15 at 12:06
  • Where are your form tags? – Jay Blanchard Apr 28 '15 at 12:13
  • There were a few small corrections in the code, which I managed to solve by looking at console and also thank you for the comments.. I happened to check the code at the specific points where you guys spotted the mistake.. Thank you so much.. :) – Vignesh Anandakumar Apr 28 '15 at 12:24
  • so tell me; did you not see the answer I posted? silence isn't always golden you know. I hope I didn't do all this for nothing. – Funk Forty Niner Apr 28 '15 at 20:42

1 Answers1

0

Nota: This is too long for comments.

Firstly, there is no type = "textbox" - <input type = "textbox">

  • The syntax is <input type = "text">

So, change all of those to that format.

You are using @ symbols often; those are error suppressors and won't help you during testing.

  • I suggest you remove them (for now).

Add or die(mysql_error()) to mysql_query() to see if your query is failing.

  • If all of those changes still don't work, you may have to add <form></form> tags with a POST method and check for errors.

  • Since you're using jQuery, make sure you've loaded the library and using the right doctype for it.

  • UPDATE is for existing data. If there is no data to update, then you may have wanted to use INSERT INTO. Only you know that.

About your Update button:

<input type="button" class = "btn btn-default" id = "updatebutton" value ="Update"></input>

You can safely remove </input> it isn't a valid closing tag.

or use type="submit" for it.

<input type="submit" class = "btn btn-default" id = "updatebutton" value ="Update">

However, you may want to use a button with a submit type for this,

Button with submit type:

<button type="submit" class = "btn btn-default" id = "updatebutton">Update</button>

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Your present code is open to SQL injection. use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


"The username, password for the database is perfect because insertion works whereas update doesnt work."

  • You will need to make the comparison between those two different pieces of code in order to see what mistake you may have made.

  • I believe you have enough to get you started.


Final note(s):

Try running your code without the jQuery/JS and adding a form with a POST method and an action to, i.e.: <form action="/TrainerApp/update_program.php" method="post">...</form>

If it works, then you may be having path problems with $.post("/TrainerApp/update_program.php"

and may need to use something like $.post("../TrainerApp/update_program.php"
or $.post("/home/var/usr/public/TrainerApp/update_program.php" as a full system path.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141