Jump to content

[SOLVED] Posting data to a mySQL table


Nomadic

Recommended Posts

I've searched all over the web (including these forums) for the answer to this. I've found plenty of posts and articles on it, the problem is that none of them actually work when I try them. So what am I trying to do? I'm just trying to post some news into a mySQL database where some currently working scripts can pull it out and serve it up as news posts and rss feed input. No matter what I do though I can't get it to post the information into the database.

 

Here's the PHP code I am using:

 

<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'rss';
mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to mysql");
mysql_select_db($dbname)

$header=$_POST['header'];
$description=substr($_POST['content'],0,20)."...";
$content=$_POST['content'];

mysql_query("INSERT INTO feeds (id, title, description, content) 
VALUES ('NULL','$header','$description','$content')") or die("Unable to insert into table");
?>

 

And here's the HTML for the posting form in case you need it:

 

<form name="form1" method="post" action="postingnews.php">
<td><table width="100%" cellpadding="3" cellspacing="1">
<tr><td colspan="3"><strong>Update News:</strong></td></tr>
<tr><td width="78">Header:</td>
<td width="294"><input name="header" type="text" id="header"></td></tr>
<tr><td valign="top">Content:</td>
<td><textarea name="content" cols=50 rows=6 id="content"></textarea></td></tr>
<td>Format:</td>
<td>
<button type="button" onclick="insert('content', '<b>', '</b>');" title="Bold"><b>Bold</b></button>
<button type="button" onclick="insert('content', '<u>', '</u>');" title="Uline"><u>Uline</u></button>
<button type="button" onclick="insert('content', '<i>', '</i>');" title="Italic"><i>Italic</i></button>
<button type="button" onclick="insert('content', '<a href="page link">', '</a>');" title="Link">Link</button>
<button type="button" onclick="insert('content', '<img src="', '">');" title="Image">Image</button>
<button type="button" onclick="insert('content', '', '<br>');" title="Line Break">Line Break</button>
</tr></td>
<tr><td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Post"></td></tr>
</table></td>
</form>

 

Any insight into what I am fudging up would be appreciated greatly. :)

Link to comment
https://forums.phpfreaks.com/topic/177370-solved-posting-data-to-a-mysql-table/
Share on other sites

Hi,

 

Try this.



<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'rss';
mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to mysql");
mysql_select_db($dbname);

$header = addslashes($_POST['header']);
$description= addslashes(substr($_POST['content'],0,20)."...");
$content = addslashes($_POST['content']);

$query = "INSERT INTO feeds (id, title, description, content)VALUES ('','$header','$description','$content')";
$result = mysql_query($query) or die(mysql_error());

?>

 

addslashes is not the best option but it is a start.

also you should not need to insert NULL into an id column as this should not be allowed to be null

 

 

Hi,

 

Try this.



<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'rss';
mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to mysql");
mysql_select_db($dbname);

$header = addslashes($_POST['header']);
$description= addslashes(substr($_POST['content'],0,20)."...");
$content = addslashes($_POST['content']);

$query = "INSERT INTO feeds (id, title, description, content)VALUES ('','$header','$description','$content')";
$result = mysql_query($query) or die(mysql_error());

?>

 

addslashes is not the best option but it is a start.

also you should not need to insert NULL into an id column as this should not be allowed to be null

 

Works like a charm. Thanks a ton :)

 

Oh and as an aside since you mentioned it. My ID column is set to auto-increment which means that NULL value causes it to tick over to the next number. I could probably just leave it out but I stuck it in there to remember that I have an ID column (which may factor in later should I add page links).

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.