?
Solved

insert null into Mysql DB

Posted on 2011-09-26
3
Medium Priority
?
292 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 1000 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 1000 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 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