/    Sign up×
Community /Pin to ProfileBookmark

Posting to Database

I am trying to post a form to the database. The form is to add a news story to the news page. I have add news and edit new. Edit news is working fine and posting to the database, but adding news is not working.

[code=php]<?php

include(“config.php”);

if(isset($_POST[‘submit’]))

{//begin of if($submit).

// Set global variables to easier names

// and pervent sql injection and apostrophe to break the db.

$title = mysql_escape_string($_POST[‘title’]);

$text1 = mysql_escape_string($_POST[‘text1’]);

$text2 = mysql_escape_string($_POST[‘text2’]);

$text3 = mysql_escape_string($_POST[‘text3’]);

$text4 = mysql_escape_string($_POST[‘text4’]);

$text5 = mysql_escape_string($_POST[‘text5’]);

$text6 = mysql_escape_string($_POST[‘text6’]);

//check if (title) field is empty then print error message.

if(!$title){ //this means If the title is really empty.

echo “Error: News title is a required field. Please fill it.”;

exit(); //exit the script and don’t do anything else.

}// end of if

//run the query which adds the data gathered from the form into the database

$result = mysql_query(“INSERT INTO news (title, dtime, text1, text2, text3, text4, text5, text5)

VALUES (‘$title’,NOW(),’$text1′,’$text2′,’$text3′,’$text4′,’$text5′,’$text6′)”,$connect);

//print success message.

echo “<b>Thank you! News added Successfully!<br><br></b><span class=’class6′><a href=’../members_home.php’>Members Area</a></style> | <span class=’class6′><a href=’../index.php’>Home</a></style>”;

}//end of if($submit).

// If the form has not been submitted, display it!

else

{//begin of else

?>[/code]

After i post the form, i get the successful message so its not a connection problem, but it is not adding to the databse :S

Cheers,

Dan

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@dankellysauthorJun 21.2009 — here is the code from edit news, which is working:

[code=php]<?php

include("config.php");

if(isset($_POST['submit']))

{
// Set global variables to easier names

// and prevent sql injection and apostrophe to break the db.

$title = mysql_escape_string($_POST['title']);

$text1 = mysql_escape_string($_POST['text1']);

$text2 = mysql_escape_string($_POST['text2']);

$text3 = mysql_escape_string($_POST['text3']);

$text4 = mysql_escape_string($_POST['text4']);

$text5 = mysql_escape_string($_POST['text5']);

$text6 = mysql_escape_string($_POST['text6']);

$newsid = $_GET['newsid'];

$result = mysql_query("UPDATE news SET title='$title', text1='$text1', text2='$text2', text3='$text3', text4='$text4', text5='$text5', text6='$text6' WHERE newsid='$newsid' ",$connect);



echo "<font color='#FFFFFF'><b>Thank you! News Updated Successfully!<br><br></b><span class='class6'><a href='../members_home.php'>Members Area</a></style> | <span class='class6'><a href='../index.php'>Home</a></style>";


}

elseif(isset($_GET['newsid']))

{



$result = mysql_query("SELECT * FROM news WHERE newsid='$_GET[newsid]' ",$connect);

while($myrow = mysql_fetch_assoc($result))

{

$title = $myrow["title"];

$text1 = $myrow["text1"];

$text2= $myrow["text2"];

$text3= $myrow["text3"];

$text4= $myrow["text4"];

$text5= $myrow["text5"];

$text6= $myrow["text6"];

?>[/code]


I cant see any differenct
Copy linkTweet thisAlerts:
@kurbyJun 22.2009 — Your insert query does not have matching a matching column count.


[code=php]$result = mysql_query("INSERT INTO news (title, dtime, text1, text2, text3, text4, text5, text5)

VALUES ('$title',NOW(),'$text1','$text2','$text3','$text4','$text5','$text6')",$connect)[/code]


Should be

[code=php]$result = mysql_query("INSERT INTO news (title, dtime, text1, text2, text3, text4, text5, text5, text6)

VALUES ('$title',NOW(),'$text1','$text2','$text3','$text4','$text5','$text6')",$connect)[/code]



You should consider adding a debug output for your queries. Either test the result or add an [code=php]or die(mysql_error());[/code]

Also, its not the best practice so have so many arbitrarily named columns in a table. You may want to rethink your design.
×

Success!

Help @dankellys spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...