Help Please... this doesnt make sense (PHP)

Hi guys,

I can’t describe how frustrated this has made me… and it’s the littlest thing but I can’t go any further without it working…

I have a simple form with a textfield and a submit button

The main goal is to send the value of the textfield to the database when you click Submit…

Problem is that when I click submit it updates the database but it inserts a blank instead of the value I typed in the textfield…
When I do my INSERT command where I use the textfield variable I’m also using a CURDATE() to insert a date into the database which actually works and the dates shows up in the database.

Given that I can now narrow it down to the fact that php just won’t recognize the value of my textfield… BUT… when I use a post method for my form to see the URL values passed the value for my textfield (which PHP doesn’t recognize) is in fact displayed in the URL…

here is the full code


<html>
<head><title>WORK!!!!</title>
</head>
<body>
<form action="<?=$PHP_SELF?>" method="get">
<p>Type your name here:<br />
<input type = "text" name = "initial"><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php  // Default page display    
// Connect to the database server    
$dbcnx = @mysql_connect("localhost", "root", "");    
if (!$dbcnx) {      echo( "<p>Unable to connect to the " .            "database server at this time.</p>" );      exit();    

}else {
echo("Connected to Dabase Server");
}
    
// Select the employee database    
if (! @mysql_select_db("employee") ) {      
echo( "<p>Unable to locate the employee " .            "database at this time.</p>" );      
exit();    
}
else {
echo("Connected to database employee");
}    

** this part right now does not execute since I’m once again trying to use a variable from my form. If I remove the first condition where I check for SUBMIT it processes the sql INSERT statement and inserts the date but of course not the Name**


if ($submitjoke == "SUBMIT") {
$sql = "INSERT INTO hrdata SET Name = '$initial', Hired = CURDATE()";      
if (@mysql_query($sql)) {        
echo("<p>Your name has been added.</p>");      } 
else {        
echo("<p>Error adding name: " .             mysql_error() . "</p>");      
}   
}

      
 echo("<p> Here are all the names in our database: </p>");      
 $result = @mysql_query("SELECT Name, Hired FROM hrdata");    
 if (!$result) {      echo("<p>Error performing query: " . mysql_error() . "</p>");      exit();    
 }      // Display the text of each joke in a paragraph    
 while ( $row = mysql_fetch_array($result) ) {      echo("<p>" . $row["Name"] . " " . $row["Hired"] ."</p>");    }      
 ?>
 </body>
 </html>

does anybody know what’s going on here?

Thank you kindly in advance

EDIT: hmm… the thread actually converts my html code and makes the form with the button and textfield… but this way you can actually see in the URL after you click submit that the actual value is passed to it… but PHP won’t take it and put it in the database… I’m using MySQL btw

for your INSERT command try this instead:


"INSERT INTO hrdata (Name, Hired) VALUES ('$initial', 'CURDATE()')"

that might work. What I usually do is put my HTML and my PHP code into two seperate files. Helps to keep my codes cleaner. Although it makes for twice as many files in my directories…

Oh yeah… What was your initial Create TABLE command? because if you issued a wrong statment that could be the difference too… like once I accidently put "CREATE TABLE links(NAME INT(15), … etc

So name kept reading as a 0 because I kept putting letters into the form, but it was looking for numbers…

let me know if that works and if not then I will try to come up with something later.

no that didn’t work but I fixed it already by using $_GET(‘initial’); to retreive my variables :slight_smile:

thanks thou

I think your problem was because you have register_globals turned off. This is the defualt in newer builds of php. It is a security messure so that attackers cant forge posted values. Here is the explanation http://www.php.net/manual/en/security.registerglobals.php

yep that was the problem

apparently they are suggestion to keep it to “OFF” in .ini file and use $_GET rather so that’s what I did :slight_smile: