{"id":383,"date":"2018-01-15T04:46:55","date_gmt":"2018-01-15T04:46:55","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=383"},"modified":"2018-01-15T04:46:55","modified_gmt":"2018-01-15T04:46:55","slug":"php-update-data-in-mysql","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-update-data-in-mysql\/","title":{"rendered":"PHP Update Data in MySQL"},"content":{"rendered":"<p>The php update data is used to update existing data in the database.<\/p>\n<p>[php]&lt;?php<br \/>\n$servername = &quot;localhost&quot;;<br \/>\n$username = &quot;username&quot;;<br \/>\n$password = &quot;password&quot;;<br \/>\n$dbname = &quot;myDB&quot;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\n\/\/ Check connection<br \/>\nif ($conn-&gt;connect_error) {<br \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n} <\/p>\n<p>$sql = &quot;UPDATE MyGuests SET lastname=&#8217;Doe&#8217; WHERE id=2&quot;;<\/p>\n<p>if ($conn-&gt;query($sql) === TRUE) {<br \/>\n    echo &quot;Record updated successfully&quot;;<br \/>\n} else {<br \/>\n    echo &quot;Error updating record: &quot; . $conn-&gt;error;<br \/>\n}<\/p>\n<p>$conn-&gt;close();<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>We are using <code>$sql<\/code> statement to update the database. In the statement we have instructed php script to update <code>MyGuests <\/code>database and set the <code>lastname <\/code>field Doe WHERE id is 2.<\/p>\n<p><code>$sql = \"UPDATE MyGuests SET lastname='Doe' WHERE id=2\";<\/code><\/p>\n<p>If everything goes well, your database will be updated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The php update data is used to update existing data in the database. [php]&lt;?php $servername = &quot;localhost&quot;; $username = &quot;username&quot;; $password = &quot;password&quot;; $dbname =<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/383\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}