Solved

PHP scripts to the mysqli library.

Posted on 2015-02-16
2
158 Views
Last Modified: 2015-02-16
Hi,

Im starting to convert my existing PHP scripts to the mysqli library.

Could someone please have a look at the PHP code below and suggest how to migrate it to use the new mysqli library.

Thank you.

M.

<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<p>Add New Record to a MySQL Database</p>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'letmein';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $emp_name = addslashes ($_POST['emp_name']);
   $emp_address = addslashes ($_POST['emp_address']);
}
else
{
   $emp_name = $_POST['emp_name'];
   $emp_address = $_POST['emp_address'];
}
$emp_salary = $_POST['emp_salary'];

$sql = "INSERT INTO employee ".
       "(emp_name,emp_address, emp_salary, join_date) ".
       "VALUES('$emp_name','$emp_address',$emp_salary, NOW())";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee Name</td>
<td><input name="emp_name" type="text" id="emp_name"></td>
</tr>
<tr>
<td width="100">Employee Address</td>
<td><input name="emp_address" type="text" id="emp_address"></td>
</tr>
<tr>
<td width="100">Employee Salary</td>
<td><input name="emp_salary" type="text" id="emp_salary"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

Open in new window

0
Comment
Question by:batesm0t3l
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40612063
We have an article here at E-E that maps the familiar but obsolete MySQL code to the currently supported MySQL extensions.  Please read it over and let me know if you still have any questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Also, you might want to reconsider that code that involves "magic quotes."  We don't use addslashes() any more - just the relevant escape functions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6630-Magic-Quotes-a-bad-idea-from-day-one.html
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40612194
Thanks for the points and thanks for using E-E.  Please post back if you get stuck on anything! ~Ray
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question