Home / Wabcamasia / Updating mysql database with php

Updating mysql database with php

Seems as if it was just a weird test server thing in any case.You can run UPDATE statements in My SQL command-line, in a GUI tool or with mysqli_query() in a PHP script.

updating mysql database with php-35updating mysql database with php-7

---- ------------ ----------- ------------------------ -------- ------- | id | first_name | last_name | job_title | salary | notes | ---- ------------ ----------- ------------------------ -------- ------- | 1 | Robin | Jackman | Software Engineer | 5700 | NULL | | 2 | Taylor | Edward | Software Architect | 7200 | NULL | | 3 | Vivian | Dickens | Database Administrator | 6000 | NULL | | 4 | Harry | Clifford | Database Administrator | 6800 | NULL | | 5 | Eliza | Clifford | Software Engineer | 4750 | NULL | | 6 | Nancy | Newman | Software Engineer | 5100 | NULL | | 7 | Melinda | Clifford | Project Manager | 8500 | NULL | | 8 | Harley | Gilbert | Software Architect | 8000 | NULL | ---- ------------ ----------- ------------------------ -------- ------- Following query appends a note to the existing `notes` value after a period and a space.First of all, we connect to the database and select the appropriate record.$id=$_GET['id']; $username="username"; $password="password"; $database="your_database"; mysql_connect(localhost,$username,$password); $query=" SELECT * FROM contacts WHERE id='$id'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i As you can see, this code will output a standard form, but instead of having blank boxes like on the form for inserting a new record, this one already has the current information from the database inserted into it.This query could then be integrated into a simple script: $ud_id=$_POST['ud_id']; $ud_first=$_POST['ud_first']; $ud_last=$_POST['ud_last']; $ud_phone=$_POST['ud_phone']; $ud_mobile=$_POST['ud_mobile']; $ud_fax=$_POST['ud_fax']; $ud_email=$_POST['ud_email']; $ud_web=$_POST['ud_web']; $username="username"; $password="password"; $database="your_database"; mysql_connect(localhost,$username,$password); $query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone', mobile='$ud_mobile', fax='$ud_fax', email='$ud_email', web='$ud_web' WHERE id='$ud_id'"; mysql_query($query); echo "Record Updated"; mysql_close(); At this time it seems appropriate to mention another use of loops with a database.As well as using a loop to get information from a database as we have before, you can also use loops to execute queries.---- ------------ ----------- ------------------------ -------- ------- | id | first_name | last_name | job_title | salary | notes | ---- ------------ ----------- ------------------------ -------- ------- | 2 | Robin | Jackman | Software Engineer | 5500 | NULL | | 3 | Taylor | Edward | Software Architect | 7200 | NULL | | 4 | Vivian | Dickens | Database Administrator | 6000 | NULL | | 5 | Harry | Clifford | Database Administrator | 6800 | NULL | | 6 | Eliza | Clifford | Software Engineer | 4750 | NULL | | 7 | Nancy | Newman | Software Engineer | 5100 | NULL | | 8 | Melinda | Clifford | Project Manager | 8500 | NULL | | 9 | Harley | Gilbert | Software Architect | 8000 | NULL | ---- ------------ ----------- ------------------------ -------- ------- In above query, we increment all the IDs by one and it starts incrementing from the last row (8 becomes 9).If it didn't have this order and My SQL tried to do the increment from first row (making 1 into 2), it will throw an error since ID number 2 already exists (`id` has to be unique since it's the primary key).Using this you can then create the update script, which will actually have two sections to it.The first part of the update script uses the single record selection from last week but adds a little HTML to it to make it more useful.We recommend truncating the table and re-entering data after each example so that you can identify the change clearly (by comparing with original table shown above).Result sets listed in this article (except for amending values section) have been generated in that way and by executing SELECT statements (SELECT * FROM `employee`) in command-line.

272 comments

  1. Learn all the features of MySQL UPDATE statement with. UPDATE statement is used for updating existing. ?php /* Change database details according to.

Leave a Reply

Your email address will not be published. Required fields are marked *

*