Solved

insert null into Mysql DB

Posted on 2011-09-26
3
248 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now