Jump to content

[SOLVED] Simple question about php


SocomNegotiator

Recommended Posts

Ok I have a submit button that takes the values entered and enters them into a database...pretty simple. Well if the quantity is not entered I want nothing to be put into the database under that field. However, it is putting a "0" into the field. I even have this code to try and prevent that...

 

if($amount = ""){
	$amount = "";
}

 

Also I have the structure of this field set to Null. I had it as Not Null before and the default was zero. I thought by changing that to Null would change it, but it did not. Any suggestions?

Link to comment
Share on other sites

First of all, your code will not work. It will always insert empty value, because you're doing an assignment in if() construct instead of comparison.

That's how it's supposed to be.

if($amount == ""){
      $amount = "";
   }

 

Second thing. How is the field in database defined. Is it INTEGER? Does it allow NULL values?

Link to comment
Share on other sites

If you use NULL in your query it should work.

 

UPDATE table SET column = NULL WHERE id = 1

 

Ok yes it is an Integer that is set to NULL. I tried $amount == "" and it did not work.

Ok that is for an update...now if I have an INSERT like this

 

$db->query("INSERT INTO guide_order (item_id, amount, user_id, priority) VALUES ( '$item_id', '$amount', '$user_id', '$num')") or die('Error, query failed');

 

Now you notice that I have $amount...could I change that to NULL? However, with that if the person enters in an amount I want it put the amount they entered into the database. So I can't use NULL here, because no matter if they left it blank or entered in something they would always get nothing.

Link to comment
Share on other sites

The simplest way. Note that I dropped '' around $amount in query

 

if($amount == "") $amount = "NULL";
$query = "INSERT INTO guide_order (item_id, amount, user_id, priority) VALUES ( '$item_id', $amount, '$user_id', '$num')";

Link to comment
Share on other sites

I would do this:

 

Set the field to allow NULL and set the default value to NULL. Then use this:

<?php
$data = array(
  'item_id' => $item_id,
  'user_id' => $user_id,
  'priority' => $num,
);
if(!empty($amount))
  $data['amount'] = $amount;
$sql = "INSERT INTO `guide_order` (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')";
$db->query($sql) or die('Error, query failed');
?>

Link to comment
Share on other sites

I would do this:

 

Set the field to allow NULL and set the default value to NULL. Then use this:

<?php
$data = array(
  'item_id' => $item_id,
  'user_id' => $user_id,
  'priority' => $num,
);
if(!empty($amount))
  $data['amount'] = $amount;
$sql = "INSERT INTO `guide_order` (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')";
$db->query($sql) or die('Error, query failed');
?>

 

Yeah I need to read up on the whole implode thing. I use arrays, but I don't use them as often as I should.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.