Jump to content

INSERT function problem


BigDaddy13

Recommended Posts

I am trying to learn php and mysql. I have started a tutorial and was playing around with one of the tasks. I have a created a test database with a contacts table. I can connect to the database and everything seems to work. When using firefox, I fill in the form and submit

and it puts most of the info in. It will put the phone, mobile, fax, email, and web info, but

will not put the first and last info. However, when I use IE, it inserts all data fine.

Any thoughts?

<?php
$user="*******";
$password="******";
$database="*******";
$con = mysql_connect(localhost,$user,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($database, $con);

$sql="INSERT INTO contacts (first, last, phone, mobile, fax, email, web)
VALUES
('$_POST[first]','$_POST[last]','$_POST[phone]','$_POST[mobile]','$_POST[fax]','$_POST[email]','$_POST[web]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 

 

 

That is my insert.php code.

 

Below I will post my html code

 

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="first" /> <br>
Lastname: <input type="text" name="last" /> <br>
Phone: <input type="text" name="phone" /> <br>
Mobile: <input type="text" name="mobile" /> <br>
Fax: <input type="text" name="fax" /> <br>
Email: <input type="text" name="email" /> <br>
Web: <input type="text" name="web" /><br>
 <input type="submit" />
</form>

</body>
</html> 

 

 

Thanks in advance!

 

 

BD

Link to comment
https://forums.phpfreaks.com/topic/161977-insert-function-problem/
Share on other sites

this:

('$_POST[first]','$_POST[last]','$_POST[phone]','$_POST[mobile]','$_POST[fax]','$_POST[email]','$_POST[web]')";

is not correct.

 

You must do one of the following:

1) wrap brackets around array var

('{$_POST[first]}',{'$_POST[last]}', repeat for the others...

OR:

2)define new regular vars, not arrays

$first = $_POST['first'];

$last = $_POST['last'];

('$first','$last,' repeat for the others...

[/hp]

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.