Jump to content

Recommended Posts

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);
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/63110-cant-run-2-sql-queries-in-a-if-statement/
Share on other sites

$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.

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.

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

 

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

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.

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 ?

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.