NewBie! Help me sending user input data to database table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

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

NewBie! Help me sending user input data to database table

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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: NewBie! Help me sending user input data to database tabl

Post by Weirdan »

crazytopu wrote: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.

Code: Select all

<?php
  if(!isset($_POST['submit'])) {
?>
<form action="<?= $_SERVER['PHP_SELF']; ? >" method="POST>
 <input type="text" name="somefield" value="" />
 <input type="submit" name="submit" />
</form>
<?php
}else{
  $host="somehost";
  $user="someuser";
  $password="somepassword";
  $conn=@mysql_connect($host,$user,$password) or die("Could not connect to db server");
  @mysql_select_db("somedatabase",$conn) or die("Could not select db");
  $res=mysql_query("insert into sometable set somefield={$_POST['somefield']}",$conn);
  if(!@mysql_affected_rows($res)) die("Could not insert data");
  echo "Your data was inserted successfuly";
}
crazytopu wrote: 2. [...skip...] 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?
yes, exactly.
crazytopu wrote: 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?

Code: Select all

if(isset($_GET['show_all'])){
   // [...connection stuff skipped...]
   $res=mysql_query("select somefield,someotherfield from sometable");
   while(list($somefield,$someotherfield)=mysql_fetch_row()){
      echo "Somefield: $somefield<br />\nSomeotherfield: $someotherfield";
   }
 }
crazytopu wrote: 4. Do you know any cool link where I can learn some searching stuff?
http://www.mysql.com/doc/en/Retrieving_data.html
Post Reply