/    Sign up×
Community /Pin to ProfileBookmark

form to db problems

[CODE]<?

mysql_connect(“localhost”, “chale”, “test”) or die(mysql_error());
mysql_select_db(“site”) or die(mysql_error());

mysql_query(“INSERT INTO ‘menu’ (‘category’,’item’,’itemdesc’,’price’,’lunch’,’dinner’) VALUES (‘$category’,’$item’,’$itemdesc’,’$price’,’$lunch’,’$dinner’)”);
$category = $_POST[‘category’];
$item = $_POST[‘item’];
$itemdesc = $_POST[‘itemdesc’];
$price = $_POST[‘price’];
$lunch = $_POST[‘lunch’];
$dinner = $_POST[‘dinner’];

$note = “<DIV>The item has been added as stated below to the menu.<br>If there is a mistake you can edit it by using the search links at the top of the page for each section of the menu.</DIV>”;

print “<!– ” . $_POST[‘formaction’] . ” –>
<HTML>
<HEAD>
<TITLE>Adding menu item</TITLE>
</HEAD>
<BODY>” . $note . ”
<br>
Item: ” . $item . ” <br>
Item description: ” . $itemdesc . ” <br>
Lunch item: ” . $lunch . ” <br>
Dinner item: ” . $dinner . ” <br>
Price: ” . $price . ” <br>
</BODY>
</HTML>”;

?>[/CODE]

Above is the php code I am using for submitting my form to a database… i am using form action php_self, since i am doing it all within one file. when I submit it prints out the info that was added on the screen but does not submit it to the DB…. any ideas why?

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@salmanshafiqNov 23.2007 — check there is any error or not? by replace this line you know is there any error in the query or not


[code=php]

mysql_query("INSERT INTO 'menu' ('category','item','itemdesc','price','lunch','dinner') VALUES ('$category','$item','$itemdesc','$price','$lunch','$dinner')") or die(mysql_error());
[/code]
Copy linkTweet thisAlerts:
@roscorNov 23.2007 — Try a print_r($_POST) to see what values are set.
Copy linkTweet thisAlerts:
@gpm1982Nov 23.2007 — Move the mysql_query() line down (after $dinner = $_POST['dinner']). The code should be as follows:

[code=php]mysql_connect("localhost", "chale", "test") or die(mysql_error());
mysql_select_db("site") or die(mysql_error());

$category = $_POST['category'];
$item = $_POST['item'];
$itemdesc = $_POST['itemdesc'];
$price = $_POST['price'];
$lunch = $_POST['lunch'];
$dinner = $_POST['dinner'];

mysql_query("INSERT INTO 'menu' ('category','item','itemdesc','price','lunch','dinner') VALUES ('$category','$item','$itemdesc','$price','$lunch','$dinner')");

$note = "<DIV>The item has been added as stated below to the menu.<br>If there is a mistake you can edit it by using the search links at the top of the page for each section of the menu.</DIV>";

print "<!-- " . $_POST['formaction'] . " -->
<HTML>
<HEAD>
<TITLE>Adding menu item</TITLE>
</HEAD>
<BODY>" . $note . "
<br>
Item: " . $item . " <br>
Item description: " . $itemdesc . " <br>
Lunch item: " . $lunch . " <br>
Dinner item: " . $dinner . " <br>
Price: " . $price . " <br>
</BODY>
</HTML>";[/code]
Copy linkTweet thisAlerts:
@dvdd127authorNov 23.2007 — check there is any error or not? by replace this line you know is there any error in the query or not


[code=php]

mysql_query("INSERT INTO 'menu' ('category','item','itemdesc','price','lunch','dinner') VALUES ('$category','$item','$itemdesc','$price','$lunch','$dinner')") or die(mysql_error());
[/code]
[/QUOTE]

i am not sure i understand what you are saying

Try a print_r($_POST) to see what values are set.[/QUOTE]
tried this... doesnt seem to fix the issue

Move the mysql_query() line down (after $dinner = $_POST['dinner']). The code should be as follows:
[/QUOTE]

