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
NewBie! Help me sending user input data to database table
Moderator: General Moderators
Re: NewBie! Help me sending user input data to database tabl
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";
}yes, exactly.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?
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";
}
}http://www.mysql.com/doc/en/Retrieving_data.htmlcrazytopu wrote: 4. Do you know any cool link where I can learn some searching stuff?