Jump to content

New to php - need help input data into a record


Worstof

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.