Insert record - problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ansa1
Forum Newbie
Posts: 16
Joined: Sat Mar 27, 2004 9:19 am
Location: Ontario, Canada

Insert record - problem

Post by ansa1 »

Db contains table called “contacts”. I want to insert record to the table using an HTML form. All what I got are empty records stored in db with an ID# (INT auto incensement). I checked many postings to try to resolve this problem but no luck. I turned GLOBALS “ON’. Help please!

Code: Select all

<?php
$id=$_GET['id'];
$username="XXXX";
$password="YYYYY";
$database="forms";

$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

//$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";

$query=("INSERT INTO contacts (first, last, phone, mobile, fax, email, web) VALUES ('" . $first . "','" . $last . "','" . $phone . "','" . $mobile . "','" . $fax . "','" . $email ."','" . $web . "')" );
mysql_query($query);
mysql_close();
?>

FORM:

Code: Select all

&lt;body&gt;
&lt;form action="insert.php" method="post"&gt;
First Name: &lt;input name="first" type="text" size="20"/&gt;
&lt;br&gt;
Last Name: &lt;input name="last" type="text" size="20"&gt;
&lt;br&gt;
Phone: &lt;input name="phone" type="text" size="20"&gt;
&lt;br&gt;
Mobile: &lt;input name="mobile" type="text" size="20"&gt;
&lt;br&gt;
Fax: &lt;input name="fax" type="text" size="20"&gt;
&lt;br&gt;
E-mail: &lt;input name="email" type="text" size="20"&gt;
&lt;br&gt;
Web: &lt;input name="web" type="text" size="20"&gt;
&lt;br&gt;
&lt;input type="Submit"&gt;
&lt;/form&gt;

:?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try the debugging tips at viewtopic.php?t=17096 and see if they help.
User avatar
AnarKy
Forum Contributor
Posts: 119
Joined: Tue Nov 02, 2004 1:49 am
Location: South Africa

Post by AnarKy »

Did you check if that username has update permissions?

:(
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Try echoing each value before you try to enter them into the database to make sure they are ok. Then try printing out the query string to the page to see what it looks like, then if you still havent found the error deconstruct the query and try to get it to work with only one filed being submitted, then when that works add a second etc - until you have the full query.
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Re: Insert record - problem

Post by dimitris »

ansa1 wrote:Db contains table called “contacts”. I want to insert record to the table using an HTML form. All what I got are empty records stored in db with an ID# (INT auto incensement). I checked many postings to try to resolve this problem but no luck. I turned GLOBALS “ON’. Help please!

Code: Select all

<?php
$id=$_GET['id'];
$username="XXXX";
$password="YYYYY";
$database="forms";

$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

//$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";

$query=("INSERT INTO contacts (first, last, phone, mobile, fax, email, web) VALUES ('" . $first . "','" . $last . "','" . $phone . "','" . $mobile . "','" . $fax . "','" . $email ."','" . $web . "')" );
mysql_query($query);
mysql_close();
?>

FORM:

Code: Select all

&lt;body&gt;
&lt;form action="insert.php" method="post"&gt;
First Name: &lt;input name="first" type="text" size="20"/&gt;
&lt;br&gt;
Last Name: &lt;input name="last" type="text" size="20"&gt;
&lt;br&gt;
Phone: &lt;input name="phone" type="text" size="20"&gt;
&lt;br&gt;
Mobile: &lt;input name="mobile" type="text" size="20"&gt;
&lt;br&gt;
Fax: &lt;input name="fax" type="text" size="20"&gt;
&lt;br&gt;
E-mail: &lt;input name="email" type="text" size="20"&gt;
&lt;br&gt;
Web: &lt;input name="web" type="text" size="20"&gt;
&lt;br&gt;
&lt;input type="Submit"&gt;
&lt;/form&gt;

:?
also write mysql_query($query) or die (mysql_error()); to get any errors occured
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Re: Insert record - problem

Post by dimitris »

Wait wait! Your form says that you post the input variables but in your code you write $id=$_GET['id'];

Instead of it try $id=$_POST['id'];

Hope this works!
ansa1
Forum Newbie
Posts: 16
Joined: Sat Mar 27, 2004 9:19 am
Location: Ontario, Canada

Post by ansa1 »

Thanks - so much - for your support. Awesome forum – people -- to be more precise .
I tried almost all that you suggested in your posts. First of all I started with markl999 suggestions as the basic. I got many errors, which I don’t know how to resolve. Wherever I make changes , effects are the same - errors . I believe all of them correspond to the form. I changed form ( as far as I know how LOL ) =NONE= positive results.
User has ALL privileges. I changed ID to POST as well.
Here are the errors:

Code: Select all

Notice: Undefined index: id in C:\WEB\form_test\tester2.php on line 6

Notice: Undefined index: first in C:\WEB\form_test\tester2.php on line 7

Notice: Undefined index: last in C:\WEB\for........... (  MORE ERRRORS ).........

Notice: Undefined index: web in C:\WEB\form_test\tester2.php on line 13
INSERT INTO contacts VALUES ('','','','','','','','')

Then I tried something different. If you don’t mind to check this codes -- what is your opinion - should they work ?? I did not get any positive results but no errors as well -- it’s completely confusing .

Code: Select all

<?php
error_reporting(E_ALL);
$user="XXXXX";
$password="YYYYYY";
$database="forms";

$db = mysql_connect('localhost', $user, $password) or die(mysql_error());
mysql_select_db('forms') or die(mysql_error());
$sql="SELECT * FROM contacts";

$res=mysql_query($sql) or die(mysql_error());;
$row=mysql_fetch_assoc($res);
echo $sql;


?>
FORM:

Code: Select all

&lt;form action="insert.php" method="post" name="form1" id="form1"/&gt;
First Name: 
  &lt;input name="first" type="text" size="20" value="&lt;? echo $row&#1111;"first"]; ?&gt;"/&gt;
&lt;br&gt;
Last Name: &lt;input name="last" type="text" size="20" value="&lt;? echo $row&#1111;"last"]; ?&gt;"/&gt;
----------------------------------- ( MORE FIELDS) -------------------------------

Web: &lt;input name="web" type="text" size="20"value="&lt;? echo $row&#1111;"web"]; ?&gt;"/&gt;
&lt;br&gt;
&lt;input type="Submit"&gt;
&lt;input name="id" type="hidden" id="id" value="&lt;? echo $row&#1111;"id"]; ?&gt;"/&gt;
&lt;/form&gt;
Thanks
Post Reply