Help me!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Help me!

Post 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
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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 :wink:
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Yup, I was wrong there..thanx Nay!
:oops:
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

what's wrong with this code?

Post 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>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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;

Code: Select all

if ($submit) {
...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 ;) )
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

you are great jam! it worked this time.

what about my other post? would u please take a look?
Post Reply