Fatal error when inserting record in a table

9 posts by 3 authors in: Forums > CMS Builder
Last Post: September 6, 2017   (RSS)

By gkornbluth - August 31, 2017

OK I’m stumped again (probably by something simple) ...

I'm using CMSB Version 3.10, and I’ve created a simple create a record form page which works to create a record in the ‘accounts’ table.

But doesn’t work when I try to create a record in a multi record test table called ‘testing’.

I get the error:
Fatal error: Call to undefined function testingTable() in /home3/ellescho/public_html/jazzonjstreet/testC.php on line 20 .

The only difference is the replacement of:

$recordNum = mysql_insert(accountsTable(), $colsToValues, true);



with

$recordNum = mysql_insert(testingTable(), $colsToValues, true);



Here’s the active code:

<?php
if (@$_POST['save']) {
      $colsToValues = array();
      $colsToValues['createdDate=']     = 'NOW()';
      $colsToValues['updatedDate=']     = 'NOW()';
      $colsToValues['createdByUserNum'] = 0;
      $colsToValues['updatedByUserNum'] = 0;
      $colsToValues['first_name']         = $_REQUEST['first_name'];
      $colsToValues['last_name']         = $_REQUEST['last_name'];
      $colsToValues['email']            = $_REQUEST['email'];
      $recordNum = mysql_insert(testingTable(), $colsToValues, true);
      }
?>

  <form method="post" action="">
  <input type="hidden" name="save" value="1" />
  <table border="0" cellspacing="0" cellpadding="2">
   <tr>
    <td>First Name</td>
    <td><input type="text" name="first_name" value="<?php echo htmlencode(@$_REQUEST['first_name']); ?>" size="50" /></td>
   </tr>
   <tr>
    <td>Last Name</td>
    <td><input type="text" name="last_name" value="<?php echo htmlencode(@$_REQUEST['last_name']); ?>" size="50" /></td>
   </tr>
   <tr>
    <td>Email</td>
    <td><input type="text" name="email" value="<?php echo htmlencode(@$_REQUEST['email']); ?>" size="50" /></td>
   </tr>

   <tr>
    <td colspan="2" align="center">
      <br/><input class="button" type="submit" name="submit" value="Submit&gt;&gt;" />
    </td>
   </tr>
  </table>
  </form>


Thanks,

Jerry Kornbluth
 

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Ryan - September 5, 2017

Hi Gerry,

Try updating your code to this.


<?php 
 if (@$_POST['save']) {
       $tablename   = 'testingTable';
       $colsToValues = array();
       $colsToValues['createdDate=']     = 'NOW()';
       $colsToValues['updatedDate=']     = 'NOW()';
       $colsToValues['createdByUserNum'] = 0;
       $colsToValues['updatedByUserNum'] = 0;
       $colsToValues['first_name']         = $_REQUEST['first_name'];
       $colsToValues['last_name']         = $_REQUEST['last_name'];
       $colsToValues['email']            = $_REQUEST['email'];
       $hideMissingFieldErrors = true;
       $newRecordNum = mysql_insert($tablename, $colsToValues, $hideMissingFieldErrors); 
       }
 ?>

You might want to look about escaping those form values before you add them to your database.

Ryan

By gkornbluth - September 5, 2017 - edited: September 5, 2017

Hi Ryan,

I gave your change a try but now I'm getting a different error:

MySQL Error: Table 'ellescho_jazzonjstreet.cmsb_testingTable' doesn't exist - in email_signupF.php on line 21 by mysql_insert() in /home3/ellescho/public_html/jazzonjstreet/cmsAdmin/lib/common.php on line 1217

Any ideas?

Thanks,

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Ryan - September 5, 2017

Hi Jerry, 

It looks like the table "testingTable" has not been setup or the name has been entered incorrectly. Can you confirm the name of the table you are trying to submit the data to?

Ryan

By gkornbluth - September 5, 2017

Minor confusion,

The actual table name was 'testing' (the 'Table' got carried over from the old code by mistake.

The code works now.

Thanks!

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - September 5, 2017

Hi Ryan,

One more question if you have a moment.

I've been using the code you updated as part of a 2 part opt in email signup database.

The first part is the insertion of a record in the 'email_signup' database (using the updated code you provided).

The second part is that an email is sent to the person signing up containing the following confirmation link:

http://jazzonjstreet.com/confirmed.php?submit=1&confirmed=1&email=jerry@jkwebdesigns.com

Then I've used the code below in confirmed.php, to update the record.

Any thoughts on how I'd change this to be PHP7 compatible like the insert record code you generously offered?

Thanks,

Jerry

  $where = "email='".mysql_escape($_REQUEST['email'])."'";
     
      if(mysql_select_count_from('test',$where)){ //check to ensure that email exists in the table
    $where.=" AND confirmed='1'";
    if(!mysql_select_count_from('email_signup',$where)){ //email exists and has not been confirmed yet
        $query = "UPDATE `{$TABLE_PREFIX}test` SET
              hidden           = '0',
              confirmed          = '1',
              updatedDate      = NOW()
               
             WHERE email = '".mysql_escape( $_REQUEST['email'] )."'";
        mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
        $userNum = mysql_insert_id();

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - September 5, 2017

Hi Jerry, 

Here's some (untested and) updated code for PHP 7 / mysqli that uses some of our cmsb database functions.

//
$emailExists        = mysql_count('test',         ['email' => $_REQUEST['email']]);
$emailConfirmed     = mysql_count('email_signup', ['email' => $_REQUEST['email'], 'confirmed' => 1]);
$userNum            = 0;
if ($emailExists && !$emailConfirmed) {
  $updateNum    = null;
  $updateWhere  = [ 'email' => $_REQUEST['email'] ];
  $colsToValues = [ 'hidden' => 0, 'confirmed' => '1', 'updatedDate=' => 'NOW()' ];
  mysql_update('test', $updateNum, $updateWhere, $colsToValues);
  $userNum = mysqli()->insert_id;
}

Let me know if it works for you.

Dave Edis - Senior Developer
interactivetools.com

By gkornbluth - September 6, 2017

Thank you Dave

I'll test it today and post back

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php