Another mysql to mysqli conversion issue

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 30, 2019   (RSS)

By gkornbluth - April 29, 2019

Hi All,

I’m back with another mysql to mysqli conversion issue and could use some help.

I was using this code to add a subscriber to an email_signup database:

mysql_query("INSERT INTO `{$TABLE_PREFIX}email_signup` SET
last_name = '".mysql_real_escape_string( $_REQUEST['last_name'] )."',
first_name = '".mysql_real_escape_string( $_REQUEST['first_name'] )."',
email = '".mysql_real_escape_string( $_REQUEST['email'] )."',
hidden = '1',
source = 'Web Site',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

And after playing around a bit, I came up with the code below, which seems to work, but throws the error: Warning: mysqli_insert_id() expects exactly 1 parameter, 0 given in /home3/mrqsygmy/public_html/email_signup.php on line 84.

I’m also concerned that I deleted the mysql_real_escape_string code, since I had no idea how to convert it to mysqli.

mysqli()->query("INSERT INTO {$TABLE_PREFIX}email_signup SET
last_name = '".( $_REQUEST['last_name'] )."',
first_name = '".( $_REQUEST['first_name'] )."',
email = '".( $_REQUEST['email'] )."',
hidden = '1',
source = 'Web Site',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysqli_error()) . "\n");
$recordNum = mysqli_insert_id();

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 daniel - April 29, 2019

Hi Jerry,

I believe you should be able to replace mysqli_insert_id() with mysqli()->insert_id;

As well, a suitable replacement for mysql_real_escape_string() is mysql_escape().

Let me know if that fixes the issue!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - April 30, 2019

Thanks Daniel,

Sorry for the delayed reply.

That worked perfectly.

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