2016-06-28 50 views
-1

我想要做的是在数据库上创建一个按钮,点击时允许您编辑该行。这也要求用户能够看到该行,以便他们知道他们正在编辑什么。到目前为止,我的表格在每行上都有一个编辑按钮,点击后会将您带到合适的页面。例如,如果单击ID = 23的行上的编辑,您将被带到http://website.com/filepath/editForm.php?id=23如何使用我的数据库中的数据填充此php表单,然后才能更新/编辑它?

问题是,这些字段都是空白的,即使您自己填写它们,它们也不会更新表格。我将发布editform.php,并且如果请求了view.php。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
</form> 


<?php 
// Only process the form if $_POST isn't empty 
if (! empty($_POST)) { 

    // Connect to MySQL 
    $mysqli = new mysqli('localhost', 'user', 'mypass', 'dbname'); 

    // Check our connection 
    if ($mysqli->connect_error) { 
    die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error); 
    }     
        $ID=$_POST['ID'] ; 
        $RYP=$_POST['RYP'] ; 
        $SHE=$_POST['SHE'] ; 
        $SCO=$_POST['SCO'] ; 
        $CName=$_POST['CName'] ; 
        $Contact= $_POST['Contact'] ;     
        $Address=$_POST['Address'] ; 
        $City=$_POST['City'] ; 
        $State=$_POST['State'] ; 
        $Zip=$_POST['Zip'] ; 
        $Phone1=$_POST['Phone1'] ; 
        $Phone2=$_POST['Phone2'] ; 
        $EMail=$_POST['EMail'] ; 
        $Web=$_POST['Web'] ; 


     $sql = ("SELECT * FROM DEALERS WHERE 'ID' = $ID"); 

     $sql = ("UPDATE dealers WHERE `ID`='$ID'(RYP, SHE, SCO, CName,Contact,Address,City, State, Zip, Phone1, Phone2, EMail, Web) 
     VALUES ('$RYP','$SHE','$SCO','$CName','$Contact','$Address','$City', '$State','$Zip','$Phone1','$Phone2','$EMail','$Web')"); 


      } 
?> 

     <form method="post"> 
<table> 
<tr> 
     <td>RYP</td> 
     <td><input type="checkbox" name="author" value="<?php $row['RYP'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>SHE</td> 
     <td><input type="checkbox" name="author" value="<?php $row['SHE'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>SCO</td> 
     <td><input type="checkbox" name="author" value="<?php $row['SCO'] ?>" class="form-control"/></td> 
    </tr> 
<tr> 
     <td>Contact</td> 
     <td><input type="text" name="author" value="<?php $row['Contact'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Address</td> 
     <td><input type="text" name="name" value="<?php $row['Address'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>City</td> 
     <td><input type="text" name="copy" value="<?php $row['City'] ?>" class="form-control"/></td> 
    </tr> 

    <tr> 
     <td>State</td> 
     <td><input type="text" name="copy" value="<?php $row['State'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Zip</td> 
     <td><input type="text" name="copy" value="<?php $row['Zip'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Phone1</td> 
     <td><input type="text" name="copy" value="<?php $row['Phone1'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Phone2</td> 
     <td><input type="text" name="copy" value="<?php $row['Phone2'] ?>" class="form-control"/></td> 
    </tr><tr> 
     <td>EMail</td> 
     <td><input type="text" name="copy" value="<?php $row['EMail'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Web</td> 
     <td><input type="text" name="copy" value="<?php $row['Web'] ?>" class="form-control"/></td> 
    </tr> 
<tr> 
     <td>CName</td> 
     <td><input type="text" name="title" value="<?php $row['CName'] ?>"class="form-control"/></td> 
    </tr> 

    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="submit" value="submit" class="btn btn-success btn-lg"/></td> 
    </tr> 
</table> 
</form></body> 
+1

'value =“<?php $ row ['RYP']?>”'(从你的输入)你永远不会回显任何这些值,基本上要注意它们 - 所以当脚本加载时,输入始终会读为“value =”“'(空)。而且你从来没有真正运行任何这些查询!所以没有数据被提取 - 此外,看看['error_reporting(E_ALL);'](http://php.net/manual/en/function.error-reporting.php) ['ini_set('display_errors ',1);'](http://php.net/manual/en/function.ini-set.php),可能会产生很多来自未定义变量的警告。 – Qirel

+0

而且你有很多具有相同'name'属性的输入。 – Qirel

回答

-1

我喜欢做这样的想法与隐藏的ID字段。

<input type="hidden" name="id" value="<?php $row['id'] ?>"> 

如果你正在寻找通过URL来做到这一点,那么你将需要隐藏标识的

$id = $_GET['id']; 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
</form> 


<?php 
// Only process the form if $_POST isn't empty 
if (! empty($_POST)) { 

    // Connect to MySQL 
    $mysqli = new mysqli('localhost', 'user', 'mypass', 'dbname'); 

    // Check our connection 
    if ($mysqli->connect_error) { 
    die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error); 
    }     
        $ID=$_POST['ID'] ; 
        $RYP=$_POST['RYP'] ; 
        $SHE=$_POST['SHE'] ; 
        $SCO=$_POST['SCO'] ; 
        $CName=$_POST['CName'] ; 
        $Contact= $_POST['Contact'] ;     
        $Address=$_POST['Address'] ; 
        $City=$_POST['City'] ; 
        $State=$_POST['State'] ; 
        $Zip=$_POST['Zip'] ; 
        $Phone1=$_POST['Phone1'] ; 
        $Phone2=$_POST['Phone2'] ; 
        $EMail=$_POST['EMail'] ; 
        $Web=$_POST['Web'] ; 


     $sql = ("SELECT * FROM DEALERS WHERE 'ID' = $ID"); 

     $sql = ("UPDATE dealers WHERE `ID`='$ID'(RYP, SHE, SCO, CName,Contact,Address,City, State, Zip, Phone1, Phone2, EMail, Web) 
     VALUES ('$RYP','$SHE','$SCO','$CName','$Contact','$Address','$City', '$State','$Zip','$Phone1','$Phone2','$EMail','$Web')"); 


      } 
