| Computing desk | ||
|---|---|---|
| < April 29 | << Mar | April | May >> | May 1 > |
| Welcome to the Wikipedia Computing Reference Desk Archives |
|---|
| The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
I'm trying to insert a null value into a MySQL table but I keep getting all zeros for the date, i.e. 0000-00-00. I've tried single quotes around the Null, I've tried lower case null... What am I doing wrong?
if (isset($_POST['add_animal'])) { // /user hit submit
// check the NOT NULL columns to be sure they have a value.
// the next 5 post vars are NOT NULL so they should have a value
$name=$_POST['name'];
$species=$_POST['species'];
$birthdate = $_POST['birthdate'];
$color = $_POST['color'];
$breed = $_POST['breed'];
// For values that allow a NULL
$fixed_date = !empty($_POST['fixed_date']) ? $_POST['fixed_date'] : "NULL";
$query =
"INSERT INTO animal (name,species,birthdate,color,breed,fixed_date) VALUES
('$name','$species','$birthdate','$color','$breed','$fixed_date')";
// print $query;
$result = mysqli_query ($db, $query);
if (!$result)
print "ERROR: Add New Animal Failed on INSERT " . mysqli_error($db);
else {
print "<br />New Animal Added <br />";
print "<form>";
print "<button type='button' onclick=\"window.location='add_animal.php';\">Add Another Animal</button>";
print "<button type='button' onclick=\"window.location='animals.php';\">Display Animals</button><br>";
print "</form>";
}
}
Thanks, Dismas|(talk) 02:01, 30 April 2016 (UTC)
'NULL' in your INSERT statement, which MySQL may be converting to 0000-00-00. As Tevildo says, this way of constructing dynamic SQL statements is not best practice, especially without sanitising the inputs, but if you're going to do it then you would need to set $fixed_date to either (the string) NULL or (e.g.) '2016-05-01' (including the quotes) and not use the quotes in the $query = ... statement. AndrewWTaylor (talk) 10:15, 30 April 2016 (UTC)Thank you both. I got it figured out by using the following:
$fixed_date = !empty($_POST['fixed_date']) ? $_POST['fixed_date'] : 'null';
if (empty($_POST['fixed_date']))
$query =
"INSERT INTO animal (name,species,birthdate,color,breed) VALUES
('$name','$species','$birthdate','$color','$breed')";
else
$query =
"INSERT INTO animal (name,species,birthdate,color,breed,fixed_date) VALUES
('$name','$species','$birthdate','$color','$breed','$fixed_date')";
It's a bit lengthy but works. I'm not too worried about SQL injection as this is only a class project and not meant for a live environment. Also, it's a course on databases and not web dev, even though our final project is to slap a front end on the database. Thanks for responding though! Dismas|(talk) 11:56, 30 April 2016 (UTC)
I have a thought experiment that doesn't quite make sense to me, so I'm hoping to clarify it here:
So, the second scenario is a gain for the customer (and kid) who uses it, at the cost of those customers who would have been in front of them, but are now behind them, having to wait longer. (Let's not discuss the ethics of this, I'm just interested in the facts.)
My trouble comes in when I consider what happens if everyone uses the same queue reservation system. It seems like there should be no gain for anybody then, on average. However, I have difficulty conceptualizing how this works out to be the case. For example, if there would have been 10 customers in front with full baskets, if nobody used the queue reservation system, and now instead there are 10 kids holding places, when Mom is ready to check out, doesn't she still go right to the front of the queue ? I suppose there is some chance that other customers with earlier queue reservations will finish shopping right at the same time, so our customer will have to wait for them, too, but it seems implausible that all 10 would finish at once. So, would we end up with more place holders than we would have customers, without the queue reservation system ? I can see this when the business first opens, but a business that is open 24 hours a day doesn't seem like it would have this issue.
Any thoughts ? (This is posted on the Computer Desk because of the possible application to computer queues.) StuRat (talk) 17:00, 30 April 2016 (UTC)
00000000 <- store opens abcdefgh <- kids fill the queue while their mothers shop abcdeFgh <- mother of child f joins him in the queue Fabcdegh <- pair F is promoted to front of queue FabCdegh <- mother of child c joins him in the queue FaBCdegh <- mother of child b joins him in the queue aBCdegh0 <- pair F checks out and leaves queue BaCdegh0 <- pair B is promoted to the front of the queue
abcd <- original queue Cabd <- C is done shopping at TimeC, so it starts checking out CaBd <- B is done shopping at TimeB, nothing happens yet CABd <- A is done shopping at TimeA, which is the same time as TimeB effectively (difference is in granularity of checkout time) ABd <- C is finally done ABde <- E joins in BdE <- A is done, B starts checking out, and E gets done Ed <- B is done, E starts checkout, cause TimeE is shorter than TimeD
Once again online dezoomify has failed here. Firefox suggests they are separate unstitched images at full size, but I'm not sure. What software is good for extracting full-sized images from that Digital Scrolling Paintings Project? Thanks. Brandmeistertalk 19:22, 30 April 2016 (UTC)
Why does my CPU restarts automatically with the monitor turning black and showing no notification and sometimes with the freezing of the desktop as if it is paused and mouse keyboard or anything doesn't run at that time of freezing? I have Intel's 2.67 GHz processor with 2 GB RAM and 500 GB harddisk. — Preceding unsigned comment added by Sahil shrestha (talk • contribs) 05:00, 30 April 2016 (UTC)