Stopping entry array

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?

Thanks in advance for any help on this

It would help to see your code @coding_noobie

If you are creating one row per array entry then it shouldn’t make any difference if the array is empty.

Is the ‘name’ field a NOT NULL ?

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:

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',
$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

  1. NEW_USER,
  2. $this->ResponseObj->_responseDB->quoteSmart(json_encode($newdetails)),
  3. $ID

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” :smile:

Next time you can make a debug before you execute the code.


Sorry I’m really new to the and don’t understand what you’re saying?

You need

$sql = sprintf( 'UPDATE Members SET userDetails = %s WHERE siteID = %d', $this->ResponseObj->_responseDB->quoteSmart(json_encode($newdetails)), $ID )

Also, try to use PDO and prepared statements, it’s way easier and secure.

thanks, I’ll have a look at prepared statements too

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:

$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

Give us the code that does not work, not the working one.
We do not know who $currentUser is and lots of other details.

PS: try to avoid sprintf when you can. It will make your code a mess, after few lines you will not know the value of your variables.

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…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.