Jump to content

Inserting from form to database(I need help please.)


madjack87

Recommended Posts

I have a test.php page and a insert.php page on my site

 

Here is the code for the insert.php

 

<?php
$connection = mysql_connect("mysql","madjack87","polosport1");
if (!$connection){
	die("Could not connect: ") . mysql_error());
}

mysql_select_db("madjacknews", $connection);

$sql="INSERT INTO members (email)
VALUES
('$_POST[email]')";

if(!mysql_query($sql,$connection)){
	die("Error: " . mysql_error());
}
echo "1 record added";
mysql_close($connection)
?>

 

Here is the code for the test.php page

 

<form action="insert.php" method="post">
Email: <input type="text" name="email" />
<input type="submit" />
</form>

 

After clicking submit on the form it takes me to insert.php and the page is blank.

 

And when checking phpmyadmin there where no new records added.

 

Any help?

You do not need mysql_close as an fyi.

 

<?php
   $connection = mysql_connect("mysql","madjack87","polosport1");
   if (!$connection){
      die("Could not connect: ") . mysql_error());
   }
   
   mysql_select_db("madjacknews") or die("Unable to select database: " . mysql_error());
   
   $sql="INSERT INTO members (email)
   VALUES
   ('{$_POST['email']}')"; // notice the use of { and }, doing that will allow the array index to display without error.
   
   if(!is_resource(mysql_query($sql))){
      die("Error: " . mysql_error());
   }
   echo "1 record added";
?>

 

Also the $connection is not required for mysql unless you are using multiple databases.

 

Try the above and see what happens...I also added to your if the is_resource definition. If the SQL had a syntax error it will not return a valid resource, so that is a decent check to do.

I just tried this exact code still no change.

 

You do not need mysql_close as an fyi.

 

<?php
   $connection = mysql_connect("mysql","madjack87","polosport1");
   if (!$connection){
      die("Could not connect: ") . mysql_error());
   }
   
   mysql_select_db("madjacknews") or die("Unable to select database: " . mysql_error());
   
   $sql="INSERT INTO members (email)
   VALUES
   ('{$_POST['email']}')"; // notice the use of { and }, doing that will allow the array index to display without error.
   
   if(!is_resource(mysql_query($sql))){
      die("Error: " . mysql_error());
   }
   echo "1 record added";
?>

 

Also the $connection is not required for mysql unless you are using multiple databases.

 

Try the above and see what happens...I also added to your if the is_resource definition. If the SQL had a syntax error it will not return a valid resource, so that is a decent check to do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.