【问题标题】:add a new set of data and store into database添加一组新数据并存储到数据库中
【发布时间】:2014-10-12 06:09:49
【问题描述】:

无法找出问题所在...例如,我想添加 2 行数据...当我按下 + 按钮...我刚刚输入的数据将消失(看起来像刷新)...和数据库将只存储第二行数据...第一行不会存储。请帮我找出答案。谢谢~

<?php if($_POST['btnPlus1'])
    $_SESSION['count1'] += 1;
else if($_POST['btnMinus1'])
    $_SESSION['count1'] -= 1;

$AddEducationalQ = "INSERT INTO tbleducational(Id,University,Level,Specialization,Year) VALUES('".$_POST['txtStaffIc']."','".strtoupper($_POST['txtUniversity'])."','".strtoupper($_POST['sLevel'])."','".strtoupper($_POST['txtSpecialization'])."','".$_POST['txtYear']."')";
$AddEducationalResult = mysql_query($AddEducationalQ,$link); ?>


<tr>
    <td>
        <fieldset>
            <legend>Educational Background</legend>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td>
                        <?php 
                        for($tempfield = 1; $tempfield <= $_SESSION['count1']; ++$tempfield)
                        {?>
                            <fieldset>
                                <legend><?php echo $tempfield ?></legend>
                                    <table width="200" border="0">
                                        <tr>
                                            <td>University</td>
                                            <td>Level</td>
                                            <td>Specialization</td>
                                            <td>Year Graduated</td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <input type="text" name="txtUniversity" id="txtUniversity" /></td>
                                            <td>
                                                <select name="sLevel" id="sLevel">
                                                    <option></option>
                                                    <option>Diploma</option>
                                                    <option>Degree</option>
                                                    <option>Master</option>
                                                    <option>Doctor</option>              
                                                </select>
                                            </td>
                                            <td>
                                                <input type="text" name="txtSpecialization" id="txtSpecialization" />
                                            </td>
                                            <td>
                                                <input type="text" name="txtYear" id="txtYear" />
                                            </td>
                                        </tr>
                                    </table>
                            </fieldset>       
                            <?php
                        }?>
                    </td>
                </tr>
                <tr>
                    <td colspan="4" align="center"><input type="submit" name="btnPlus1" id="btnPlus1" value="+" />    <input type="submit" name="btnMinus1" id="btnMinus1" value="-" /></td>
                </tr>
                </table>
        </fieldset>
    </td>
</tr>

【问题讨论】:

  • 我缺少您的部分代码。你的表单元素在哪里?您的第一张桌子的开头在哪里?
  • 只是简单的
  • 你难怪它不工作..我会看看我能不能解决一些问题

标签: php database add fieldset


【解决方案1】:

好的,首先您必须将表单元素更改为:

<form method="post" action="filename.php" accept-charset="utf-8">

确保将 filename.php 更改为此代码的文件名。

现在你必须做出选择。您可以继续使用已弃用且极其不安全的 mysql_*!或者,您可以将代码升级到 PDO()

如果您希望继续使用 mysql_*,请将您的 PHP 部分更改为:

if(isset($_POST['txtStaffIc'], $_POST['txtUniversity'], $_POST['sLevel'], $_POST['txtSpecialization'], $_POST['txtYear'])){
    $AddEducationalQ = "INSERT INTO tbleducational(Id,University,Level,Specialization,Year) VALUES('".$_POST['txtStaffIc']."','".strtoupper($_POST['txtUniversity'])."','".strtoupper($_POST['sLevel'])."','".strtoupper($_POST['txtSpecialization'])."','".$_POST['txtYear']."')";

    mysql_query($AddEducationalQ,$link) or die(mysql_error());

    echo "Data succesfully added to database.";
}

如果您想升级到 PDO(),请将您的 PHP 部分更改为:

if(isset($_POST['txtStaffIc'], $_POST['txtUniversity'], $_POST['sLevel'], $_POST['txtSpecialization'], $_POST['txtYear'])){
    $AddEducationalQ = "INSERT INTO tbleducational(Id,University,Level,Specialization,Year) VALUES(':txtStaffIc',':txtUniversity',':sLevel',':txtSpecialization',':txtYear')";
    $prepare = $pdo->prepare($AddEducationalQ);
    $prepare->bindValue(":txtStaffIc",$_POST['txtStaffIc']);
    $prepare->bindValue(":txtUniversity",strtoupper($_POST['txtUniversity']));
    $prepare->bindValue(":sLevel",strtoupper($_POST['sLevel']));
    $prepare->bindValue(":txtSpecialization",strtoupper($_POST['txtSpecialization']));
    $prepare->bindValue(":txtYear",$_POST['txtYear']);

    if($prepare->execute()){
        echo "Data succesfully added to database.";
    } else {
        print_r($prepare->errorInfo());
    }
}

还要确保将数据库连接文件更改为:

<?php

$dbhost = ""; //Enter MySQL server host
$dbuser = ""; //Enter MySQL database user
$dbpass = ""; //Enter MySQL database pass
$dbname = ""; //Enter MySQL database name

$pdo = new PDO("mysql:host=".$dbhost.";dbname=". $dbname, $dbuser, $dbpass);

?>

最后,我应该补充一点,我在您的 PHP 代码中没有看到任何需要的数据库连接文件。我假设您根本没有发布此内容。

【讨论】:

  • 对不起,它不能工作......它只能存储第二组数据......第一组数据不会存储
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 2021-10-12
相关资源
最近更新 更多