Help....if you can!

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
richcoleuk
Forum Newbie
Posts: 24
Joined: Fri Nov 05, 2004 1:33 pm

Help....if you can!

Post by richcoleuk »

I am trying to create a simple HTML form that calls a .php script called submitter. Submitter should then put the values into my database called test. The HTML code is:

<html>

<body>

<form method="post" action="submitter.php">

First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Int" name="age"><br>
<input type="Submit" name="submit" value="Enter information">

</form>

</body>

</html>

and the php script:

<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("test",$db);
$sql = "INSERT INTO test (FirstName,LastName,Age) VALUES ('$first','$last','$age')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
?>

All this does is put nothing into the database. When i display the database using a seperate php script it just shows 0??

Any ideas?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You will be running a version of php with registered globals off. Instead of using just $first, use $_POST['first'], and it should work.
richcoleuk
Forum Newbie
Posts: 24
Joined: Fri Nov 05, 2004 1:33 pm

Post by richcoleuk »

in the html or the php?

could you edit the code for me?

sorry im a newbie to this!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Form:

Code: Select all

&lt;html&gt; 
   &lt;body&gt; 
      &lt;form method="post" action="submitter.php"&gt; 
         First name:&lt;input type="text" name="first"&gt;&lt;br&gt; 
         Last name:&lt;input type="text" name="last"&gt;&lt;br&gt; 
         Age:&lt;input type="text" name="age"&gt;&lt;br&gt; 
         &lt;input type="Submit" name="submit" value="Enter information"&gt; 
      &lt;/form&gt; 
   &lt;/body&gt; 
&lt;/html&gt;
submitter.php

Code: Select all

<?php 
$db = mysql_connect("localhost", "root"); //connect to database
mysql_select_db("test",$db);  //select database

//check that values are present - can add more checking at this poing
if(isset($_POST['first'])){
   $first = $_POST['first'];
}else{
   $first = "";
}
if(isset($_POST['last'])){
   $last = $_POST['last'];
}else{
   $last = "";
}
if(isset($_POST['age'])){
   $age = $_POST['age'];
}else{
   $age = "";
}

//insert data into database
$sql = "INSERT INTO test (FirstName, LastName, Age) VALUES ('$first', '$last', '$age')"; 
$result = mysql_query($sql); 
echo "Thank you! Information entered.\n"; 
?>
Post Reply