Page 1 of 1
Help me!
Posted: Mon Dec 29, 2003 12:33 am
by crazytopu
I have a book entry form. I want to send some of the information to my database (MySQL) which user input.
The primary key (call_no) of the book table is an auto-increment int type. So, I do not want the users to assign the primary key rather it is the system, which would take care of it, and every time a new book is being entered into the stock the system would increase the call_no automatically.
That’s why I did not keep any call_no entry field in my form.
Question:
1. Can you give me a sample code structure that sends user input info to a database table and save it? I wanna use a submit button and a reset button that will clear the previous entry and make room for the next entry.
2. Since the users do not input any data into the call_no field, there would always be one value less than it is in the actual book table. How can PHP add that auto-increment value to each row? For example, I have 8 fields in the book table, but users input 7 fields, how php takes care of that auto increment field. Or is this just that PHP does not have to do anything at all. Once those 7 fields are being passed to the table the DBMS itself put the auto no?
3. If I want to display the current value of the table how can I do it by clicking a button? Like from “show all the book” button?
4. Do you know any cool link where I can learn some searching stuff?
Many many thanks in advance
Posted: Mon Dec 29, 2003 1:08 am
by crazytopu
Okay I got the following piece of code:
<html>
<body>
<?php
if ($submit) {
// process form
$db = mysql_connect("localhost", "root");
mysql_select_db("test",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>
</body>
</html>
In this case, I have a table named employees with 5 field where id is the primary key and of an int auto increment type. But it has not been included in the form and left with the system to take care.
But why can’t I send info to my database using this code? My database name is test.
Any clue?
Posted: Mon Dec 29, 2003 1:13 am
by microthick
If register_globals is OFF, then you'll have to change $first to $_POST["first"] and $last to $_POST["last"] etc, etc, etc.
Posted: Mon Dec 29, 2003 7:38 am
by crazytopu
you mean something like this:
$sql = "INSERT INTO employees (first,last,address,position) VALUES ("$_POST["first"]","$_POST["last"]","$_POST["address"]","$_POST["position"]")";
??
I am a newbie so dont get angry to see my stupidity..
I got the following error msg when tried with the above example:
Parse error: parse error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in g:\program files\apache group\apache\htdocs\insert.php on line 19
I then exclude the quote from the varible ...i.e $_POST["first"] not "$POST .... ".
but still get the same error msg.
Posted: Mon Dec 29, 2003 8:15 am
by Derfel Cadarn
Try this:
Code: Select all
$sql = "INSERT INTO employees (first,last,address,position) VALUES ($_POST[first],$_POST[last],$_POST[address],$_POST[position])";
no quotes needed here

Posted: Mon Dec 29, 2003 8:59 am
by Nay
MySQL ain't going to like that. Here's a more normal way:
Code: Select all
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('{$_POST['first']}','{$_POST['last']}','{$_POST['address']}','{$_POST['position']}')";
-Nay
Posted: Mon Dec 29, 2003 10:42 am
by Derfel Cadarn
Yup, I was wrong there..thanx Nay!

what's wrong with this code?
Posted: Fri Jan 02, 2004 9:41 am
by crazytopu
What's wrong with this code? It sounds like all right but still unable to send value to my database table.
can anybody tell me please?
I do not get the message " Your data was inserted successfuly" after i submit the data.
<html>
<body>
<?php
if ($submit) {
// process form
$db = mysql_connect("localhost", "root");
mysql_select_db("test",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('{$_POST['first']}','{$_POST['last']}','{$_POST['address']}','{$_POST['position']}')";
$result = mysql_query($sql);
echo "Your data was inserted successfuly\n";
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>
</body>
</html>
Posted: Fri Jan 02, 2004 9:46 am
by JAM
Make a new page, add this and tell us what the results are:
Code: Select all
<?php
echo '<pre>';
echo 'PHP Version: '.phpversion()."\n";
echo 'Display Errors: '.(ini_get('display_errors') == '1' ? 'On' : 'Off')."\n";
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n";
echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n";
echo '</pre>';
?>
I have a strong feeling that microthick's post didn't got read. If so is the case;
...is also in need of a change.
@ Derfel Cadarn:
Make a habit of using " and ' in the correct places when inserting/selecting data from a database. You can bump into trouble and security flaws if you for example try to insert data containing a space ( $var = 'foo bar'; ) without using quotes. The space breaks the code. (Just a tip

)
Posted: Fri Jan 02, 2004 9:52 am
by crazytopu
wow! you are damn first!
Thanks.
It gives me the following information:
PHP Version: 4.3.4
Display Errors: On
Error Level: Not E_ALL
Register Globals: Off
Jam, i replied to your queries in the post titled : " i have no clue.. do you?
can you check it and let me know if you can shed any light?
Posted: Fri Jan 02, 2004 9:55 am
by JAM
Code: Select all
// bad
if ($submit) {
// better
if (isset($_POST['submit'])) {
The actual submit button is something that affects $_POST also. I'm not verifying the other code/comments posted above, just mentioning this issue.
Posted: Fri Jan 02, 2004 10:01 am
by crazytopu
you are great jam! it worked this time.
what about my other post? would u please take a look?