# Changing mysql to PDO

**URL:** <https://www.phphelp.com/t/changing-mysql-to-pdo/26993>\
**Category:** General Database (Oracle, SQL Server, etc)\
**Created:** [August 23, 2018, 11:31am UTC](https://www.phphelp.com/t/changing-mysql-to-pdo/26993 "2018-08-23T11:31:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nightmaster](https://www.phphelp.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@Nightmaster](https://www.phphelp.com/u/Nightmaster)\
**Post date:** [August 23, 2018, 11:31am UTC](https://www.phphelp.com/t/changing-mysql-to-pdo/26993/1 "2018-08-23T11:31:01Z")

</div>

Hey all,

I need to switch from mysqli to PDO lib, and what I managed to to is to connect to database and list existing entries, but when I’m trying to insert I’m having a problems.  
Here’s what I previously had in mysqli:

`$firstName = $db-&gt; real_escape_string($_POST['first_name']); $lastName = $db-&gt;real_escape_string($_POST['last_name']); $licnakarta = $db-&gt;real_escape_string($_POST['licnakarta']); $sql = "INSERT INTO subjects (first_name,last_name,licnakarta,kompanija,regbr,kategorija,brdokumenta,domacin,comment,potpis) VALUES('".$firstName."', '".$lastName."', '".$licnakarta ."', '".$kompanija ."', '".$regbr."', '".$dep."', '".$brdokumenta."', '".$dom."', '".$comment."', '".$potpis."')";`

This is processing when form input is sent, and I’m not sure if how to change real\_escape\_string, from what I understand I won’t need that at all as I’m using the prepare when running queries?  
In that case, how should I handle this variables and pass it to query? I tried to just leave $firstName = $\_POST[‘first\_name’] but it won’t insert anything to db.  
as for $sql, i changed it to:

```
$sql = 'INSERT INTO subjects (first_name,last_name,licnakarta,kompanija,regbr,kategorija,brdokumenta,domacin,comment,potpis) VALUES(:firstname, :lastname, :licnakarta, :kompanija, :regbr, :dep, :brdokumenta, :dom, :comment, :potpis)'; $success = $db-&gt;prepare($sql); $stmt-&gt;execute(['firstname' =&gt; $firstName, 'lastname' =&gt; $lastName, 'licnakarta' =&gt; $licnakarta, 'kompanija' =&gt; $kompanija, 'regbr' =&gt; $regbr, 'kategorija' =&gt; $dep, 'brdokumenta' =&gt; $brdokumenta, 'domacin' =&gt; $dom, 'comment' =&gt; $comment, 'potpis' =&gt; $potpis]);
```

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [August 23, 2018, 1:33pm UTC](https://www.phphelp.com/t/changing-mysql-to-pdo/26993/2 "2018-08-23T13:33:48Z")

</div>

```
<?php

$sql = 'INSERT INTO subjects (
			first_name,
			last_name,
			licnakarta,
			kompanija,
			regbr,
			kategorija,
			brdokumenta,
			domacin,
			comment,
			potpis) 
        VALUES(
        	:firstname, 
        	:lastname, 
        	:licnakarta, 
        	:kompanija, 
        	:regbr, 
        	:dep, 
        	:brdokumenta, 
        	:dom, 
        	:comment, 
        	:potpis)'; 

$success = $db->prepare($sql); 
$stmt->execute([
	'firstname' => $firstName, 
	'lastname' => $lastName, 
	'licnakarta' => $licnakarta, 
	'kompanija' => $kompanija, 
	'regbr' => $regbr, 
	'kategorija' => $dep, 
	'brdokumenta' => $brdokumenta, 
	'domacin' => $dom, 
	'comment' => $comment, 
	'potpis' => $potpis
	]);

```

So, after reformatting due to the sites inability to keep things nice…  
Drop the extra variables, they aren’t needed. Use the $\_POST values that they came in on.

I can’t read most of what you are doing, but if possible, I would consider redesigning your database, it looks like it’s not normalized as well.