tried this too and it also didnt work

I tried the last two suggestions both together and separate.... doesnt seem to work either way.
Copy linkTweet thisAlerts:
@roscorNov 24.2007 — First confirm that your database connection info is correct,

best practice is to set those varibles eg dbinfo.php then use an include, like so include("dbinfo.php");

in that file you have your connection info

dbinfo.php
[code=php]$host = "localhost";

$username = "chale";

$password = "test";

$database = "site";
[/code]


[code=php]
include("dbinfo.php");
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO menu SET
category = '$catergory',
item = '$item',
itemdesc = '$itemdesc',
price = '$price',
lunch = '$lunch',
dinner = '$dinner',
mysql_query($query);[/code]

maybee your intial problem was your admitance of a null value for your primary key which may be set as auto incremnent.

so another alternative could be
[code=php]$query =INSERT INTO landlordlo VALUES ('','$catergory','$item','$itemdesc','$price','$lunch','$dinner')";
mysql_query($query);

// then check if the info has been submited or display an error
if($query){
echo "<td align ='center'><font face='Verdana' size='2' color='#000000'>Record now added to Database<br><br><br>";

die() ;
}

else {
echo "<td align ='center'><font face='Verdana' size='2' color='#000000'>Record Failed to be added to database please try again";

die() ;

}[/code]
or something similar, hope this helps
Copy linkTweet thisAlerts:
@dvdd127authorNov 24.2007 — First confirm that your database connection info is correct,

best practice is to set those varibles eg dbinfo.php then use an include, like so include("dbinfo.php");

in that file you have your connection info

dbinfo.php
[code=php]$host = "localhost";

$username = "chale";

$password = "test";

$database = "site";
[/code]


[code=php]
include("dbinfo.php");
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO menu SET
category = '$catergory',
item = '$item',
itemdesc = '$itemdesc',
price = '$price',
lunch = '$lunch',
dinner = '$dinner',
mysql_query($query);[/code]

maybee your intial problem was your admitance of a null value for your primary key which may be set as auto incremnent.

so another alternative could be
[code=php]$query =INSERT INTO landlordlo VALUES ('','$catergory','$item','$itemdesc','$price','$lunch','$dinner')";
mysql_query($query);

// then check if the info has been submited or display an error
if($query){
echo "<td align ='center'><font face='Verdana' size='2' color='#000000'>Record now added to Database<br><br><br>";

die() ;
}

else {
echo "<td align ='center'><font face='Verdana' size='2' color='#000000'>Record Failed to be added to database please try again";

die() ;

}[/code]
or something similar, hope this helps[/QUOTE]

Thank you, but for some reason that did not work either.... any other ideas.
Copy linkTweet thisAlerts:
@dvdd127authorNov 24.2007 — Ok. I manged to get it working. Now I just need to make it so that the message only comes up if the query is successful.

Here is the working code.
[CODE]<?

mysql_connect("localhost", "binaryco_chale", "ale7");
@mysql_select_db("binaryco_chale") or die(mysql_error("Unable to connect to database."));

$category = $_POST['category'];
$item = $_POST['item'];
$itemdesc = $_POST['itemdesc'];
$price = $_POST['price'];
$lunch = $_POST['lunch'];
$dinner = $_POST['dinner'];

$query = "INSERT INTO menu VALUES ('','$category','$item','$itemdesc','$price','$lunch','$dinner')";
mysql_query($query);


echo "The item has been added as stated below to the menu.<br>If there is a mistake you can edit it by using the search links at the top of the page for each section of the menu.";

