Jump to content

[SOLVED] Syntax error


abitlikehomer

Recommended Posts

Hi all

 

First post here so go easy on me. Currently doing some coding to ensure that when a user posts a new story that is added to the database the link that is added auto increments. I've got this working but there appears to be a syntax error and I don't know how to fix it. Any help would be appreciated.  :)

 

Here's my code:

<?php
include "connection.php"
?>
<?php 

// (2) gather all details from form
$newTitle= $_POST['txtTitle'];
$newDay = substr($_POST['txtDate'],0,2);
$newMonth = substr($_POST['txtDate'],3,2);
$newYear = substr($_POST['txtDate'],6,4);
$newContent = $_POST['txtContent'];
$newLink = "<a href=http://www.2asmooth.com/Kingfisher_Trust/showNews.php?"."NewsID=0>Link</a>";

// (3) Create an INSERT query of the form  
$query = "INSERT INTO News (Title, Date, Content, Link) VALUES ('$newTitle', '$newYear''$newMonth''$newDay', '$newContent', '$newLink')"; 

// (4) Run query through connection
$result1 = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// (6) Create an UPDATE query of the form to include the new link 
$query2 = "UPDATE News SET Link = <a href=http://www.2asmooth.com/Kingfisher_Trust/showNews.php?NewsID=".mysql_insert_id($connection).">Link</a> WHERE Title =".$newTitle." AND Content =".$newContent; 

// (4) Run the query on the customer table through the connection
$result2 = mysql_query($query2) or die ("Error in query: $query2. ".mysql_error()); 

//  print message with ID of inserted record    
header("Location: newsReceipt.php?"."NewsID=". mysql_insert_id($connection));   

// (5) close connection 
    mysql_close($connection);     
?>

 

And this is the error message I am getting:

Error in query: UPDATE News SET Link = Link WHERE Title =Adding new link to db AND Content =

 

To check and see if the link will work with the RSS and see if it correctly adds to the database.

. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Link ' at line 1

 

Oh by the way I am using Server version: 5.0.51 and MySQL client version: 5.0.32.

 

Thanks guys :D

Link to comment
https://forums.phpfreaks.com/topic/181421-solved-syntax-error/
Share on other sites

You have to put quotes around strings in your query.

Hi Mchl

 

I've tried that but when I put double quotes "" around it then it doesn't work because of the http:// - the two forward slashes make the rest of the code think that it has been commented out.

 

Hope this makes sense and thanks for your help so far. :)

Link to comment
https://forums.phpfreaks.com/topic/181421-solved-syntax-error/#findComment-957032
Share on other sites

have you figured this out yet? i see some things in your code that i think you should change, but if you've gotten it working then i dont want to confuse you.

Not as yet I haven't. Any help would be appreciated please.

 

Thanks

 

Simplified:

 

$yourlink = "<a href=http://www.2asmooth.com/Kingfisher_Trust/showNews.php?NewsID=".mysql_insert_id($connection).">Link</a>";

$query2 = "UPDATE News SET Link ='$yourlink' WHERE Title ='$newTitle' AND Content ='$newContent'"; 

 

Link to comment
https://forums.phpfreaks.com/topic/181421-solved-syntax-error/#findComment-957043
Share on other sites

have you figured this out yet? i see some things in your code that i think you should change, but if you've gotten it working then i dont want to confuse you.

Not as yet I haven't. Any help would be appreciated please.

 

Thanks

 

Simplified:

 

$yourlink = "<a href=http://www.2asmooth.com/Kingfisher_Trust/showNews.php?NewsID=".mysql_insert_id($connection).">Link</a>";

$query2 = "UPDATE News SET Link ='$yourlink' WHERE Title ='$newTitle' AND Content ='$newContent'"; 

 

Thanks - that's made it partly work now but for some reason it stops incrementing now and always goes to the

<a href=http://www.2asmooth.com/Kingfisher_Trust/showNews.php?"."NewsID=0>Link</a>
link no matter. It's like it doesn't even bother doing the second query to update the table.

 

Sorry to be a pain guys but do you know why this is?

 

In my database I have five fields: NewsID, Title, Date, Content and Link.

The NewsID is an autoincrement and what I am trying to do is; everytime a user saves a new article it saves it to the database and also autoincrements the Link field so that there is a unique web link to each news story. Hope that makes sense.

 

Obviously I am doing something wrong. Thanks for your help so far though, its really appreciated.

Link to comment
https://forums.phpfreaks.com/topic/181421-solved-syntax-error/#findComment-957061
Share on other sites

OK got most of it working now. Only problem is although it is updating the link and that is working - because it is using the

mysql_insert_id($connection)

to redirect the header it won't work because it has already been used before for the link .Is there anyway I can get that to work. Will that command allow you to put it in a variable?

Link to comment
https://forums.phpfreaks.com/topic/181421-solved-syntax-error/#findComment-957084
Share on other sites

OK got most of it working now. Only problem is although it is updating the link and that is working - because it is using the

mysql_insert_id($connection)

to redirect the header it won't work because it has already been used before for the link .Is there anyway I can get that to work. Will that command allow you to put it in a variable?

It works now. I needed to put the

mysql_insert_id($connection)
into a variable and I just called that twice.

 

THANKS ALL FOR YOUR HELP :D

Link to comment
https://forums.phpfreaks.com/topic/181421-solved-syntax-error/#findComment-957094
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.