Insert HMTL Code into MySQL Database through a Form with PHP

Thread Solved

Join Date: May 2009
Posts: 7
Reputation: MArun25039 is an unknown quantity at this point 
Solved Threads: 0
MArun25039 MArun25039 is offline Offline
Newbie Poster

Insert HMTL Code into MySQL Database through a Form with PHP

 
0
  #1
May 19th, 2009
Hello,

I'm having an issue with a MySQL/PHP site I'm working on. I'm new to both. I've seen several posts that were similar or same but was unable to derive a solution based on my code.

I have a form that will submit HTML code to a database. I know the form/php/database works since I've test it with non-code text and it works fine.

An example of the HTML code I'd like to submit is as follows; it will be for a link within an image (the image located on the web).
  1. <a target='new' href='http://www.destination.com><IMG alt='ALT TAG' border='0' src='http://www.imagelocation.com></a><IMG border='0' width='1' height='1' src='http://www.image sourece.com>
  2.  

The Error reads as follows:
  1. ERROR: 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 'new' href='http://www.destination.com>ALT TAG
  2.  

Below is the code for the 'add' which contains the form and 'added' which sends to the database.

add.php
  1. <form method="post" action="added.php">
  2. <b>Please enter the catalog item information:</b><br>
  3.  
  4. <table>
  5. <tr>
  6. <td>
  7. <label for="link"><b>Link:</b></label>
  8. </td>
  9. <td>
  10. <input id="link" name="link" size="100" />
  11. </td>
  12. </tr>
  13. </table>
  14.  
  15. <input type="submit" name="submit" value="Add" />
  16. </form>

added.php
  1. <?php
  2.  
  3. include("connect.php");
  4.  
  5. mysql_select_db("dbase", $con);
  6.  
  7. $sql="INSERT INTO catalog (link)
  8. VALUES('$_POST[link]')";
  9.  
  10. if (!mysql_query($sql,$con))
  11. {
  12. die('ERROR: '. mysql_error());
  13. }
  14.  
  15. if(isset($_POST['submit']))
  16. {
  17.  
  18. echo "<h1>Success!</h1><br>Catalog item has been entered into database.<br><br>";
  19.  
  20. }
  21. ELSE
  22. {
  23. header("Location: add.php", 301);
  24. }
  25.  
  26. mysql_close($con)
  27.  
  28. ?>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,750
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Insert HMTL Code into MySQL Database through a Form with PHP

 
0
  #2
May 22nd, 2009
You should escape ' before adding it to the database.
Use,
  1. $link = mysql_real_escape_string($_POST['link']);
  2. $sql="INSERT INTO catalog (link)
  3. VALUES ('$link')";
You got that error because, the opening ' in your query is terminated when it encounters
<a target='
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 44
Reputation: guru12 is an unknown quantity at this point 
Solved Threads: 6
guru12's Avatar
guru12 guru12 is offline Offline
Light Poster

Re: Insert HMTL Code into MySQL Database through a Form with PHP

 
-1
  #3
Jul 15th, 2009
Dear friend

Use the insert code like this
  1. INSERT INTO `tablename` (`id`, `name`) VALUES
  2. (30, '<a href=ww.yahoo.com>Yahoo</a>');

THanks and Regards
Last edited by peter_budo; Jul 15th, 2009 at 7:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC