Wednesday, November 25, 2009

PHP MySQL update script not working on older webserver?

Hi,



I have this code. It works fine using PHP 4 and MySQL 5.0. but it doesn't work at all on my webhost (php4 mySQL 4) and I cannot for the life of me figure out why.



Here's my code:



/%26lt;?



// Connect database.



include("connectdb.php");



//if "Submit" button is clicked



if($_POST['Submit']){



//Get parameters from form



$id=$_GET['id'];



$name=$_POST['name'];



$email=$_POST['email'];



$tel=$_POST['tel'];



//Update database record



mysql_query("update phonebook set name='$name', email='$email', tel='$tel' where id='$id'");



//Redirect to record view page



header("location:select.php");



exit;



}



Any ideas?



PHP MySQL update script not working on older webserver?microsoft windows



Why are you using GET for the ID when all other variables are coming across as POST? Have you tried using $id = POST['id'] instead? I bet GET['id'] is coming up null, so there's nothing to update as far as the query is concerned.



I would comment out the update query and header and output all variables to see that they are populated:



foreach($_POST as $key =%26gt; $value) {



echo "%26lt;p%26gt;$key: $value (post)%26lt;/p%26gt;";



}



foreach($_GET as $key =%26gt; $value) {



echo "%26lt;p%26gt;$key: $value (get)%26lt;/p%26gt;";



}



PHP MySQL update script not working on older webserver?windows media player 10 internet explorer



It'd help if you listed the error you are getting...



I'm wondering...have you adjusterd your connectdb file for the new database?



Like I said...it's hard to tell without knowing what error you get...considering that the code above looks correct...
email='$email', tel='$tel' where id='$id'");



why have that double quote that doesnt make sense to me.
It actually looks fine. I would first check your connectdb.php file, and I feel I'm obligated to remind you about htmlspecialchars, and sql injection. (If you don't know about the two, please spend the rest of the day googling them.)



Also, try using a db identifier in your mysql query, and see if it works if you connect to the db in the script.



So, instead of include(), just put you're db info, with error identifiers, if you haven't already:



$conn = mysql_connect("localhost","NAME","PASS")...



if (!$conn)



{



die('Could not connect to db



' . mysql_error());



}



mysql_select_db("DB_NAME",$conn);



and for your query, try mysql_query("SELECT....",$conn);



Also, the "exit;" at the end isn't really necessary there.
Try this:



if(isset($_POST['Submit']))



instead of your if($_POST....



Also, double check the form fields' names including the submit button.
I would recommend using a paste bin for so much code, it's far too hard to read with no indentation and syntax highlighting. That way I can assist and possibly fix your code in no time at all.

No comments:

Post a Comment

 
c++