So I'm trying to have a relatively simple script. I want to just have one entry in the SQL table and when new information is entered just overwrite that same entry. For the table I have the primary key as the variable ID. I'm not sure how to just extract that one primary key from SQL, I can extract the entire row but then that over writes my new values for $text and $datetime with the ones that are already stored. So I tried just using this code to see if it would properly update and I'm getting an error:

mysql_query("UPDATE pptext SET text='$text', datetime='$datetime' WHERE id='1')",$link) OR DIE(mysql_error());

That returns a SQL syntax error near ')' error. I can't for the life of me figure out where the syntax is wrong. I know everything else is working in SQL because I texted an INSERT INTO statement and it inserted a new row with correct values. Also I'm getting those values from a post form.

If anyone can help me it'd be appreciated. I need help with extracting the id value from the database, rather than me putting the actual id value in there myself. I can extract the entire row but I don't know how to just get that one variable out.

Thanks in advance!

Recommended Answers

All 3 Replies

You have too many parenthesis after the sql query you have a ")" before the $link variable. Is that $link variable necessary?

thanks nonshatter that was it, I don't know why I didn't see that extra ")" earlier! Now how would I go about extracting just the id from the table? this is the code I use but it extracts all the variables and rewrites them, not just the id.

//fetch from db
					$fetch = mysql_query("SELECT * FROM pptext ORDER BY id DESC LIMIT 0,1",$link) OR DIE(mysql_error());		
							while($display=mysql_fetch_assoc($fetch)) {
								extract($display);

No probs darkdot. To extract just the ID from your table, replace the * with the column name of the PRIMARY KEY in your table pptext. * means select every column in the table.

A while loop is not required as you are only fetching one value from your table.

$fetch = ("SELECT ID FROM pptext",$link) or die(mysql_error());

$result = mysql_fetch_assoc($fetch);
echo $result['ID'];

Note: you may need to change "ID" to the name of the id column in your table

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.