Simple Input Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Simple Input Form

Post by JimiH »

Hi, trying to create my first input form (Below)

Code: Select all

<head>
</head>
<center>
<form method="post" action="inputcat.php">
<table>
<tr><td align="left">Catagory</td>
<td><input type="text" name="category"></td>
<tr><td align="left">cat_id</td>
<td><input type="text" name="cat_id"></td>
<tr><td colspan="2">
<p align="center">
<input type="submit" value="Enter New Catagory">
</td>
</tr>
</table>
</form>
</center>
</html>
This passes the entered values into inputcat.php

Code: Select all

<?
    
error_reporting(E_ALL);

include 'upload2/config.php';		//Connect to DB etc
include 'upload2/opendb.php';

 $category=$_POST['category'];
 $cat_id=$_POST['cat_id'];


$sqlquery = "INSERT INTO category VALUES('$category', '$cat_id')";

$results = mysql_query($sqlquery);

mysql_close();

print "<html><body><center>";
print "<p>You have just entered this record<p>";
print "Category : $category<br>";
print "ID : $cat_id<br>";

print "</body></html>";
?>
I get the "You have just entered this record" with the values I typed in on the input form.

But when I query the "category" table the record isn't entered?
I have error reporting on "ALL" but no errors are displayed.

Thanks

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Doh

Had the "cat_id" after "category"

Code: Select all

$sqlquery = "INSERT INTO category VALUES('$category', '$cat_id')";

Code: Select all

$sqlquery = "INSERT INTO category VALUES('$cat_id', '$category')";
Fixed, still dont know why Mysql didn't throw an error?

Geoff
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

This

Code: Select all

$sqlquery = "INSERT INTO category VALUES('$category', '$cat_id')";
should be more like this

Code: Select all

$sqlquery = "INSERT INTO category (column_name_category, column_name_catid) VALUES('$category', '$cat_id')";
To see why you SQL command is failing, append mysql_error() to the query like so:

Code: Select all

$results = mysql_query($sqlquery) or die(mysql_error());
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

It depends on the system you are running and the error reporting you have setup in your PHP.INI! It's always a good idea to validate each function so you know what is happening...

If you did this....

Code: Select all

$results = mysql_query ( $sqlquery ) or die ( mysql_error () );
You would have seen the error!

pif!
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks for the help.

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Hi

I would like the cat_id to be inserted into the DB automatically.

At the moment I type in the cat_id & the catagory manually.

I have tried

Code: Select all

<input type="hidden" name="id" value="null">
This gives the following error

Code: Select all

Out of range value adjusted for column 'cat_id' at row 1PHP Notice: Undefined index: cat_id in C:\websites\user_login\inputcat.php on line 9
The cat_id is a primary key with Auto incriment and No Nulls allowed and datatype INT(2)

Thanks

Geoff
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

The problem is that after PHP parses the query, when cat_id is null, you are trying to insert into the database the string 'null' which is NOT the same as NULL.

Remove the single quotes from the query for cat_id:

Code: Select all

INSERT INTO categories VALUES ($cat_id, '$category')
But... if $cat_id is always going to be null, why is it on your input form??

Have a read in the manual about Auto Increment columns.
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks that worked pefectly.

It was on the form as I didn't know how to insert it into the table, now I do :)

Geoff
Post Reply