# Update Data in mysql from php



## kurosagi01 (Dec 12, 2013)

Hey guys, its been awhile since i have last posted in the forum. I need some help getting my update php script working.
I've basically used this tutorial to help get me started.
So far i can get records from $row data from my list onto update.php file.
So it does identify the variable from list php file.
But from Update.php script it has a submit button which obviously when click it should update certain variable.
But i can't seem to get it working, soon as i click submit it doesn't do anything.
So i am assuming im missing something in my 3rd php file. 
Now i have tried numerous solutions such as $_Post[' ']; etc etc but still having no luck.


> update.php
> <?php
> include("johndb.php");
> 
> ...





> updatepage.php
> <? ob_start(); ?>
> <?php
> include("johndb.php");
> ...



Much help be appreciated.


----------



## silentbogo (Dec 12, 2013)

Look at the "updatepage.php":
You refer to the values of the target row as $CARNAME, $TRANSMISSION etc. But if you don't have the proper option enabled in php.ini, you'll need to update fields through $_POST:


```
<?php
include("johndb.php");
$carid = $_POST['CARID']; // Because your form's method is "post"


$query="UPDATE CAR_TABLE set CARNAME= '$_POST[CARNAME]', TRANSMISSION= '$_POST[TRANSMISSION]', ENGINESIZE= '$_POST[ENGINESIZE]', FUELTYPE= '$_POST[FUELTYPE]', DOORS= '$_POST[DOORS]', TOTAL= '$_POST[TOTAL]', AVAILABLE= '$_POST[AVAILABLE]', DATEADDED='$_POST[DATEADDED]' WHERE CARID='$carid'";
$result=mysql_query($query);


header("location:list.php");
?>
```

This is just a draft example, so i do not recommend to use it that way. Pharse and check the input before entering it into DB.


----------



## BiggieShady (Dec 12, 2013)

silentbogo said:


> ... if you don't have the proper option enabled in php.ini ...



Original code assumes that setting "register_globals = on" is in php.ini file. This is, however, not recommended. It's better, as silentbogo said, to use $_POST array to access the post variables ... and, of course, sanitize your inputs:


----------



## kurosagi01 (Dec 12, 2013)

silentbogo said:


> Look at the "updatepage.php":
> You refer to the values of the target row as $CARNAME, $TRANSMISSION etc. But if you don't have the proper option enabled in php.ini, you'll need to update fields through $_POST:
> 
> 
> ...



Just tried this and it didn't make any difference, the values are correct and the same how i have listed them in mysql and throughout my whole website. But still got no form of luck of getting it to work =/
I would sanitize the data but i don't necessary need too as this is just a simple website design.


----------



## silentbogo (Dec 12, 2013)

Ah! Another thing i have noticed is that you didn't close the mysql connection.
I'm  assuming johndb.php takes care of the connection, but there is nothing that closes it...
Also you can try and enable error reproting in php options or through error_reporting(E_ALL) in your code. See what's happening.


----------



## kurosagi01 (Dec 12, 2013)

How would i go about enabling error reporting?? All my codes are hand typed and not done via software.
EDIT:
Even when enabling error messages im sure it wouldn't work because soon as i click submit it doesn't display any error. It will take me back to my list.php file which doesn't show the updated value.


----------



## silentbogo (Dec 12, 2013)

at the beginning of your code insert

```
error_reporting(E_ALL);
```
this enables PHP to list all the errors including minor warning notices on the output page.
Usually error reporting is set to the minimal level, because it is not needed for any reason but debugging(which we are doing).

You can also open php.ini and change the error_reporting value to E_ALL
Don't forget to uncomment it.
This is what it looks like originally. As you can see the default value is show all errors except notices. For debugging purposes we should change that to E_ALL | E_STRICT. But the PHP official site says that E_ALL already includes E_STRICT, unless you are using PHP version lower than 5.

```
; error_reporting
;   Default Value: E_ALL & ~E_NOTICE
;   Development Value: E_ALL | E_STRICT
;   Production Value: E_ALL & ~E_DEPRECATED
```

The actual entry is

```
error_reporting=E_ALL
```


----------



## kurosagi01 (Dec 12, 2013)

Okay i got something lol


> *Warning*: Cannot modify header information - headers already sent by *updatepage.php* on line *13*


*
*


----------



## silentbogo (Dec 12, 2013)

that's why you have to close mysql connection every time you open it. It may not make much sense at first, but this is a good practice.


----------



## caleb (Dec 12, 2013)

Just like folks said before you need to access your variables from $_POST array and stick with that.
To debug you must:
1. get firefox and Firebug addon (press F12 to activate) and make sure your form works like it should
2. turn off the header line and start with echoing out your POST vars to be sure everything is received as it should. ( you can also inspect with Firebug)
3. it appears some data was already passed to the browser before the header line - that's why you have an error.


----------



## caleb (Dec 12, 2013)

mysql connection being left open is not a problem at all its just wasting resources which isn't really an issue nowday's.


----------



## Aquinus (Dec 12, 2013)

kurosagi01 said:


> Okay i got something lol



That just means headers and output has been sent and you're trying to send a http header after it has already been sent (it gets sent before any input and outputting any input will automatically output default headers.)


----------



## silentbogo (Dec 12, 2013)

so the problem is not mysql at all 
Keep us posted


----------



## kurosagi01 (Dec 12, 2013)

Still having no luck guys,i have checked debug on my form and i got syntax error on:


> <form name="update" method="post" action="updatepage.php">


I have also closed mysql connection aswell,deleted header from updatepage.php and still got no luck.
I have also tried echoing $_POST which seems to have no luck either.


----------



## BiggieShady (Dec 12, 2013)

```
<td><a href="updatepage.php?CARID=<?echo $row['carid'];?>"><input type="submit" value="Submit"></a></td>
```

This table cell is mangled - you have a submit button (with a bad html tag syntax, mind you) inside of a link.
Try using only a submit button and close the tag properly:


```
<td><input type="submit" value="Submit" /></td>
```


----------



## kurosagi01 (Dec 12, 2013)

No nothing works when i click submit, record does not update. =/


----------



## BiggieShady (Dec 12, 2013)

Use developer mode in browser (ctrl+shift+i in chrome or use firebug extension in firefox), check the request headers being sent and inspect the hidden field on the form to check if your record key is correct ... hopefully that will give you more info to help with your debugging.


----------



## kurosagi01 (Dec 12, 2013)

Been using the firebug and it doesn't seem to find any problems at all with headers.
This is just really confusing and frustrating me completely.

EDIT:
Oh my god as if, made a noob mistake ever i simply had to change 1 of the declare variable in update.php file and it works!

Thanks for help and advice guys. 
TPU as awesome as usual.


----------