?> 

     <form method="post"> 
<table> 
<tr> 
     <td>RYP</td> 
     <td><input type="checkbox" name="author" value="<?php $row['RYP'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>SHE</td> 
     <td><input type="checkbox" name="author" value="<?php $row['SHE'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>SCO</td> 
     <td><input type="checkbox" name="author" value="<?php $row['SCO'] ?>" class="form-control"/></td> 
    </tr> 
<tr> 
     <td>Contact</td> 
     <td><input type="text" name="author" value="<?php $row['Contact'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Address</td> 
     <td><input type="text" name="name" value="<?php $row['Address'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>City</td> 
     <td><input type="text" name="copy" value="<?php $row['City'] ?>" class="form-control"/></td> 
    </tr> 

    <tr> 
     <td>State</td> 
     <td><input type="text" name="copy" value="<?php $row['State'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Zip</td> 
     <td><input type="text" name="copy" value="<?php $row['Zip'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Phone1</td> 
     <td><input type="text" name="copy" value="<?php $row['Phone1'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Phone2</td> 
     <td><input type="text" name="copy" value="<?php $row['Phone2'] ?>" class="form-control"/></td> 
    </tr><tr> 
     <td>EMail</td> 
     <td><input type="text" name="copy" value="<?php $row['EMail'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Web</td> 
     <td><input type="text" name="copy" value="<?php $row['Web'] ?>" class="form-control"/></td> 
    </tr> 
<tr> 
     <td>CName</td> 
     <td><input type="text" name="title" value="<?php $row['CName'] ?>"class="form-control"/></td> 
    </tr> 

    <input type="hidden" name="id" value="<?php $row['id'] ?>"> 

    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="submit" value="submit" class="btn btn-success btn-lg"/></td> 
    </tr> 
</table> 
</form></body> 

最后一个URL例如

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
</form> 


<?php 
// Only process the form if $_POST isn't empty 
if (! empty($_POST)) { 

    // Connect to MySQL 
    $mysqli = new mysqli('localhost', 'user', 'mypass', 'dbname'); 

    // Check our connection 
    if ($mysqli->connect_error) { 
    die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error); 
    }     
        $ID=$_POST['ID'] ; 
        $RYP=$_POST['RYP'] ; 
        $SHE=$_POST['SHE'] ; 
        $SCO=$_POST['SCO'] ; 
        $CName=$_POST['CName'] ; 
        $Contact= $_POST['Contact'] ;     
        $Address=$_POST['Address'] ; 
        $City=$_POST['City'] ; 
        $State=$_POST['State'] ; 
        $Zip=$_POST['Zip'] ; 
        $Phone1=$_POST['Phone1'] ; 
        $Phone2=$_POST['Phone2'] ; 
        $EMail=$_POST['EMail'] ; 
        $Web=$_POST['Web'] ; 

        $url_id = $_GET['id']; 


     $sql = ("SELECT * FROM DEALERS WHERE 'ID' = $url_id"); 

     $sql = ("UPDATE dealers WHERE `ID`='$ID'(RYP, SHE, SCO, CName,Contact,Address,City, State, Zip, Phone1, Phone2, EMail, Web) 
     VALUES ('$RYP','$SHE','$SCO','$CName','$Contact','$Address','$City', '$State','$Zip','$Phone1','$Phone2','$EMail','$Web')"); 


      } 
?> 

     <form method="post"> 
<table> 
<tr> 
     <td>RYP</td> 
     <td><input type="checkbox" name="author" value="<?php $row['RYP'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>SHE</td> 
     <td><input type="checkbox" name="author" value="<?php $row['SHE'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>SCO</td> 
     <td><input type="checkbox" name="author" value="<?php $row['SCO'] ?>" class="form-control"/></td> 
    </tr> 
<tr> 
     <td>Contact</td> 
     <td><input type="text" name="author" value="<?php $row['Contact'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Address</td> 
     <td><input type="text" name="name" value="<?php $row['Address'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>City</td> 
     <td><input type="text" name="copy" value="<?php $row['City'] ?>" class="form-control"/></td> 
    </tr> 

    <tr> 
     <td>State</td> 
     <td><input type="text" name="copy" value="<?php $row['State'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Zip</td> 
     <td><input type="text" name="copy" value="<?php $row['Zip'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Phone1</td> 
     <td><input type="text" name="copy" value="<?php $row['Phone1'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Phone2</td> 
     <td><input type="text" name="copy" value="<?php $row['Phone2'] ?>" class="form-control"/></td> 
    </tr><tr> 
     <td>EMail</td> 
     <td><input type="text" name="copy" value="<?php $row['EMail'] ?>" class="form-control"/></td> 
    </tr> 
    <tr> 
     <td>Web</td> 
     <td><input type="text" name="copy" value="<?php $row['Web'] ?>" class="form-control"/></td> 
    </tr> 
<tr> 
     <td>CName</td> 
     <td><input type="text" name="title" value="<?php $row['CName'] ?>"class="form-control"/></td> 
    </tr> 

    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="submit" value="submit" class="btn btn-success btn-lg"/></td> 
    </tr> 
</table> 
</form></body> 

我也会看看MYSQLI O r PDO它看起来你使用的是过时的,也许是不安全的方法

相关问题