print ("<!-- " . $_POST['formaction'] . " -->
<HTML>
<HEAD>
<TITLE>Adding menu item</TITLE>
</HEAD>
<BODY>" . $note . "
<br>
Item: " . $_POST['item'] . " <br>
Item description: " . $_POST['itemdesc'] . " <br>
Lunch item: " . $_POST['lunch'] . " <br>
Dinner item: " . $_POST['dinner'] . " <br>
Price: " . $_POST['price'] . " <br>
</BODY>
</HTML>");


?>[/CODE]


This is the message I would like to set to ONLY show up if the query is successful.
[CODE]echo "The item has been added as stated below to the menu.<br>If there is a mistake you can edit it by using the search links at the top of the page for each section of the menu.";

print ("<!-- " . $_POST['formaction'] . " -->
<HTML>
<HEAD>
<TITLE>Adding menu item</TITLE>
</HEAD>
<BODY>" . $note . "
<br>
Item: " . $_POST['item'] . " <br>
Item description: " . $_POST['itemdesc'] . " <br>
Lunch item: " . $_POST['lunch'] . " <br>
Dinner item: " . $_POST['dinner'] . " <br>
Price: " . $_POST['price'] . " <br>
</BODY>
</HTML>");


?>[/CODE]
Any ideas?? Thanks again for all your help.
Copy linkTweet thisAlerts:
@roscorNov 25.2007 — use an if statement
[code=php]if(condition){
something
} else {
something different
}[/code]

therefore if your query has executed {display your message} else {display an error message}

see [URL]http://www.w3schools.com/php/php_if_else.asp[/URL]
Copy linkTweet thisAlerts:
@dvdd127authorNov 25.2007 — use an if statement
[code=php]if(condition){
something
} else {
something different
}[/code]

therefore if your query has executed {display your message} else {display an error message}

see [URL]http://www.w3schools.com/php/php_if_else.asp[/URL][/QUOTE]
thanks again for your help.

one final issue. a blank record is saved to the db whenever i load the page. I am sure this is once again something really easy. i am new to php though so that is why I dont know how to fix it.
Copy linkTweet thisAlerts:
@gpm1982Nov 26.2007 — as suggested by roscor, you can use the if - else block to verify if the input has been submitted. example:

[code=php]
if( isset($_POST['category']) ){
mysql_connect("localhost", "binaryco_chale", "ale7");
// the remaining code
}
else{
// display the form
}
[/code]
Copy linkTweet thisAlerts:
@dvdd127authorNov 26.2007 — do i need to do that for each field.. or just the first field that is submitted?
Copy linkTweet thisAlerts:
@gpm1982Nov 26.2007 — do i need to do that for each field.. or just the first field that is submitted?[/QUOTE]

You can check for every field submitted, or you can check for just one or two fields, it's up to you. For my case, I would normally check for one field. But then, I'll validate the other fields before I submit them to db. Example for user login page:

[code=php]
if( isset($_POST['username']) ){
// get the values, with every slashes stripped
// this is not necessary, but I use mysql's built in escape character function
$username = ( get_magic_quotes_gpc() ) ? stripslashes($_POST['username']) : $_POST['username'];
$password = ( get_magic_quotes_gpc() ) ? stripslashes($_POST['password']) : $_POST['password'];

// I want to make sure both username and password contains at min 6 to max 15 characters
if( !preg_match("#^w{6,15}$#", $username) || !preg_match("#^w{6,15}$#", $password) ){
// display error message, then exit
}
else{
// continue with the script
}
}
[/code]
Copy linkTweet thisAlerts:
@dvdd127authorNov 26.2007 — thanks a lot for all your help.

Is there a way to make it so that on initial load it does not show an error message??
Copy linkTweet thisAlerts:
@gpm1982Nov 26.2007 — thanks a lot for all your help.

Is there a way to make it so that on initial load it does not show an error message??[/QUOTE]


Can you tell me what error message appears when you perform initial load?
Copy linkTweet thisAlerts:
@dvdd127authorNov 26.2007 — Can you tell me what error message appears when you perform initial load?[/QUOTE]sorry, i was not clear in what I was asking..

I was referring to my custom error message that lets the user know nothing was added to the db.
Copy linkTweet thisAlerts:
@dvdd127authorNov 27.2007 — so is there a way to get rid of the custom error message that tells the user nothing was added to the db?
×

Success!

Help @dvdd127 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.18,
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,
)...