Inserting data from mysql into a html form using PHP

From Hashmysql
(Redirected from Replacing data using PHP)
Jump to: navigation, search

This example has three files.

It assumes that you have created a database along the lines of Basic SQL Statements, the same as in PHP Tutorial. It is a follow on from Inserting data using PHP

First File

This is basic html but note how the id is given a default value 1. This is static here - the file that is called by this file will, however, do the same dynamically.

<form  action="mydbshow.php" method="post">
Enter id of record that you wish to change: 
<input type="text" name="id" value="1">
 <p><input type="submit" /></p>
</form> 

Second File

Forget about trying to send messages to a html form. Instead remember that PHP generates html. So what we do is generate a html form and weave in the data using value. We need the htmlspecialchars fuction if we have any quotations in any database fields because the html form uses quotes to bracket the string given to value.

<html>
 <head>
  <title>db select for amending</title>
 </head>
 <body>
 <?php
    $myuser = 'myuser';//change this
    $mypass = 'mypass';//change this
    $mydb = 'mydb';
    $link = mysql_connect('localhost',$myuser,$mypass);
    $id = $_POST['id'];
     
    // If there was an error report the error.
    if(!$link) {
     die('Could not connect to the database: ' . mysql_error());
    }
    
     
    $db = mysql_select_db('mydb',$link);
 
 
    // Report any error with DB selection here.
    if(!$db) {
     die ('Could not use the test db because: ' . mysql_error());
    }
   
   
    // Not that php uses  a full stop to add the two elements of the query string together
    $sql = "SELECT id,name FROM mytable where id=".$id;
    $rs = mysql_query($sql); // Run the Query 


    // The loop will only run once of course.    
    while($row=mysql_fetch_assoc($rs)) {
     $name = $row['name'] ;
    }
  $name=htmlspecialchars($name);
  echo $name;
//Here we use php code to generate a html form and while doing so insert the values of name and id
  echo "<form action=\"mydbrep.php\" method=\"post\">";
  echo "<p>name<input type=\"text\" name=\"name\" . value=\"".$name."\" /></p>";
  echo "id<input type=\"text\" name=\"id\" value=\"".$id."\" /></p>" ;
  echo "<p><input type=\"submit\" /></p>";
  ?>
  </form>
 </body>
</html>

Third File

And now we can amend the record selected. Note that Replace, as defined in mysql, will simply do an insert unless one of the fields is unique.

 <?php
 
    $id = $_POST['id']; 
    $name = $_POST['name']; 
    $myuser = 'myuser';//change this
    $mypass = 'mypass';//change this
    $mydb = 'mydb';
    $link = mysql_connect('localhost',$myuser,$mypass);
     
    // If there was an error report the error.
    if(!$link) {
     die('Sorry we have not been able to connect to the database: ' . mysql_error());
    }
    
     
    $db = mysql_select_db($mydb,$link);
 
 
    // Report any error with DB selection here.
    if(!$db) {
     die ('Could not use the  db because: ' . mysql_error());
    }
   
    echo "We have selected the database.. <br>";
   

    // Print out the contents of the rows line by line. 
    // you can switch assoc, with array if you don't know the names of the fields.    
   $query = "Replace INTO mytable (id , name)";
   $query .= "VALUES('$id','$name')";
   echo  $query ."<br>";
   if(!mysql_query($query, $link)){
    die ('Could not insert name into database because: ' . mysql_error());
   }

   //if we'v got this far we can assume that the record has been inserted
   print("successfully replaced  value in the mydb database!<br>"); 
  ?>