Forms

Ask about a PHP problem here.
Post Reply
kpkguru003
Posts: 14
Joined: Fri Nov 11, 2011 12:32 pm
Location: India

Forms

Post by kpkguru003 »

how to store the inputs in the form into the database when the "submit" button is clicked ?
i am using the "id" as auto increment.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Forms

Post by Temor »

kpkguru003 wrote:how to store the inputs in the form into the database when the "submit" button is clicked ?
i am using the "id" as auto increment.
your form data gets stored in a $_POST array or a $_GET array depending on what method your form is using.

all you have to do is retrieve the form data like this
<?php
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];

?>
//action is where you send the user after pressing submit. leaving this blank redirects to the same page.
//method is the method in which the form will store the data inserted into the field(s). You can choose either get or post.
<form action="" method="post">
//the name value1 corresponds to $_POST['value1'] and value2 corresponds to $_POST['value2']. i.e whatever you name this is what you will have to put in $_POST['here'].
<input type="text" name="value1" />
<input type="text" name="value2" />
<input type="submit" value="Submit" />
</form>
after that you'll just have to write the query to insert it into your database.
 $sql = "INSERT INTO `table`(`column1`, `column2`) VALUES('{$value1}','{$value2}')"; 
and then you have to run the query.
mysql_query($sql);
you might want to add some error handling in this, and also some security.
this is just the pure basics.
If you need more help, feel free to post here or private message me.

Note: I hope I haven't missed anything crucial. I apologize in advance for any errors or typos.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Forms

Post by jacek »

Temor wrote:all you have to do is retrieve the form data like this
    <?php
    $value1 = $_POST['value1'];
    $value2 = $_POST['value2'];
     
    ?>
There is no need to do this if you are not doing anything to the $_POST variables ! $_POST could be used in the SQL directly.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Forms

Post by Temor »

jacek wrote:
Temor wrote:all you have to do is retrieve the form data like this
    <?php
    $value1 = $_POST['value1'];
    $value2 = $_POST['value2'];
     
    ?>
There is no need to do this if you are not doing anything to the $_POST variables ! $_POST could be used in the SQL directly.
that is true. I only did that so that it would make more sense down the line :)
kpkguru003
Posts: 14
Joined: Fri Nov 11, 2011 12:32 pm
Location: India

Re: Forms

Post by kpkguru003 »

<?php
$regno=$_POST['regno'];
$name =$_POST['name'];
	$query = "INSERT INTO `cs09` values(' ','$regno','$name')";
	$query_run = mysql_query($query);
	
?>
Is this correct , the first field is "Id" which is auto increment , so i left it blank
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Forms

Post by Temor »

kpkguru003 wrote:
<?php
$regno=$_POST['regno'];
$name =$_POST['name'];
	$query = "INSERT INTO `cs09` values(' ','$regno','$name')";
	$query_run = mysql_query($query);
	
?>
Is this correct , the first field is "Id" which is auto increment , so i left it blank
Please put your code in syntax tags
[syntax=php]Code goes here
[/syntax]

yes, that works. But you do not have to put mysql_query in a variable. It will run on its own and since you're only inserting data, not retrieving any, the variable will never have a value.

what you can do is this:
$query = "INSERT INTO ´cs09(`regno`,`name`) VALUES ('{$regno}','{$name}')";
you can choose which columns to insert into.


/Edit:

I didn't notice the space in your query ( where the ID field is ) without syntax tags. What you're doing now is actually inserting a space in the ID field.
at least I think so. I don't know if mysql takes a space as a valid character or if it treats it like null.
kpkguru003
Posts: 14
Joined: Fri Nov 11, 2011 12:32 pm
Location: India

Re: Forms

Post by kpkguru003 »

i got the output ,,,, thank you very much ...

$query = "INSERT INTO cs09 (id,regno) values(' ','$regno','$name');
Post Reply