Solved

insert null into Mysql DB

Posted on 2011-09-26
3
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

690 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