...

View Full Version : query wont run in page



KingOfKYA
07-15-2007, 06:59 PM
ok i need help with this page that updates a mysql database but when it runs i get no errors and it looks like it ran but it never does. also the query is right because if i take it from the page it runs fine.

php part of code:

<?php




$username="xxxxxx";
$password="xxxxxx";
$database="xxxxxxx";

$id=$_POST['id'];
$name=$_POST['name'];
$wage=$_POST['wage'];
$date=$_POST['date'];
$job=$_POST['job'];
$type=$_POST['type'];
$des=$_POST['des'];
$hours=$_POST['hours'];



//get query setup to run

$query = "UPDATE timecard SET `name` = '$name' , `wage` = '$wage' , `date` = '$date' , `job` = '$job' , `type` = '$type' , `des` = '$des' , `hours` = '$hours' , id = '$id' WHERE id = '$id'";



//connect to mysql

mysql_connect(localhost,$username,$password);



//send and run query

$result = mysql_query($query);


mysql_close();

print $result;

//just for test delete later

print $query;

?>

guelphdad
07-15-2007, 07:28 PM
look in the php manual, you want to use mysql_fetch_array or mysql_fetch_assoc and adapt your script with that.

also note you have no error messaging whatsoever, so you should look into that as well. look at the use of mysql_error() in the php manual as well.

phpandmysql
07-15-2007, 07:45 PM
Change


$result = mysql_query($query);

to


$result = mysql_query($query) or die(mysql_error());

and run the script to see the mysql errors.

If you have magic quotes on ignore the following:
Also I noticed you do not have any addslashes on your variables. If one of the posts contains a character like ' or " it could be trying to read it as part of the code. ie mysql injection.

KingOfKYA
07-15-2007, 08:19 PM
Thank you for the help, i will go look at the functions later by why does mysql not give an error for it being incorrect. when i use the error function VERY CONFUSED

phpandmysql
07-15-2007, 08:27 PM
Thank you for the help, i will go look at the functions later by why does mysql not give an error for it being incorrect. when i use the error function VERY CONFUSED

It won't give you the error message unless you ask for it as I posted above.
Why? because sometimes you may not want other to see your errors for security reasons.

KingOfKYA
07-15-2007, 10:18 PM
sry about my confusing post what i meant is i tried it and it returns no error

phpandmysql
07-16-2007, 04:09 AM
sry about my confusing post what i meant is i tried it and it returns no error

Oh yeah cool.

Sorry none of these posts have been helpful, here is another attempted debug. I noticed you have.


//connect to mysql

mysql_connect(localhost,$username,$password);

but I dont see


mysql_select_db("$database");

Do you have that in your script?

_Aerospace_Eng_
07-16-2007, 06:54 AM
And this

mysql_connect(localhost,$username,$password);
should be

mysql_connect('localhost',$username,$password);

CFMaBiSmAd
07-16-2007, 01:30 PM
Actually, I think there was no mysql_error() because there was no connection to the mysql server. The mysql_query(...) statement would have attempted to create a connection using default values, failed, and issued a PHP Warning message. Error output is likely turned off in php.ini and you don't see anything.

Either check your web server log for errors and/or turn on full PHP error reporting. When learning a programming language or writing and debugging code, making use of all the error reporting that the language provides will help you find syntax errors, problems with variables (typos, referencing non-existent or un-initialized variables), and failed calls to functions...

Aerospace_Eng's post will likely correct the connection error and phpandmysql's mysql_select_db(...) code will properly select a database.

I realize you want to write code quickly to see what the results are, but by writing minimal code that has no error checking logic of the returned values from the mysql_connect(), mysql_select_db(), and mysql_query() function calls (the code examples in the PHP manual under each of these functions shows one possible way of doing this using "if" tests of the returned value - most people prefer the "or die(...)" method) also gives you no indication of why it does not work and results in a blank page that leaves you wondering what is going on.

Edit: Once you get the query working, I notice a logic problem. You are setting/getting the "id" from the form. This is used in WHERE id = '$id'. If you ever change this to a new value in the form, the query will not find any matching rows and the UPDATE won't change anything (or it will change some other row matching the newly entered value.) You probably don't want to allow the id to be changed on the form as this is the "key" you are using to insure which record is being updated. The $id should probably be passed to this code in a session variable so that it is not visible or alterable by anyone and you should probably not have the id be in the list of SET... values.

You might want to look at using the mysql_affected_rows() function to check if the UPDATE changed any rows (note that if all the new values in the UPDATE are the same as the existing values, the UPDATE won't change anything and mysql_affected_rows() will be zero too) - http://php.net/mysql_affected_rows



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum