Jump to content

[SOLVED] Isset or empty or what?


Siggles

Recommended Posts

Hi,

 

I have a form and if the user does not fill out one of the fields it is still changing that field to a 0 and I want it to stay as NULL. Here is my code chopped down (the php bit)...

 

switch($_POST['opt2']) {
case "editprediction":  //SQL FOR EDITING PREDICTION IN DBASE

		$score=$_POST['score'];

		if (isset($_POST['score'])){
		mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'");
		}
		break;
}
?>

 

I understand that isset only checks that a varible exists which is why it is setting it to 0. There may be times in the form however when I actually want the user to put 0 in the form field. So I only want it to do the query when the form field has something in it (0 or any number) but not when it is left blank. Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/
Share on other sites

try this:

<?php
switch($_POST['opt2']) {
case "editprediction":  //SQL FOR EDITING PREDICTION IN DBASE

		$score=$_POST['score'];

		if (isset($_POST['score']) && !empty($_POST['score']) && is_numeric($_POST['score'])){
		mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'");
		}
		break;
}
?>

 

Regards ACE

try this:

<?php
switch($_POST['opt2']) {
case "editprediction":  //SQL FOR EDITING PREDICTION IN DBASE

		$score=$_POST['score'];

		if (isset($_POST['score']) && !empty($_POST['score']) && is_numeric($_POST['score'])){
		mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'");
		}
		break;
}
?>

 

Regards ACE

 

Thank you for your help, only it wont accept a 0 in the form and leaves it null. It works in that it leaves field null thogh when field is empty. halfway there! :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.