0

I am inserting some data with below code but not getting inserted. I have checked every thing by echo $_REQUEST data everything is fine in output but doesn't insert data

With this code. I am Grabbing the data from the form

$bname         =$_REQUEST['bname'];
$btype         =$_REQUEST['btype'];
$bemail        =$_REQUEST['bemail'];
$bwebsite      =$_REQUEST['bwebsite'];
$bphone        =$_REQUEST['bphone'];
$bpostal       =$_REQUEST['bpostal'];
$bcountry      =$_REQUEST['bcountry'];
$bannertype    =$_REQUEST['bannertype'];
$bgcolor       =$_REQUEST['bgcolor'];
$bheadcolor    =$_REQUEST['bheadcolor'];
$bsubheadcolor =$_REQUEST['bsubheadcolor'];
$btextcolor    =$_REQUEST['btextcolor'];

It gets echo easily with this

echo "$bname, $btype, $bemail, $bwebsite, $bphone, $bpostal, $bcountry, $bannertype,  
$bgcolor, $bheadcolor,$bsubheadcolor,$btextcolor";

but when it comes to insertion doesnt work gives error

include 'dbconnect.php';

$sql="insert into company (business_id, business_name, business_type, bunsiness_email,

business_website, work_phone,  postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('NULL','".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')";

mysql_query($sql) or die("An Error Occured while updating");
include 'dbclose.php';*/

this is my table description

+----------------------------+---------------+------+-----+---------+-----------
-----+
| Field                      | Type          | Null | Key | Default | Extra
 |
+----------------------------+---------------+------+-----+---------+-----------
-----+
| business_id                | int(10)       | NO   | PRI | NULL    | auto_increment |
| business_name              | varchar(50)   | NO   |     | NULL    ||
| business_type              | varchar(50)   | NO   |     | NULL    |      |
| business_email             | varchar(50)   | NO   |     | NULL    |
| business_website           | varchar(50)   | NO   |     | NULL    |
| work_phone                 | varchar(20)   | NO   |     | NULL    |
| postal_code                | varchar(20)   | NO   |     | NULL    |
| business_country           | varchar(20)   | NO   |     | NULL    |
| banner_type                | varchar(50)   | NO   |     | NULL    |
| select_bgcolor             | varchar(50)   | NO   |     | NULL    |
| select_heading1            | varchar(50)   | NO   |     | NULL    |
| select_heading2            | varchar(50)   | NO   |     | NULL    |
| select_text                | varchar(50)   | NO   |     | NULL    |
Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86
Bandayar
  • 164
  • 1
  • 2
  • 11
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – John Conde Feb 11 '13 at 12:44
  • Output the error using `mysql_error()`. – Sirko Feb 11 '13 at 12:44
  • 2
    Also, you are wide open to [SQL injections](http://stackoverflow.com/q/60174) – John Conde Feb 11 '13 at 12:44
  • what is the error please show in question –  Feb 11 '13 at 12:44
  • first, it would be a smart idea to die and echo the `mysql_error()` at the same time. Next santitize your inputs! if someone entered a ' as input you get bobby tables. – Najzero Feb 11 '13 at 12:46
  • Update line of your code mysql_query($sql) or die("An Error Occured while updating"); with mysql_query($sql) or die("An Error Occured while updating
    ".mysql_error()); and post the output here and also echo your sql and paste here
    – Minesh Feb 11 '13 at 12:47
  • `NULL` should not be quoted. If you quote it, it will be the string `'NULL'` instead. – Joachim Isaksson Feb 11 '13 at 12:49
  • Thanx @Sirko i echo the Mysql_error and got my mistake thanx again answer it so i may accept it – Bandayar Feb 11 '13 at 12:50
  • @KhanKoder Accept one of the answers below, which fits your problem. – Sirko Feb 11 '13 at 12:52

6 Answers6

1

You are trying to insert NULL value into business_id column. You can't do that because this column cannot be null (because it's the primary key)

Please try to use: (I have deleted the insert to business_id column)

$sql="insert into company (business_name, business_type, bunsiness_email,

business_website, work_phone,  postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')";
Stasel
  • 1,298
  • 1
  • 13
  • 26
1

bunsiness_email should be business_email in your insert, that will outright break it because the column bunsiness_email doesn't exist. Nthing learning about prepared statements and because here you have a scenario where you will be looking after many open and close single and double quotaion marks, and prepared statements makes handling that a whole lot easier + more secure against SQL injection.

Paul Stanley
  • 4,018
  • 6
  • 35
  • 56
  • Gotcha This One thanx @Octopi can u more explain how to get secure from injection – Bandayar Feb 11 '13 at 13:00
  • Booya, check out the tutorial that @John Conde posted in the first comment. There's an explanation of the SQL injection problem and how prepared statements solves it. – Paul Stanley Feb 11 '13 at 13:05
0

business_id will never be NULL. it is auto incremented field.

$sql="insert into company (business_name, business_type, bunsiness_email,

business_website, work_phone,  postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')";
Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
0

Since business_id is an INT AUTO INCREMENT column, you don't need it in INSERT query.

    $sql = "insert into company (
    business_name, 
    business_type, 
    business_email,
    business_website, 
    work_phone,
    postal_code,
    business_country,
    banner_type, 
    select_bgcolor, 
    select_heading1, 
    select_heading2, 
    select_text
    ) values (
        '" . $bname . "',
        '" . $btype . "',
        '" . $bemail . "',
        '" . $bwebsite . "',
        '" . $bphone . "',
        '" . $bpostal . "', 
        '" . $bcountry . "',
        '" . $bannertype . "', 
        '" . $bgcolor . "', 
        '" . $bheadcolor . "', 
        '" . $bsubheadcolor . "', 
            '" . $btextcolor . "'
    )";

and if you ever want to pass NULL in query, do it without quotes.

Zagor23
  • 1,953
  • 12
  • 14
0

Your business_id is autoincrement primary key so as you are sending it as null in query it is giving error remove business_id from your query

$sql="insert into company ( business_name, business_type, bunsiness_email,

business_website, work_phone,  postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')";

mysql_query($sql) or die("An Error Occured while updating");
Meherzad
  • 8,433
  • 1
  • 30
  • 40
0

your business id is primary key.it is auto increment value.so while inserting business_id can not insert as NULL. So query will be:

$sql="insert into company (business_name, business_type, bunsiness_email,

business_website, work_phone,  postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')";
user2001117
  • 3,727
  • 1
  • 18
  • 18