Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UPDATE news WHERE news=%s

Author  Topic 

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 01:37:00
hi everyone, trying to make updates to a news area on a website through an update form.

have a table (news) with one row (news).

the record set displays correctly in my update form in a browser. after changing content and clicking update however, error displayed:
You have an error in your SQL syntax near 'WHERE news ='contents for news area'' at line 1

corresponding code:

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "newsForm")) {
$updateSQL = sprintf("UPDATE news WHERE news =%s",
GetSQLValueString($_POST['news'], "text"));

have tried multiple variations of the sql command. my question is do i have to add a row such as news_id and reference this as a primary key in order to use the UPDATE command? I have tried to do this but perhaps I should keep trying with that rather than with a single-row database. . .

it's just unclear where i should be looking. thanks anyone for your advice.

chris

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-15 : 01:39:44
This site is for posting MS SQL Server questions.

BTW, the syntax of UPDATE is:

UPDATE <table-name>
SET <column-name> = <value>
WHERE <condition>


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 01:43:38
thanks harsh, wasn't sure if SET was required. adding "SET news" still triggers the same error though.
You have an error in your SQL syntax near 'WHERE news ='contents for news area'' at line 1
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-15 : 01:45:17
Post your complete Update statement here..after adding SET.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 01:46:28
adding SET news = news does not trigger an error but neither does it execute the update. will keep trying
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 01:48:03
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "newsForm")) {
$updateSQL = sprintf("UPDATE news SET news = news WHERE news =%s",
GetSQLValueString($_POST['news'], "text"));

just also tried SET news = %s but the error states there are too few arguments . . .
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 01:51:20
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "newsForm")) {
$updateSQL = sprintf("UPDATE news SET news = news WHERE news =%s",
GetSQLValueString($_POST['news'], "text"));

mysql_select_db($database_gAdmin, $gAdmin);
$Result1 = mysql_query($updateSQL, $gAdmin) or die(mysql_error());

$updateGoTo = "news_insert.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_gAdmin, $gAdmin);
$query_getNewz = "SELECT * FROM news";
$getNewz = mysql_query($query_getNewz, $gAdmin) or die(mysql_error());
$row_getNewz = mysql_fetch_assoc($getNewz);
$totalRows_getNewz = mysql_num_rows($getNewz);
?>
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-15 : 01:51:45
What you want to update 'news' column with? updating news with itself is meaningless.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 01:56:36
i'm trying to update the news column string with the user-edited string in an update form . . .
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-15 : 02:05:12
I am not aware how this is done in MySQL. But in SQL Server, we build SQL statement like this:

Declare @colValue Varchar(20),
@Sql varchar(4000)

Set @colValue = 'SomeThing' -- I want to update column with this value

set @Sql = 'Update SomeTable
Set Col1 = ''' + @colValue + '''
Where col2 = 'some-condition''



You will have to consult your MySQL help for proper syntax.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 02:14:48
i'll look into the mysql syntax further, thanks for your help, appreciate it.
chris
Go to Top of Page

chrischris
Starting Member

8 Posts

Posted - 2007-03-15 : 03:26:17
resolved. needed to use another row as primary key . . .

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE news SET news_text=%s WHERE news_id=%s",
GetSQLValueString($_POST['news_text'], "text"),
GetSQLValueString($_POST['news_id'], "int"));
Go to Top of Page
   

- Advertisement -