Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL UPDATE - Heeeeeeeelllllp!!!!!!!

Status
Not open for further replies.

pease

Programmer
Apr 2, 2009
29
GB
This is got to be something really really silly but I've now been banging my head off a desk for 5 hours with it.

Heres my code.

Code:
$thequery="INSERT INTO news VALUES('','$heading','$link','$blurb','$nowtime','$datekill')";
if ($hideid) {
	$thequery="UPDATE news SET heading = '$heading',link = '$link', blurb = '$blurb' WHERE id = $hideid"; 
}
	$result = $connector->query($thequery);	
echo $thequery;

Which is linked to a simple form for adding/ updating news items. When adding a new item it works a treat, no problems.

When amending an item, nothing happens. I dont get an error message and the echo produces:

Code:
UPDATE news SET heading = 'testheading',link = 'testlink', blurb = 'test the blurb here' WHERE id = 43

Record 43 definitely exists (its the one I just successfully added).

If I copy the echo above and paste it directly into the SQL of my phpMyAdmin it updates the record so I know the code is fine. Im now using the same code for three different forms on three different tables and in each case, have the same problem. Will create a new record but will not update one.

Ive checked the permissions to ensure that the relevant login is allowed to update and it is (I even created a whole new user and changed the details to that, but nothing).

Has anyone got any thoughts?
 
Are actually executing the query? Ccalling:

$result = $connector->query($thequery);

After setting the variable.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi pease,

What i can think of is there can be problem with your condition
Code:
if ($hideid) {

Is that condition is satisfied? You can print the query inside the condition to check.

Code:
if ($hideid) {
    $thequery="UPDATE news SET heading = '$heading',link = '$link', blurb = '$blurb' WHERE id = $hideid"; 
echo "$thequery<br>";
}
 
vacunita - yes, 5th line of the code does just that and it works when the action is an insert.

zoom - yes, the echo at the end produces the code shown in my 2nd snippet. Like I say, I then paste that directly into SQL and it executes.
 
this may help you debug the issue. with the mysqli extension (which I assume you are using) it is advisable to use placeholders.

Code:
<?php
$thequery="INSERT INTO news VALUES('','$heading','$link','$blurb','$nowtime','$datekill')";
if ($hideid) {
    $thequery="UPDATE news SET heading = ?,link = ?, blurb = ? WHERE id = ?"; 
	$s = $connector->prepare($thequery);
	if ($s=== false){
		printf("Error: %s<br>", $connector->error); 
	}
	$s->bindparams('sssd', $heading, $link, $blurb, $hideid);
} else {
	$thequery = "INSERT INTO news VALUES('',?,?,?,?,?)";
	$s = $connector->prepare($thequery);
		if ($s=== false){
		printf("Error: %s<br>", $connector->error); 
	}
	$s->bindparams('sssss', $heading, $link, $blurb, $nowtime, $datekill);
}
$result = $s->execute();
if ($result === false){
	printf("Error: %s<br>", $s->error);
} else {
	printf ("Query was successful.  %s rows were affected", $s->affectedRows()); 
}
?>
 
So what does $hideid contain and where does it get set ?
Just looking at your snippet in the first post you insert a new row and then update a news row - is this the same one or a differenyt one ?, are they related ?, do you need to get the last row inserted id ?. Where does 43 come from ?
Remember if you update a table and the where condition doesn't resolve any rows you don't get an error just zero row changed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top