Jump to content

posting data in categories


vividona

Recommended Posts

I found this code in google

 

CREATE TABLE 
`categories` ( `id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL, 
`parent` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;
-- -- Dumping data for table `categories` -- 

INSERT INTO `categories` 
(`id`, `name`, `parent`) VALUES 
(1, 'Web development', 0),
(2, 'Application development', 0), 
(3, 'Linux', 0), (4, 'Misc', 0), 
(5, 'Php', 1), (6, 'Mysql', 1), 
(7, 'Javascript', 1), 
(8, 'CSS', 1), 
(9, 'C plus plus', 2),
(10, 'wxWidgets', 2), 
(11, 'Tutorials', 3), 
(12, 'My thoughts', 4);

 

I need to know

how can I used $_POST['submit']

to insert like these random numbers in my database

parent column seem have strange numbers

 

Link to comment
https://forums.phpfreaks.com/topic/168497-posting-data-in-categories/
Share on other sites

Just to let you know i don't want you to think we are all being rude but I don't actually understand the question?

 

Please make it more understandable all you have done is post the code to create a database table and insert its contents.

 

If you want to use $_POST you will have to make a form first.

Just to let you know i don't want you to think we are all being rude but I don't actually understand the question?

 

Please make it more understandable all you have done is post the code to create a database table and insert its contents.

 

If you want to use $_POST you will have to make a form first.

 

Hi shadiadiph

 

thx for your reply.

I know that the posting need a form

I made the form

$id = $_POST['id'];
$name = $_POST['name'];
$parent = $_POST['parent'];

 

all I need to know is how can I post $parent and fill the column [parent] in my db with numbers like the data that are in the db now

INSERT INTO `categories` 
(`id`, `name`, `parent`) VALUES 
(1, 'Web development', 0),
(2, 'Application development', 0), 
(3, 'Linux', 0), (4, 'Misc', 0), 
(5, 'Php', 1), (6, 'Mysql', 1), 
(7, 'Javascript', 1), 
(8, 'CSS', 1), 
(9, 'C plus plus', 2),
(10, 'wxWidgets', 2), 
(11, 'Tutorials', 3), 
(12, 'My thoughts', 4);

This is but a simple SQL query.

 

You will need to make a submit form using HTML.

example:

<form action="whatever.php" method="POST">
<input type="text" name="blah">
<input type="submit" name="submit" value="Submit">
</form>

 

And then make a PHP file to process the submitted data,

example:

<?php
if ($POST['blah']) {//if you receive the form submission for the text area name, blah

//if yes then do whatver you need it to do.

}
?>

 

Hope it helps

After making the form receiving code,

you will need the processing code.

 

Such as:

<?php
if ($POST['submit']) {//if you receive the form submission for the submit button

//You do not need to submit an id, since it is auto-increment.
$name = $_POST['name'];//if yes then make variable $name as the value of the text area //named name
$parent = $_POST['parent'];//and so on

//after defining the variables, you will need to connect to your database.
//Let's define your database details.
$host = 'localhost'; //The host for your database.
$username = 'username'; //your username assigned to your database
$password = 'password'; //your password assigned to your user & database
$database = 'database'; //your database name

//After that, we connect to it.
$connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

//After that, we insert those variables we defined.
$query = "INSERT INTO `categories` (`name`, `parent`) VALUES ($name, $parent);
@mysql_query("$query") or die('There was an unexpected error adding the info into the database.');
mysql_close();

}
?>

Oh my, I see to have forgotten to finish close up with a (") and a few of those (').

 

Please replace:

//After that, we insert those variables we defined.
$query = "INSERT INTO `categories` (`name`, `parent`) VALUES ($name, $parent);
@mysql_query("$query") or die('There was an unexpected error adding the info into the database.');
mysql_close();

 

With this:

//After that, we insert those variables we defined.
$query = "INSERT INTO `categories` (`name`,`parent`) VALUES ('$name','$parent');";
@mysql_query("$query") or die('There was an unexpected error adding the info into the database.');
mysql_close();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.