Solved

insert null into Mysql DB

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

15 Experts available now in Live!

Get 1:1 Help Now