Page 1 of 1
Insert record - problem
Posted: Mon Nov 08, 2004 12:50 am
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
<body>
<form action="insert.php" method="post">
First Name: <input name="first" type="text" size="20"/>
<br>
Last Name: <input name="last" type="text" size="20">
<br>
Phone: <input name="phone" type="text" size="20">
<br>
Mobile: <input name="mobile" type="text" size="20">
<br>
Fax: <input name="fax" type="text" size="20">
<br>
E-mail: <input name="email" type="text" size="20">
<br>
Web: <input name="web" type="text" size="20">
<br>
<input type="Submit">
</form>

Posted: Mon Nov 08, 2004 2:20 am
by markl999
Try the debugging tips at
viewtopic.php?t=17096 and see if they help.
Posted: Mon Nov 08, 2004 3:05 am
by AnarKy
Did you check if that username has update permissions?

Posted: Mon Nov 08, 2004 8:26 am
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.
Re: Insert record - problem
Posted: Mon Nov 08, 2004 9:53 am
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
<body>
<form action="insert.php" method="post">
First Name: <input name="first" type="text" size="20"/>
<br>
Last Name: <input name="last" type="text" size="20">
<br>
Phone: <input name="phone" type="text" size="20">
<br>
Mobile: <input name="mobile" type="text" size="20">
<br>
Fax: <input name="fax" type="text" size="20">
<br>
E-mail: <input name="email" type="text" size="20">
<br>
Web: <input name="web" type="text" size="20">
<br>
<input type="Submit">
</form>

also write mysql_query($query) or die (mysql_error()); to get any errors occured
Re: Insert record - problem
Posted: Mon Nov 08, 2004 9:55 am
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!
Posted: Mon Nov 08, 2004 5:18 pm
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
<form action="insert.php" method="post" name="form1" id="form1"/>
First Name:
<input name="first" type="text" size="20" value="<? echo $rowї"first"]; ?>"/>
<br>
Last Name: <input name="last" type="text" size="20" value="<? echo $rowї"last"]; ?>"/>
----------------------------------- ( MORE FIELDS) -------------------------------
Web: <input name="web" type="text" size="20"value="<? echo $rowї"web"]; ?>"/>
<br>
<input type="Submit">
<input name="id" type="hidden" id="id" value="<? echo $rowї"id"]; ?>"/>
</form>
Thanks