Solved

insert null into Mysql DB

Posted on 2011-09-26
3
280 Views
Last Modified: 2012-05-12
I am trying to insert "NULL" where form values are empty/blank so why doesnt this work, on retrieval I get "0" for cost, sell and expenses

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("CWCCARS", $con);
$cost=$_POST['cost'];
$expenses=$_POST['expenses'];
$sell=$_POST['sell'];
$cost = !empty($cost) ? "'$cost'" : "NULL";
$expenses = !empty($expenses) ? "'$expenses'" : "NULL";
$sell = !empty($sell) ? "'$sell'" : "NULL";
$sql="INSERT INTO cars (registration, carmake, carmodel, year, cost, expenses, sell, selldate, seller, workdone, comments)
VALUES
('$_POST[registration]','$_POST[carmake]','$_POST[carmodel]','$_POST[year]','$cost','$expenses','$sell','$_POST[selldate]','$_POST[seller]','$_POST[workdone]','$_POST[comments]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close();
?>
0
Comment
Question by:cookers
3 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
ID: 36707580
try wih no single quotes while insert


$sql="INSERT INTO cars (registration, carmake, carmodel, year, cost, expenses, sell, selldate, seller, workdone, comments)
VALUES
('$_POST[registration]','$_POST[carmake]','$_POST[carmodel]','$_POST[year]',$cost,$expenses,$sell,'$_POST[selldate]','$_POST[seller]','$_POST[workdone]','$_POST[comments]')";
0
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 250 total points
ID: 36707590
remove single quote ,

try like this
$sql="INSERT INTO cars (registration, carmake, carmodel, year, cost, expenses, sell, selldate, seller, workdone, comments)
VALUES('$_POST[registration]','$_POST[carmake]','$_POST[carmodel]','$_POST[year]',$cost,$expenses,$sell,'$_POST[selldate]','$_POST[seller]','$_POST[workdone]','$_POST[comments]')";

Open in new window

0
 

Author Closing Comment

by:cookers
ID: 36708024
Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question