Help me!
Moderator: General Moderators
-
crazytopu
- Forum Contributor
- Posts: 259
- Joined: Fri Nov 07, 2003 12:43 pm
- Location: London, UK
- Contact:
Help me!
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
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
-
crazytopu
- Forum Contributor
- Posts: 259
- Joined: Fri Nov 07, 2003 12:43 pm
- Location: London, UK
- Contact:
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?
<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?
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
-
crazytopu
- Forum Contributor
- Posts: 259
- Joined: Fri Nov 07, 2003 12:43 pm
- Location: London, UK
- Contact:
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.
$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.
- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
Try this:
no quotes needed here 
Code: Select all
$sql = "INSERT INTO employees (first,last,address,position) VALUES ($_POST[first],$_POST[last],$_POST[address],$_POST[position])";MySQL ain't going to like that. Here's a more normal way:
-Nay
Code: Select all
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('{$_POST['first']}','{$_POST['last']}','{$_POST['address']}','{$_POST['position']}')";- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
-
crazytopu
- Forum Contributor
- Posts: 259
- Joined: Fri Nov 07, 2003 12:43 pm
- Location: London, UK
- Contact:
what's wrong with this code?
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>
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>
Make a new page, add this and tell us what the results are:
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
)
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>';
?>Code: Select all
if ($submit) {@ 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
Code: Select all
// bad
if ($submit) {
// better
if (isset($_POST['submit'])) {