n00b question

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
lyte
Forum Newbie
Posts: 3
Joined: Thu Feb 26, 2004 9:03 am

n00b question

Post by lyte »

I'm trying to setup my first mysql database and insert data into it using forms. I've been looking through a tutorial and trying to use code from it to test my dbase. everything seems to work, I can pull data from it but when I try to add data to it using forms and a button, it just seems to refresh the page and not insert the data. The code I have been using is as follows:

Code: Select all

<html>

<body>

<?php

if ($submit) &#123;

  // process form

  $db = mysql_connect("localhost", "root", "password");

  mysql_select_db("mydb",$db);

  $sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";

  $result = mysql_query($sql);

  echo "Thank you! Information entered.\n";

&#125; else&#123;

  // display form

  ?>

  <form method="post" action="<?php echo $PHP_SELF?>">

  First name:<input type="Text" name="first"><br>

  Last name:<input type="Text" name="last"><br>

  Address:<input type="Text" name="address"><br>

  Position:<input type="Text" name="position"><br>

  <input type="Submit" name="submit" value="Enter information">

  </form>

  <?php

&#125; // end if

?>

</body>

</html>
Now what happens is that, I fill out the forms and click the submit button and the page just seems to refresh, no error message just a reload of the page and the data does not get inserted but it clears the data from the textboxes. I noticed that in the "if then statement that if i took out the $ from $submit that it would show me: "Thank you! Information entered." and it inserts blank data into the database. What could I be missing? It just seems like the varible $submit is not being set = true when I click the button. The mysql_connect is set correctly on my webpage. Thanks for any help you can provide.

LyTe
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Unfortunately the tutorial you are following is a bit outdated and relies on functionality that is no longer available by default in PHP.

You need to change:

Code: Select all

<form method="post" action="<?php echo $PHP_SELF?>">
to

Code: Select all

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
and

Code: Select all

if ($submit) {
to

Code: Select all

if (isset($_POST['submit'])) {
and

Code: Select all

$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
to

Code: Select all

$sql = "INSERT INTO employees (first,last,address,position) VALUES ('{$_POST['first']}','{$_POST['last']}','{$_POST['address']}','{$_POST['position']}')";
Mac
lyte
Forum Newbie
Posts: 3
Joined: Thu Feb 26, 2004 9:03 am

Post by lyte »

I implemented the requested changes and now I get a: Parse error: parse error, unexpected T_STRING in /var/www/html/test.php on line 10
which refers to the $db = mysql_connect("localhost","root","password"); line. I have the proper hostname, username and password in there. Any other help would be greatly appriciated.

Code: Select all

<html> 
 <body> 

 <?php 

if (isset($_POST&#1111;'submit'])) &#123;

   // process form 

   $db = mysql_connect("localhost","root","password"); 

   mysql_select_db("mydb",$db); 

$sql = "INSERT INTO employees (first,last,address,position) VALUES ('&#123;$_POST&#1111;'first']&#125;','&#123;$_POST&#1111;'last']&#125;','&#123;$_POST&#1111;'address']&#125;','&#123;$_POST&#1111;'position']&#125;')";


  $result = mysql_query($sql); 

   echo "Thank you! Information entered.\n"; 

 &#125; else&#123; 

   // display form 

   ?> 
<form method="post" action="<?php echo $_SERVER&#1111;'PHP_SELF']; "> 

First name:<input type="Text" name="first"><br>

Last name:<input type="Text" name="last"><br>

Address:<input type="Text" name="address"><br>

Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>


</body>

</html>
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

you shoud think about simplifying your $_POST['variables']; by turning them into something else

for eg

Code: Select all

$firstname = $_POST['first'];
this will make your queries look much nicer, and therefore easier to debug
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

String problem, I think. Try concatenating.

Code: Select all

<?php
$sql = "INSERT INTO employees (first,last,address,position) 
           VALUES ('" . $_POST['first'] . "','" . $_POST['last'] . "','" . $_POST['address'] . "','" . $_POST['position'] . "')";
?>
Last edited by McGruff on Tue Aug 09, 2005 3:41 pm, edited 1 time in total.
lyte
Forum Newbie
Posts: 3
Joined: Thu Feb 26, 2004 9:03 am

Post by lyte »

Thanks but its still no good.

Code: Select all

<html> 
 <body> 

 <?php 

if (isset($_POST&#1111;'submit'])) &#123;

   // process form 

   $db = mysql_connect("localhost","root","password"); 

   mysql_select_db("mydb",$db); 

$sql = "INSERT INTO employees (first,last,address,position) 
            VALUES ('" . $_POST&#1111;'first'] . "','" . $_POST&#1111;'last'] . "','" . $_POST&#1111;'address'] . "','" . $_POST&#1111;'position'] . "')"; 



  $result = mysql_query($sql); 

   echo "Thank you! Information entered.\n"; 

 &#125; else&#123; 

   // display form 

   ?> 
<form method="post" action="<?php echo $_SERVER&#1111;'PHP_SELF']; "> 

First name:<input type="Text" name="first"><br>

Last name:<input type="Text" name="last"><br>

Address:<input type="Text" name="address"><br>

Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>



</body>

</html>
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Try using this :

Code: Select all

<html> 
<body> 

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

First name:<input type="Text" name="first"><br> 

Last name:<input type="Text" name="last"><br> 

Address:<input type="Text" name="address"><br> 

Position:<input type="Text" name="position"><br> 

<input type="Submit" name="submit" value="Enter information"> 
</form> 
</body> 
</html> 

<?php 

if (isset($_POST['submit'])) { 

  // process form 

  mysql_connect("localhost","root","password") or die(MySQL_Error());
  mysql_select_db("mydb") or die(MySQL_Error()); 

  $sql = "INSERT INTO employees (first,last,address,position) VALUES ('".$_POST['first']."','".$_POST['last']."','".$_POST['address']."','".$_POST['position']."')"; 
  $result = mysql_query($sql) or die(MySQL_Error());

  echo "Thank you! Information entered.\n"; 

} else{ 

  // display form 

?>
and see if you are getting any errors.. (notice the die messages)

(edit : you also missed a ?> in the <form method="post" action="<?php echo $_SERVER['PHP_SELF'];"> . so i added that too
Post Reply