简体   繁体   中英

How to update values to table fields in MySql from html form?

I am trying to update a table in my DB for the past two days. But I am unable to get it to work. Somebody please help me. I could connect to my database, see my table fields perfectly. Posted values from the form could be read perfectly from the destined PHP file. MySQL query doesn't seen to return any error. But I dont understand why the values are not getting updated into the table.

//form.html

        <form name="account" action="test.php" method="post">
    <td align="left" valign="top" class="labelstyle" width="25%">First Name</td>
                                          <td align="left" valign="top" class="labeltextstyle" width="75%"><input type="text" name="fname" value="" /></td>
    <td align="left" valign="top" class="labelstyle" width="25%">Last Name</td>
                                          <td align="left" valign="top" class="labeltextstyle" width="75%"><input type="text" name="lname" value="" /></td>
    <td align="left" valign="top" class="labelstyle" width="25%">Email</td>
                                          <td align="left" valign="top" class="labeltextstyle" width="75%"><input type="text" name="email" value="" /></td>


    <td  align="left" valign="top" class="labeltextstyle"><input type="submit" name="submit" value="Save" /></td>

    </form>

// test.php
    <?php

    $dbhost = "localhost"; 
    $dbname = "test"; 
    $dbuser = ""; 
    $dbpass = "";

    mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); 
    mysql_select_db($dbname) or die(mysql_error()); 

    session_start();
    if(isset($_REQUEST['submit'])){

    // $query = "select * from form";
    // $result = mysql_query($query);
    // $numcolumn = mysql_num_fields($result);
    // for ( $i = 0; $i < $numcolumn; $i++ ) {
    // $columnnames = mysql_field_name($result, $i);
    // echo $columnnames;
    // }


    $fname = $_POST['fname']; 
    $lname = $_POST['lname'];  
    $email = $_POST['email'];

    echo $fname ;
    echo $lname ;
    echo $email ;

    $query = "update test set
               fname = $fname,
               lname = $lname,
               email = $email
               where 1 ";

    $result = mysql_query($query); 

    if ($query = 1) { 

    echo "IT WORKED"; 

    } else { 
    echo "DIDNT WORK";
    } 
    }else{
     echo "NOT SUBMITTED";
     }

?>

//form.html

<form name="account" action="test.php" method="post">
<td align="left" valign="top" class="labelstyle" width="25%">First Name</td>
                                      <td align="left" valign="top" class="labeltextstyle" width="75%"><input type="text" name="fname" value="" /></td>
<td align="left" valign="top" class="labelstyle" width="25%">Last Name</td>
                                      <td align="left" valign="top" class="labeltextstyle" width="75%"><input type="text" name="lname" value="" /></td>
<td align="left" valign="top" class="labelstyle" width="25%">Email</td>
                                      <td align="left" valign="top" class="labeltextstyle" width="75%"><input type="text" name="email" value="" /></td>


<td  align="left" valign="top" class="labeltextstyle"><input type="submit" name="submit" value="Save" /></td>

</form>

When i fill the form with values A, B and C and submit the form, I get the following output.

fnamelnameemailABCIT WORKED

please help me soon.

There is no id in the where clause to indicate which row must be updated. This will update all the rows in the table. It should look like this:

 $query = "update test set
               fname = $fname,
               lname = $lname,
               email = $email
               where id = 1";

I assume that a row is created beforehand so that you know which row to update. If not then rather use an INSERT statement should be used.

$query = "insert into test (fname,lname,email) VALUES ($fname,$lname,$email);

fname, lname and email are strings, which have to be escaped in the SQL query:

 $query = "update test set
           fname = '$fname',
           lname = '$lname',
           email = '$email'
           where 1";

You probably can leave the where 1 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM