I’m doing my very first project with arrays. I’m sending an array to the database which is great and works okay, the problem is that it’s also entering a blank array too and I don’t know how to stop it so that it will only enter the array if the $_POST[‘name’] field isn’t blank?
I’m storing the array in the database as JSON so there’s no field for name - just the whole array.
The problem is that if somebody edits a user then the form to add a user is sent as well. I’ve been told I can’t stop the empty entry from sending too but that I should be able to stop it from processing if it’s blank. So if it’s got a blank entry then it will be skipped and the rest f the data will be added.
Here’s the code I’ve got:
<?php
if(count($_POST['newUser']) > 0) {
if($_POST['newUser']['name'] !="") {
$newUserDetails = array();
$newTableRows = json_decode(str_replace("\t", '', stripcslashes($_POST["detailFields"])), true);
foreach($newTableRows as $newTableRow) {
}
$sql = sprintf('SELECT dataFields FROM Members WHERE siteID = %d', NEW_USER, $ID) ;
$newUserDetails = json_decode($this->ResponseObj->_responseDB->getOne($sql), true) ;
$newdetails[] = $_POST['newUser'];
foreach($newUserDetails as $userDetails) {
$newdetails[] = $userDetails;
}
$sql = sprintf('UPDATE Members SET userDetails = %s WHERE siteID = %d',
NEW_USER,
$this->ResponseObj->_responseDB->quoteSmart(json_encode($newdetails)),
$ID) ;
$this->ResponseObj->_responseDB->query($sql) ;
}
}
?>
$sql = sprintf('UPDATE Members SET userDetails = %s WHERE siteID = %d',
NEW_USER,
$this->ResponseObj->_responseDB->quoteSmart(json_encode($newdetails)),
$ID)
I think you should get an error from sprintf - you need two arguments (%s and %d) but you give three
LE: looks like sprintf does not return an error, it just takes what you give.
So, your SQL will be “update userDetails = NEW_USER where siteID = json_encode”
Next time you can make a debug before you execute the code.
This might be slightly off topic but I’ve now come to a new problem.
I’m now adding a new clean 2d array to the database. The problem is I’m not sure how to get the results back.
When it was 3d array that wasn’t a problem but now I need to work out the field number to make sure I’m getting the right details.
This is what used when it was a 3d array:
<?php
$userResults[] = sprintf($userRowDetails, $user['Name'], $currentUser[$user['Name']], $counter, $user['Gender']);
?>
$user['Name'] // this is the field name (i.e. First Name)
$currentUser[$user['Name']] // this is the results from Name field (is Jane Doe)
This is what the 3d array looked like:
[1] => Array
(
[First Name] => Jane
[Last Name] => Doe
[Gender] => Female
)
and this is what the 2d array now looks like
[0] => Array
(
[First Name] => Array
(
[1] => Jane
[2] => Joe
[3] => Alan
[4] => Fran
)
[Last Name] => Array
(
[1] => Doe
[2] => Bloggs
[3] => Jones
[4] =>
)
[Gender] => Array
(
[1] => Female
[2] => Male
[3] => Male
[4] => Female
)
)
I just don’t know how to get the results from the 2d array
I don’t subscribe to that. This is merely personal preference. I read sprintf statements just fine. Though the way you have used it is a little convoluted…