I am newbie php programmer trying to insert info into a mysql database using the very basic "insert into db" command but nothing happens or noshould I say nothing is inserted. But if use phpmyadmin to insert data it inserts the info fine.
THis is the 'basic' code that I can't get to work----->
<?php
if($submit)
{
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb", $db);
$sql = "insert into personnel (firstname, lastname, nick, email, salary) Values
('$first', '$last', '$nickname', '$email', '$salary')";
$result = mysql_query($sql);
echo "Thank you! Information Entered. \n";
}
else
{
?>
<form method="post" action="input.php">
First name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Nickname: <input type="text" name="nickname"><br>
E-mail: <input type="text" name="email"><br>
Salary: <input type="text" name="salary"><br>
<input type="submit" name="submit" value="Enter Information">
</form>
<?
}
?>
I know this code works 'cos i copied it from a book and it doesn't spit out any errors, is there anything else that I could check that could make this process work.
Thanks Guys
inserting info into a mysql db
Moderator: General Moderators
After the line
$sql = "insert into personnel (firstname, lastname, nick, email, salary) Values
('$first', '$last', '$nickname', '$email', '$salary')";
put
Print "The SQL Statement is $sql";
This will show you what is passed as values.
Check that for all fields that are 'not null' allowed that you are passing values for them.
Look at mysql_error in the manual and try using it after the mysql_query line.
$sql = "insert into personnel (firstname, lastname, nick, email, salary) Values
('$first', '$last', '$nickname', '$email', '$salary')";
put
Print "The SQL Statement is $sql";
This will show you what is passed as values.
Check that for all fields that are 'not null' allowed that you are passing values for them.
Look at mysql_error in the manual and try using it after the mysql_query line.
But the problem is that.....
well the problem with that is, the code goes back to the 2nd part of the if stamement so therefore doesn't allow me to see the code which you've just given me. In other words when I press submit it returns to the forms as if nothing has been filled in and the submit button hasn't been pressed.
I hate to be such a nag but...
it still doesn't seem to give me anything, this is weird , I mean if you had to run this code on your server would do you think it would insert the info??
It is now probably to do with your version of PHP and the register_globals thing, read this first
-
NikolasTSR
- Forum Newbie
- Posts: 3
- Joined: Mon Sep 16, 2002 3:07 pm
that maybe help
When you write your code add some scipt that will help you.
1. This will print "could not connect" if your username and pass is wrong
$db = mysql_connect("localhost", "root", "")
or die ("could not connect");
2. This will print "could not select" if that happens.
mysql_select_db("learndb", $db)
or die ("cound not select");
3. you can use $PHP_SELF in your action field. PHP will find the page name by itself.
4. You have to pass the $submit variable so that the script will insert the second time. This, you can do,
a. by naming the form button as "submit" <<case does matter!!>>
b. adding a hidden field named submit, if you use an image as your submit button.
5. If you are working on a linux, case dose matter, so check that your table name is correct (copy paste from your PhpMyAdmin page)
6. Also check that your other fields (firstname, lastname) are "case correct" and that they all excist in your db table.
7. Since you give "$submit" the value of "Enter Information" you can start your script like this.
if ($submit=="Enter Information"){
the first time the script runs "$submit" has no value the second time it has a value and it will insert your data.
I hope this will help you, if you haven't solve it allready.
1. This will print "could not connect" if your username and pass is wrong
$db = mysql_connect("localhost", "root", "")
or die ("could not connect");
2. This will print "could not select" if that happens.
mysql_select_db("learndb", $db)
or die ("cound not select");
3. you can use $PHP_SELF in your action field. PHP will find the page name by itself.
4. You have to pass the $submit variable so that the script will insert the second time. This, you can do,
a. by naming the form button as "submit" <<case does matter!!>>
b. adding a hidden field named submit, if you use an image as your submit button.
5. If you are working on a linux, case dose matter, so check that your table name is correct (copy paste from your PhpMyAdmin page)
6. Also check that your other fields (firstname, lastname) are "case correct" and that they all excist in your db table.
7. Since you give "$submit" the value of "Enter Information" you can start your script like this.
if ($submit=="Enter Information"){
the first time the script runs "$submit" has no value the second time it has a value and it will insert your data.
I hope this will help you, if you haven't solve it allready.