Icebergness Posted June 7, 2012 Share Posted June 7, 2012 Hi, This one has been doing my head in for hours!. I have a script that, to put it simply, inserts either NULL or a string in to a MySQL database. However, it's acting rather weird with different values. Here is my SQL script... <?php $sql = "INSERT INTO stocks (s_isin) VALUES ('$isin')"; mysql_query($sql); ?> And then the variables being fed in to it are... <?php if ($user_isin_dummy == "1") // ISIN is Dummy { $isin = "NULL"; } else // ISIN is live { $isin = $user_isin; } ?> In it's current state, the "else" clause works fine, but the "if" statement inserts the string "NULL" in to the database. If I remove the single quotes from "$isin" in the SQL code, the "else" works only if the string begins with a number. If it starts with a letter, it simply doesn't insert at all. However, the "if" now correctly inserts a null value. How can I make it so that a proper null value is inserted on the "if" statement, and also for any string to be inserted on the else clause? Cheers, Dave Quote Link to comment https://forums.phpfreaks.com/topic/263825-problems-inserting-null-into-database/ Share on other sites More sharing options...
requinix Posted June 7, 2012 Share Posted June 7, 2012 Remove the quotes from the general case, add them in the specific case. Quote Link to comment https://forums.phpfreaks.com/topic/263825-problems-inserting-null-into-database/#findComment-1351933 Share on other sites More sharing options...
Icebergness Posted June 7, 2012 Author Share Posted June 7, 2012 Remove the quotes from the general case, add them in the specific case. Hi Requinix, Please forgive me, but I don't understand what you mean? Where do I need to remove/add quotes? Cheers, Dave Quote Link to comment https://forums.phpfreaks.com/topic/263825-problems-inserting-null-into-database/#findComment-1351938 Share on other sites More sharing options...
requinix Posted June 7, 2012 Share Posted June 7, 2012 Your $sql assumes that there should be quotes around $isin (the general case). Remove those. $isin may need quotes around its value (the specific case). Add them if it should have them. As in $isin = "'" . $user_isin . "'"; But $user_isin is a string, right? Not a number? If it is a number then it shouldn't have quotes at all. Quote Link to comment https://forums.phpfreaks.com/topic/263825-problems-inserting-null-into-database/#findComment-1351983 Share on other sites More sharing options...
Icebergness Posted June 7, 2012 Author Share Posted June 7, 2012 Your $sql assumes that there should be quotes around $isin (the general case). Remove those. $isin may need quotes around its value (the specific case). Add them if it should have them. As in $isin = "'" . $user_isin . "'"; But $user_isin is a string, right? Not a number? If it is a number then it shouldn't have quotes at all. Hi Requinix, Thanks for clearing that up for me, makes a lot more sense to me now You understood me correctly, yes, and your suggestion worked perfectly, so thank you very much! You've saved me countless hours of smashing my head against a wall! Cheers, Dave Quote Link to comment https://forums.phpfreaks.com/topic/263825-problems-inserting-null-into-database/#findComment-1352015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.