Jump to content

[SOLVED] Form only adding limited info into database


toxictoad

Recommended Posts

Hey I'm back again...

 

I've got a HTML form that posts the info entered to a php page that then updates the database.

 

I have all this working for one database so adapted it for another but for some reason it's only updating 2 fields in the database. I've gone over it repeatidly and just can't understand why it's not updating all the Fields so an extra pair of eyes to look over what I've got might help.

 

DB Name: rss

Table Name: rssfeeds

Fields: NewsID - int(11) | Title - varchar(250) | Link - varchar(250) | PubDate - date | Description - varchar(250) | Content - text | Source - varchar(250)

 

The HTML form

<html>
<head>
<title>Add News Artcle</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="title" size="70" /><br />
Link: <input type="text" name="link" size="60" value="http://www.mysite.co.uk/details.php?id=" /><br />
Date: <input type="text" name="date" size="20" /><br />
Description:<br /> <textarea name="description" cols="50" rows="5"></textarea><br />
Content:<br /> <textarea name="content" cols="50" rows="12"></textarea><br />
Source: <input type="text" name="source" size="40" value="http://" /><br />
<input type="submit" value="Add Article" /><br />
</body>
</html>

 

The Update php code

<?php
// initiate DB connection
mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("DBNAME");

$title = $_POST['Title'];
$link = $_POST['Link'];
$date = $_POST['PubDate'];
$description = $_POST['Description'];
$content = $_POST['Content'];
//$link = $_POST['link'];
$source = $_POST['source'];

mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_rss");

// pull the id for the last article entered into the rssfeeds table 
$lastArticle = mysql_fetch_assoc(mysql_query("SELECT NewsID FROM rssfeeds ORDER BY NewsID DESC LIMIT 1"));
$link = "http://www.mysite.co.uk/rss/details.php?id=" . ($lastArticle['NewsID'] + 1);  // append the last id plus one to the link before inserting...

$query="INSERT INTO rssfeeds (title, link, pubdate, description, content, source) VALUES ('".$title."', '".$link."', '".$date."', '".$description."', '".$content."', '".$source."')";

mysql_query($query) or die ('Error updating database');

echo "" ;

?>
<html>
<head>
<title>Updated Database</title>
</head>
<body>
Database Updated<br />
<a href="http://www.mysite.co.uk/rss/form.php">Enter New Article</a></body>
</html>

 

When I check what's been entered into the DB it's only the following

NewsID | Link | Source | The Date field just shows 0000-00-00 after entering 2008-12-05

 

Can you see what I'm missing?

Thanks

Link to comment
Share on other sites

simple... name of field and you post values are different. check capitals

example:

$title = $_POST['Title'];// name of field is name="title"

$title = $_POST['title'];//so the post variable needs to match

 

fix them and then try again

 

Easy when you know how ;) I didn't realise that caps made a difference (til now)

 

I do have a problem with the date though because it's still only showing 0000-00-00

 

Is there a specific way the date needs to be entered into the form? I'm entering it the way it's shown in mySQL 2008-12-05

 

thanks

Link to comment
Share on other sites

sorry, didn't mean easy and you suck... i meant easy to fix. I made the same mistake back when i first started, till do accidentally. as for date, can't quite help you there since by the sounds of it "2008-12-05" didn't work and I don't know why it doesn't. must be a special syntax you must use which i'm not aware of. still a noob in many ways.

Link to comment
Share on other sites

You just had one form field name that did not match the name used in the $_POST variable, would it not be prudent to check if the form field name used for your date matched?

 

Absolutely! I changed every reference so that they all start with capitals but still the date in the database shows as 0000-00-00

 

<html>
<head>
<title>Add News Artcle</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="Title" size="70" /><br />
Link: <input type="text" name="Link" size="60" value="http://www.mysite.co.uk/details.php?id=" /><br />
Date: <input type="text" name="Date" size="20" /><br />
Description:<br /> <textarea name="Description" cols="50" rows="5"></textarea><br />
Content:<br /> <textarea name="Content" cols="50" rows="12"></textarea><br />
Source: <input type="text" name="Source" size="40" value="http://" /><br />
<input type="submit" value="Add Article" /><br />
</body>
</html>

 

<?php
// initiate DB connection
mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_rss");

$Title = $_POST['Title'];
$Link = $_POST['Link'];
$Date = $_POST['PubDate'];
$Description = $_POST['Description'];
$Content = $_POST['Content'];
//$Link = $_POST['Link'];
$Source = $_POST['Source'];

mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_rss");

// pull the id for the last article entered into the rssfeeds table 
$lastArticle = mysql_fetch_assoc(mysql_query("SELECT NewsID FROM rssfeeds ORDER BY NewsID DESC LIMIT 1"));
$Link = "http://www.mysite.co.uk/rss/details.php?id=" . ($lastArticle['NewsID'] + 1);  // append the last id plus one to the link before inserting...

$query="INSERT INTO rssfeeds (Title, Link, PubDate, Description, Content, Source) VALUES ('".$Title."', '".$Link."', '".$Date."', '".$Description."', '".$Content."', '".$Source."')";

mysql_query($query) or die ('Error updating database');

echo "" ;

?>
<html>
<head>
<title>Updated Database</title>
</head>
<body>
Database Updated<br />
<a href="http://www.mysite.co.uk/rss/form.php">Enter New Article</a></body>
</html>

Link to comment
Share on other sites

A good tip for debugging is to echo out your query and see if it has all the data you want it to have.

 

A side note, here is the way I write queries to avoid escaping data and such..

 

<?php
$query="INSERT INTO rssfeeds (title, link, pubdate, description, content, source) VALUES ('$title', '$link', '$date', '$description', '$content', '$source')";

echo $query;

?>

 

Since the whole line is wrapped with " and ", the single quotes inside don't turn the variables into literal strings like it would if you used single quotes to wrap the query.

 

Nate

Link to comment
Share on other sites

And what would happen if someone entered data with a " in it? would that not escape out of it?  I think it would be worth it to still either mysql_real_escape_string or addslashes to the data still. You really cant trust people to put in the correct information, either by mistake or by malice, someone might try to take advantage of the situation or at the very least, break it. 

Link to comment
Share on other sites

Date (your form field name) is not equal to PubDate (your $_POST variable name.)

You got it! man am I cut out for this programming stuff! (please don't answer)

 

Thank you for your help  :)

 

Thanks for this chronister. I have gone through some tutorials but there's so much and I guess I'm ju7mping in the deep end so to speak without a proper understanding.

 

Your help's much appreciated guys, thank you!

Link to comment
Share on other sites

Thanks for this chronister. I have gone through some tutorials but there's so much and I guess I'm ju7mping in the deep end so to speak without a proper understanding

 

Well, learning little by little is they way to go. You are not going to get a proper understanding without making mistakes and learning from them. That is what I have done over the last few years, though I still don't have a "proper" understanding. I am going to be learning better ways to do things for a LONG TIME.

 

This forum is filled with VERY knowledgeable people who will help you if your willing to help yourself. If you want to learn, then keep trying and when you get stuck come back here and someone will help ya.

 

Hapy Coding!

 

Nate

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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