0

我有这个表格,它将动作发布到 test2.php。

     <form action="test2.php" method="post" name="add">
     <tr>
     IC NUMBER:<input name="defIc" value="" type="varchar" size="20" maxlength="20">
     NAME:<input name="defName" value="" type="varchar" size="60" maxlength="60">

    <input name="reset" type="reset" value="RESET">
    <input type="submit" name="add" value=" ADD GUARANTOR ">
    </form>

这是我的 test2.php 编码。在这个 test2.php 中,我结合了添加过程来将数据存储在“被告”表中。在 test2.php 中,我还包括了另一种需要回叫“被告 IC”和“IC 号码”的表格。

           <?php   
    if (isset($_POST['add'])) {
        $defIc = addslashes($_POST['defIc']);
        $defName = addslashes($_POST['defName']);
        include 'dbconnect.php';

        $query = "INSERT INTO defendant (defIc, defName) VALUES 
            ('$defIc', '$defName')";


        echo $result = mysql_query($query);

    if ($result)
    {
    if (isset($result['defIc']))
        $defIc = $result['defIc'];
    else
        $defIc = 0;
    include 'dbconnect.php';
    $query = "Select * from defendant where defIc = '".$defIc."'";

    $result = mysql_query($query1) or die('SQL error');

    $row = mysql_fetch_array($result);
            ?>  
        DEFENDANT IC:<input name="defIc" value="<?php echo $row['defIc']; ?>"       
            type="varchar" size="20" maxlength="20" />
            DEFENDANT NAME:<input name="defName" value="<?php echo $row['defName']; ?>"          
            type="varchar" size="20" maxlength="20" />
           " 
            <?php 
    }

         else
            echo 'Add failed';   
    }
    ?>

现在的问题是,当我尝试回调 test2.php 中的“defIc”时,它没有出现。

4

1 回答 1

0

碰巧的是,这段代码中的错误比最初假设的要多得多这需要做很多工作。

<?php
//Only include a connection once!
include_once ('dbconnect.php');  
//If the 'add' button was pressed. 
if (isset($_POST['add'])) {

    //What type of data is this? This is very insecure code.
    //Look at preg_replace or mysqli_real_eascape_string to sanitise data

    $defIc = addslashes($_POST['defIc']);
    $defName = addslashes($_POST['defName']);


    $query = "INSERT INTO defendant (defIc, defName) VALUES 
        ('$defIc', '$defName')";


    //Why did you echo the result?
    $result = mysql_query($query);

if ($result)
{
if (isset($result['defIc']))
    $defIc = $result['defIc'];
else
    $defIc = 0;
$query = "Select * from defendant where defIc = '$defIc'";

$result = mysql_query($query1) or die('SQL error');

$row = mysql_fetch_array($result);
        ?>  
    DEFENDANT IC:<input name="defIc" value="<?php echo $row['defIc']; ?>"       
        type="varchar" size="20" maxlength="20" />
        DEFENDANT NAME:<input name="defName" value="<?php echo $row['defName']; ?>"          
        type="varchar" size="20" maxlength="20" />
       " 
        <?php 
}

     else
        echo 'Add failed';   
}
?>

您包含了两次数据库连接。你不能做这个。

这是我迄今为止收集到的。我将更新任何进一步的发现。

于 2014-10-21T21:04:46.670 回答