Worstof Posted February 12, 2008 Share Posted February 12, 2008 Hello...just started yesterday trying out PHP and mysql. I have created a database and connect to it and add records through a simple html form. However, whenever I try to pass the input I type in via the form to either the form itself (via $_SERVER['PHP_SELF'] or calling another php file) nothing gets passed. When I try to use the $_SERVER method, I get a HTTP error 404, page not found add.phpaddrsvp=1 or when I pass to another php file, the code adds an empty record to the database. Right now, I'm on an iteration where I have two seperate files...one to enter the data and another to add to the database. I've included the code here. I'm at a loss...I've searched the forums with no success...I'm sure it is something very basic that I'm missing. Thanks for the help. -----This is the first file. Accepts the simple input and calls add.php ---------- <html> <head><title>User Admin Page : Add a User</title></head> <body bgcolor="#ffffff"> <table bgcolor=#000000 valign=top align=center border=0><tr><td bgcolor=#000000><table cellpadding=4 bgcolor=#ffffff cellspacing=2 border=0> <Tr><th>Add a User</th></tr><tr><td> <FORM METHOD="post" ACTION="add.php"> Last Name: <INPUT TYPE=text MAXLENGTH=70 NAME="lastname" SIZE=20><Br> First Name: <INPUT TYPE=text MAXLENGTH=70 NAME="firstname" SIZE=20><Br> <INPUT TYPE=submit VALUE="Add"> <INPUT type=reset VALUE="Reset Form"></form> </tr></td></table></tr></td></table> </body> </html> -----This is the second file...supposed to get the lastname and firstname from the first file and add to database but it doesn't------ <html> <body> <?php $db = mysql_connect("localhost","myusername","mypassword"); mysql_select_db (mydbtable); $result = mysql_query ("INSERT INTO mydbtable(lastname, firstname) VALUES ('$lastname', '$firstname')"); if(!$result) { echo "<b>User not added:</b> ", mysql_error(); exit; } if($result) { mysql_close($db); print "User <b>$lastname</b> added sucessfully!"; } else { print ("Didn't work"); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/90703-new-to-php-need-help-input-data-into-a-record/ Share on other sites More sharing options...
soycharliente Posted February 12, 2008 Share Posted February 12, 2008 You need to grab the data from the POST array. I don't see where you are doing that. $lastname = $_POST["lastname"]; Something to that effect for each variable that you're trying to work with. Quote Link to comment https://forums.phpfreaks.com/topic/90703-new-to-php-need-help-input-data-into-a-record/#findComment-464929 Share on other sites More sharing options...
Worstof Posted February 12, 2008 Author Share Posted February 12, 2008 Thanks a ton! That made it work. I still can't get the $_SERVER[php_SELF] to pass arguments back to itself. Any thoughts would be appreciated. <html> <head> <title></title> </head> <body bgcolor="#FFFFFF"> <?php if (isset($_GET['addrsvp'])): //add a RSVP ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Last name: <input type="test" name="lastname" /></label><br /> <label>First name: <input type="test" name="firstname" /></label><br /> <input type="submit" value="SUBMIT" /> </form> <?php else: //Default page display //Connect to DB server $dbcnx=@mysql_connect('localhost', 'db', 'pswd'); if (!$dbcnx) { exit('<p>Unable to connect to server</p>');} //Select the rsvp database if (!@mysql_select_db('mydb')) { exit('<p> Unable to connect to database</p>'); } if (isset($_POST['lastname'])) { $lastname=$_POST['lastname']; $firstname=$_POST['firstname']; $sql="INSERT INTO rsvp SET lastname='$lastname', firstname='$firstname'"; // if (!@mysql_query($sql)) { // echo '<p>Did not add RSVP to database</p>'; // } } //echo '<p>Here are all the people registered.</p>'; //Request all the rsvps //$result=@mysql_query('SELECT lastname FROM rsvp'); //if (!$result) { // exit('<p>Error performing query: ' . mysql_error() . '</p>; //} //while ($row=mysql_fetch_array($result)) { // echo '<p> .$row['lastname'] . '</p>'; //} //When clicked this link will load this page with the people listed echo '<p><a href"' . $_SERVER['PHP_SELF'] . '?addrsvp=1">RSVP</a></p>'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/90703-new-to-php-need-help-input-data-into-a-record/#findComment-465186 Share on other sites More sharing options...
chronister Posted February 12, 2008 Share Posted February 12, 2008 Maybe it's just me, but please explain a little more clearly on what your trying to accomplish... also, instead of <?php echo '<p><a href"' . $_SERVER['PHP_SELF'] . '?addrsvp=1">RSVP<a></p>'; ?> Try, <?php echo '<p><a href=" ' . $_SERVER['PHP_SELF'] . '?addrsvp=1">RSVP</a></p>'; ?> Nate Quote Link to comment https://forums.phpfreaks.com/topic/90703-new-to-php-need-help-input-data-into-a-record/#findComment-465193 Share on other sites More sharing options...
Worstof Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks...I'll fix the /a piece. Think I've got this fixed. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/90703-new-to-php-need-help-input-data-into-a-record/#findComment-465519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.