Using UPDATE instead of INSERT

slashchuck 18 Mar, 2008
I'm using Joomla! 1.5
How do I use the form to UPDATE a table instead of INSERTing new rows? I changed the AutoGenerated code to:
<?php
$database =& JFactory::getDBO();

$database->setQuery( "UPDATE #__chronoforms_8 VALUES (
'' , '". date('Y-m-d')." - ".date("H:i:s")."', '".$_SERVER['REMOTE_ADDR']."' , '".JRequest::getVar('fullname','', 'post', 'string', '' )."' , '".JRequest::getVar('firstname','', 'post', 'string', '' )."' , '".JRequest::getVar('middlename','', 'post', 'string', '' )."' , '".JRequest::getVar('lastname','', 'post', 'string', '' )."' , '".JRequest::getVar('addressa','', 'post', 'string', '' )."' , '".JRequest::getVar('addressb','', 'post', 'string', '' )."' , '".JRequest::getVar('city','', 'post', 'string', '' )."' , '".JRequest::getVar('state','', 'post', 'string', '' )."' , '".JRequest::getVar('zipcode','', 'post', 'string', '' )."' WHERE 'uid'='$uid'");" );
if (!$database->query()) {
echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>
";
}
?>

I got:
Parse error: syntax error, unexpected '>' in /home/laborat2/public_html/lcs1/components/com_chronocontact/chronocontact.php(525) : eval()'d code on line 7<br><br>Post edited by: slashchuck, at: 2008/03/18 05:12
Max_admin 18 Mar, 2008
Sure, you have extra - " - after the $uid

Make sure the SQL statement is correct too!

Cheers

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
slashchuck 18 Mar, 2008
Max,

Thanks for the quick response.

I removed the extra ", and I no longer get any error messages. However the data is no longer being saved to the database.

Any ideas?

Basically I would like to display a form and allow them to change some data like if their address changed. That is why I'd like to use the "Update" as opposed to the original "Insert Into" I am having a rough time with the SQL syntax. Can you assist?

Thanks,

Chuck


<?php
$database =& JFactory::getDBO();

$database->setQuery( "UPDATE #__chronoforms_8 VALUES (
'' , '". date('Y-m-d')." - ".date("H:i:s")."', '".$_SERVER['REMOTE_ADDR']."' , '".JRequest::getVar('fullname','', 'post', 'string', '' )."' , '".JRequest::getVar('firstname','', 'post', 'string', '' )."' , '".JRequest::getVar('middlename','', 'post', 'string', '' )."' , '".JRequest::getVar('lastname','', 'post', 'string', '' )."' , '".JRequest::getVar('addressa','', 'post', 'string', '' )."' , '".JRequest::getVar('addressb','', 'post', 'string', '' )."' , '".JRequest::getVar('city','', 'post', 'string', '' )."' , '".JRequest::getVar('state','', 'post', 'string', '' )."' , '".JRequest::getVar('zipcode','', 'post', 'string', '' )."' WHERE 'uid'='$uid');" );
if (!$database->query()) {
echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>
";
}
?>
Max_admin 19 Mar, 2008
Hi Chuck,

I guess you will need to test with the SQL query completed with real static values first so you can get where the error isπŸ™‚

google "SQL update statement syntax" for more info too!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 19 Mar, 2008
Hi Chuck,

I believe that with UPDATE you have to use the SET syntax.

UPDATE #__chronoforms_8 
  SET 
    fullname = '".JRequest::getVar('fullname','', 'post', 'string', '' )."',
 firstname = '".JRequest::getVar('firstname','', 'post', 'string', '' )."',
. . .



Bob
This topic is locked and no more replies can be posted.