maxim Posted August 2, 2007 Share Posted August 2, 2007 im pretty baffed by this problem im having. Basicly i have a script which displays a entry from a database. i then call that same database record again so i can display it in a HTML form. now the code works the values of the database record get displayed in a text feild and text area of this form (so the user can edit its content). however when the submit button gets pressed the query to update the record never gets executed. to make sure the code is being run when the user presses the submit button i put and echo statement. and sure enough the button works and it echos the sting to the screen. the problem is the the UPDATE sql statement never happens. now if i take the SELECT statement,query call and fetch call and remove it from this code. the UPDATE statement will work. so my question is WHY ? why cant i execute 2 calls to my database ? <?php if ($_GET['action'] == 'edit') { $sql = "SELECT * FROM posts WHERE id = '".$_GET['id']."';"; $result = $db_handle->query($sql); $selected_post = $result->fetch(); //<input type="text" name="title" value=" $selected_post['title']"/> (this form is in an include) // submit button code // (this form is in an include and is there for edited its fine tho) if ($_POST['submit'] == true) { //this line echos just fine echo $edit = "UPDATE posts SET title='".$_POST['title']."',content='".$_POST['content']."',author_id=1,page='".$_POST['pages']."' WHERE id=".$_GET['id'].";"; //this db query never gets called it wont work (it will work when the previous slect query is removed however) $db_handle->exec($edit); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/ Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 $edit = "UPDATE posts SET title='".$_POST['title']."',content='".$_POST['content']."',author_id=1,page='".$_POST['pages']."' WHERE id=".$_GET['id']; echo $edit; What is the difference in the query and exec, I think you need to use query for the edit statement too. Unsure as I do not know the $db_handle class and how it works. Generally you should not put ; in the sql statements, as php cannot do 2 seperate queries in one line it can throw it off. Try removing those and try the above. See what happens. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314415 Share on other sites More sharing options...
simcoweb Posted August 2, 2007 Share Posted August 2, 2007 Also, you have 'echo' for your 2nd query. Remove the 'echo' and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314419 Share on other sites More sharing options...
maxim Posted August 2, 2007 Author Share Posted August 2, 2007 What is the difference in the query and exec i use PHP5's PDO for database access/abstration query returns a result set. (select statments where you want to see what has been returned) exec just returns the number of rows effected by your query. (Its a UPDATE query so i dont do the query method because i dont need anything returned) i also removed the ";" from the end of the SQL statements. but this didnt seem to do anything. thanks for the suggestions tho. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314421 Share on other sites More sharing options...
maxim Posted August 2, 2007 Author Share Posted August 2, 2007 Also, you have 'echo' for your 2nd query. Remove the 'echo' and it should work. no it wont, the echo is simply there as i stated in my first post to see if any code will work after a user presses the submit button. the echo simply echos the sting. the string is still a vaild and working SQL statement. it just wont get executed while the above SELECT statement is there Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314426 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 heres my thought hmm you have the update inside the condition saying if get but you still have the condition for post its impossible to have get variable and post variable at the same time so thats the error i seee try to put outside the if ($_GET['action'] == 'edit') { your update then it will work Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314431 Share on other sites More sharing options...
maxim Posted August 2, 2007 Author Share Posted August 2, 2007 interesting, i did as you suggested putting the UPDATE condition statement out side the SELECT condition, with the same results, the code works the same when it is nested or outside. so im still at square 1. its defiantly possible to have a GET and POST at the same time. i have a delete query which works this way. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314436 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 look get is use for query string !!! and post is use to for submission of form so now can you click on the link to obtain the get and click the submit at the same time hmm your amazing Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314441 Share on other sites More sharing options...
simcoweb Posted August 2, 2007 Share Posted August 2, 2007 Maxim, when you say it echoes what does it echo? The query code/statement? Using GET and POST just requires you use something like action = "process.php?id=whatever" Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314443 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 Maxim, when you say it echoes what does it echo? The query code/statement? Using GET and POST just requires you use something like action = "process.php?id=whatever" is this what you mean??? Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314447 Share on other sites More sharing options...
maxim Posted August 2, 2007 Author Share Posted August 2, 2007 it just echos the SQL statement, just to see if the submit button works which it dose because it echos teh SQL string. remove the echo nothing happens this is the line that wont work $db_handle->exec($edit); now if i remove the following code from what i posted. $sql = "SELECT * FROM posts WHERE id = '".$_GET['id']."';"; $result = $db_handle->query($sql); $selected_post = $result->fetch(); then this line $db_handle->exec($edit); WILL work. my question is why or at least how do i get it to work ? Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314450 Share on other sites More sharing options...
simcoweb Posted August 2, 2007 Share Posted August 2, 2007 Ok, cool. Then lets assume that we know it's working. It would be beneficial now to echo the results of the query to see if it contains anything. $edit = "UPDATE posts SET title='".$_POST['title']."',content='".$_POST['content']."',author_id=1,page='".$_POST['pages']."' WHERE id=".$_GET['id'].""; $results = mysql_query($edit) or die(mysql_error()); foreach($results as $item) { echo $item; } Let's see if those are populating. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314454 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 just a guess maybe your db connection is within those with the select try use mysql_query and put a die if fails to see the error that causing this prob???? Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314456 Share on other sites More sharing options...
maxim Posted August 2, 2007 Author Share Posted August 2, 2007 i can var_dump the database handle (connection) and it is definatly there. the database hande is within the same scope. i dont even have any user defined functions at all. im using SQLite not MySQL and PDO for databse access/abstration. sorry i should have said that before. it is a update query. it updates 1 row and returns the number of rows effected. in my case it returns nothing, however if i remove these lines form the code i posted $sql = "SELECT * FROM posts WHERE id = '".$_GET['id']."';"; $result = $db_handle->query($sql); $selected_post = $result->fetch(); then it will return 1, meaning that it works. i however need to run BOTH querys. the first query populates a HTML form with a record from the database and works fine the second query UPDATES the record with the information the user types(edits) in the html form. it is this query that dose not work. is only works if i remove the first. which i cant do as i need the form feild populated so the data can be edited. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314462 Share on other sites More sharing options...
maxim Posted August 3, 2007 Author Share Posted August 3, 2007 for whatever reason i think the problem is with PDO. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314591 Share on other sites More sharing options...
maxim Posted August 3, 2007 Author Share Posted August 3, 2007 any takers ? still cant work this one out Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314906 Share on other sites More sharing options...
webgrrl Posted August 3, 2007 Share Posted August 3, 2007 you could try putting if ($result=="1") { do update query } Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-314912 Share on other sites More sharing options...
premiso Posted August 3, 2007 Share Posted August 3, 2007 for whatever reason i think the problem is with PDO. Chances are you are right. Is there a clean function or something, it seems as though it is keeping the same sql statement and trying to use it for the exec, which of course probably does not except select statements. The problem is obviously with the PDO class and the exec code. If the creator has a forum or email check there. Chances are not many people worked with that class and will be of no help to you here. Quote Link to comment https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/#findComment-315015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.