Jump to content
xisto Community
contactskn

A Data Adding Prob In Php please help

Recommended Posts

Dear friends I have a database of mysql named as contactskn_skndatathe username for the database is contactskn_adminthe password is "xyz"The table created is named as matri?The fields are named as "name, dob"I want a simple code to add data through a form regarding name and date of birth of the person entering data.?i want a PHP code for entering the data into this mysql database.?Please help me.?

Share this post


Link to post
Share on other sites

I shall assume a few things for this: [1] You already have your form set up with the input fields named properly, [2] you use the POST method for the form, [3] your script is already set up to connect to the database and is included in the necessary pages, and [4] your table has a column that auto increments (and is the primary key) named "id."

 

Since the POST method is used, we have to use the predefined PHP variable, _POST, to obtain the values from the submitted form. For security reasons, we shall use mysql_real_escape_string to filter out the values in _POST. This is easily achieved with array_map since _POST is a variable that holds an array.

$_POST = array_map('mysql_real_escape_string', $_POST);

The function mysql_query is pretty much self-explanatory, to execute MySQL queries. You want to insert into the database the values retrieved from the form.

mysql_query("INSERT INTO `matri` (`id`, `name`, `dob`) VALUES (NULL, '".$_POST['name']."', '".$_POST['dob']."')");
To get all the data in the database you use select from.

if ($query = mysql_query("SELECT `name`, `dob` FROM `matri`")){	 while ($result = mysql_fetch_object($query))	 {		  echo $result->name;		  echo $result->dob;	 }}

Share this post


Link to post
Share on other sites

Dear friend according to your instructions I coded as follows no error is being displayed but the required results are not gained I mean to say the data is not added to the database. Please do check it and please guide me actually where am I mistaken. In the sno field autoincrement property is set true and it is set as the primary key. Here with this code I like to add 1 in the sno and 'sanju' in the name field. Please help me. It is the exact code being hosted. Thank you in advance and Thank you very much for your previous help on this topic. 

<?php $username = "contactskn_admin";$password = "*****";$hostname = "localhost"; $dbh = mysql_connect($hostname, $username, $password)  or die("Unable to connect to MySQL");print "Connected to MySQL<br>";$selected = mysql_select_db("contactskn_skndata",$dbh)  or die("Could not select contactskn_skndata");$result = mysql_query("SELECT sno, name FROM matri");// you're going to do lots more here soonmysql_query("INSERT INTO `matri` (`sno`, `name`, `dob`, `phone`, `adress`, `email`, `sex`) VALUES (1, 'sanju')"); 		 mysql_close($dbh);?>

Share this post


Link to post
Share on other sites

For an auto-incremented field, you don't need to supply the number; it's better if you don't. So instead of supplying a number, supply null instead. Also, you don't have to provide every field in the table to insert things, you only need to provide the fields you're going to be inserting to.

mysql_query("INSERT INTO `matri` (`sno`, `name`) VALUES (NULL, 'sanju')");
I'm not sure how you're viewing the database, whether through this script, MySQL Query Browser, PHPMyAdmin, or other, but if it's through this exact script you have posted, it's not going to show the results. And if you want to retrieve data, select from has to come after the data is inserted.

Share this post


Link to post
Share on other sites

Thought you might like to see this link.
Handy for small databases and saves repetitive coding.




Thank you very much dear friends for the kind help done to me. The codes provided by you all were really helpful and my problem was solved for the time being. If a problem arises in the future then hope you all be there to help me. Thanks again. And a very very happy NEW YEAR in Advance.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.