Script needed to add data via web page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pha3dr0n
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 9:12 am

Script needed to add data via web page

Post by pha3dr0n »

I have a database, call it "data". There's a table in it called "movies". The parameters for the entries is :

Title varchar (50) not null, primary key
genre (enum - horror, action, comedy, etc)
format (enum - dvd, video, vcd, etc)
no of cd's (int 2 unsigned).

I have written a script whereby I enter the whole INSERT query and it works, but I am trying to write a script where I can just enter the values into a web page, hit submit, and it enters the values into the database. I have tried a variety of script attempts, but have got nowhere !!! I admit I am a complete noobie at this, and have been trying to learn this for a week (I hadent even tried html until last week).

Can anybody help, or even post a script that they use that I could edit ??

Thanks in advance.

Pha3dr0n
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

make the fields in html...
call them

Code: Select all

title
genre
format
no
then put this between your <form> and </form>

Code: Select all

<input type='hidden' name='ready'>
and use this query/script

Code: Select all

<?php
if(IsSet($ready))
&#123;
$sql - mysql_query("insert into movies values('$title','$gener','$format','$no'");
&#125;
?>
hope that helps
pha3dr0n
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 9:12 am

Post by pha3dr0n »

I think I know what you mean, but I'm not too sure how to put it together.

/why cant there be a cure for dyslexia ??? :(
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Include this in your page that you're going to submit the data:-

Code: Select all

&lt;form action="&lt;?=$PHPSELF ?&gt;" method="POST"&gt;
  &lt;input type="text" name="title"&gt;
  &lt;input type="text" name="genre"&gt;
  &lt;input type="text" name="format"&gt;
  &lt;input type="text" name="no"&gt;
  &lt;input type="hidden" name="ready"&gt;
  &lt;input type="submit" value="Submit!"&gt;
&lt;/form&gt;
Then have this code at the top of your form page:-

Code: Select all

&lt;?php
if(isset($_POST&#1111;"ready"])) {
  $sql = "INSERT INTO movies VALUES ('".$_POST&#1111;"title"]."','".$_POST&#1111;"genre"]."','".$_POST&#1111;"format"]."','".$_POST&#1111;"no"]."')";
  mysql_query($sql);
}
?&gt;
Then the data will be inserted!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You probably want to change:

Code: Select all

&lt;form action="&lt;?=$PHPSELF ?&gt;" method="POST"&gt;
to

Code: Select all

&lt;form action="&lt;?php echo $_SERVER&#1111;'PHPSELF']; ?&gt;" method="POST"&gt;
in case register globals is turned off either in your or your hosts version of PHP.

Mac
pha3dr0n
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 9:12 am

Post by pha3dr0n »

Takuma - I take it that you mean I use the first script in the php pahe, and the second in the html pahe that will point to the php one ??
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you could, but it is better to put it in the same page, the php script won't run untill the form is submitted.
Post Reply