10 Replies - 1695 Views - Last Post: 08 July 2009 - 08:39 PM Rate Topic: -----

#1 tuanis70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 09

Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 07:38 PM

Hello everyone! I'm new to php and have been having a problem. See, I've been trying to create a webpage where someone can enter text and hit submit, and the text will be added to a row in a MySQL 5.0.51a database called contactdb. But, I keep getting two errors. The first one reads, "Notice: Undefined variable: testField in insert.php on line 15". The error below that text reads, "Something went wrong: Incorrect integer value: '' for column 'id' at row 1". The "id" row is an auto increment column, not null. Please help!
-------------------------------------------------
Here's the webpage html:
<HTML>
<HEAD>
<TITLE>Insert Form</TITLE>
</HEAD>
<BODY>
<FORM ACTION="insert.php" METHOD=POST>
<p>Text to add:<br>
<input type=text name="testField" size=30>
<p><input type=submit name="submit" value="Insert Record"></p>
</FORM>
</BODY>
</HTML>


---------------------------------------------------
Here's the PHP code:
<?php

// Open the connection
$con = mysql_connect("localhost","mark","mypass567");

// Pick the database to use
mysql_select_db("contactdb", $con);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create the SQL Statement
$sql = "INSERT INTO testtable values ('', '$testField')";

// Execute the Statement
if (mysql_query($sql,$con)) 
  {
  echo "Record Added";
  }
else
  {
  echo "Something went wrong: " . mysql_error();
  }

mysql_close($con);
?> 


---------------------------------------------------
None of the code has been altered. This is exactly as I have it. Please help!

Thanks in advance!

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined variable: testField in insert.php on line 15

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 07:41 PM

You need to pull the data off the POST array.

<?php

if($_POST['testField']!="") {
  $testField=strip_tags($_POST['testField']);
}
else die("Test Field was not set!");

// Open the connection
$con = mysql_connect("localhost","mark","mypass567");

// Pick the database to use
mysql_select_db("contactdb", $con);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create the SQL Statement
$sql = "INSERT INTO testtable values ('', '$testField')";

// Execute the Statement
if (mysql_query($sql,$con))
  {
  echo "Record Added";
  }
else
  {
  echo "Something went wrong: " . mysql_error();
  }

mysql_close($con);
?> 

Was This Post Helpful? 0
  • +
  • -

#3 tuanis70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 09

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 07:43 PM

What a LIGHTNING FAST response! Thanks! I'll try it.
Was This Post Helpful? 0
  • +
  • -

#4 tuanis70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 09

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 07:49 PM

Ok, pulling the data off the POST seems to have worked but I still get the, "Something went wrong: Incorrect integer value: '' for column 'id' at row 1." How do I fix that? The row "id" is an auto increment and not not null row. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 07:54 PM

Quote

$sql = "INSERT INTO testtable values ('', '$testField')";


What columns are you putting them into? Does the 1st value accept NULL?
Was This Post Helpful? 0
  • +
  • -

#6 tuanis70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 09

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 08:15 PM

The columns are "id" and "testField". The id column is auto incrementing. The rest is: Type = int, I need the id column to auto increment as data is entered. I'm using MySQL 5.0.51a. I read somewhere that this version has a bug concerning that? This is really bugging me! lol.
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 08:18 PM

When you created your table, did you set the value of the column to auto increment?

Drop table Product;
  
CREATE TABLE Product
(
   ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
   Name VARCHAR(40) NOT NULL,
   PRIMARY KEY (ID)
);

Describe Product;


Was This Post Helpful? 0
  • +
  • -

#8 tuanis70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 09

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 08:22 PM

Yes, the id column is set to auto increment.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 08:23 PM

I would still remove the blank VALUE from insert. Let the column ID auto increment without forcing data into it. It's possible that it's just a difference in coding style, but I've never used a VALUE without specifying the columns that I'm going to insert into.
Was This Post Helpful? 0
  • +
  • -

#10 tuanis70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-July 09

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 08:35 PM

I figured out what to do! Thanks so much! The way I fixed it was by forcing a '0' value to be added in the php code. Therefore, the auto increment assigned it a value when it entered it into the database. Nice! Thanks so much for your help! Your guidance is greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Undefined variable: testField in insert.php on line 15

Posted 08 July 2009 - 08:39 PM

:D :^:

Glad to see you got it figured out!

Looking forward to seeing you around the forums some more :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1