Page 1 of 1

Script needed to add data via web page

Posted: Mon Sep 02, 2002 5:24 pm
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

Posted: Mon Sep 02, 2002 5:43 pm
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

Posted: Mon Sep 02, 2002 6:09 pm
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 ??? :(

Posted: Tue Sep 03, 2002 1:22 am
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!

Posted: Tue Sep 03, 2002 2:07 am
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

Posted: Tue Sep 03, 2002 6:51 am
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 ??

Posted: Tue Sep 03, 2002 8:15 am
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.