php - 表单不会将数据发送到mysql

标签 php mysql forms

我正在尝试编写一个表单,从 mysql 数据库获取一些数据,其余的我必须自己填写。问题是在 add.php 中插入 mysql 时无法从表单中获取数据。 我的代码是:

    <form action="add.php" method="post">
                <table border="0px" align="center" width="300px">

                <tr align="center">
                <td><h2>Flight</h2></td>
                <td><h2>Org</h2></td>
                <td><h2>Dest</h2></td>
                <td><h2>STD</h2></td>
                <td><h2>ATD</h2></td>
                <td><h2>Delay</h2></td>
                <td><h2>NET</h2></td>
                <td><h2>Gros</h2></td>
                <td><h2>Core Material</h2></td>
                <td><h2>ACS</h2></td>
                <td><h2>Total sorted</h2></td>

                </tr>
                <?php
$con = mysql_connect("localhost","db","pass") or die('Could not connect: ' .mysql_error());
mysql_select_db("db", $con) or die(mysql_error());
$q="SELECT flightnr, org, dest, std FROM flight";
$sql = mysql_query($q) or die("MySQL ERROR: ".mysql_error());
while($row = mysql_fetch_array($sql))
                    {
                        $flightnr2 = $row['flightnr'];
        $org2 = $row['org'];
        $dest2 = $row['dest'];
        $std2 = $row['std'];
                        }
                        ?>
                             <tr>
                             <td><input type="text" name="flightnr" value="<?php echo $flightnr2;?>" /></td>
                             <td><input type="text" name="org" id="org" value="<?php echo $org2;?>" /></td>
                             <td><input type="text" name="dest" id="dest" value="<?php echo $dest2;?>" /></td>
                             <td><input type="text" name="std" id="std" value="<?php echo $std2;?>" /></td>
                             <td><input type="time" name="adt" id="adt" placeholder="ATD"></td>
                             <td><input type="time" name="delay" id="delay" placeholder="Delay"></td>
                             <td><input type="int" name="net" id="net" placeholder="NET"></td>
                             <td><input type="int" name="gros" id="gros" placeholder="Gros"></td>
                             <td><input type="int" name="core" id="core" placeholder="Core Material"></td>
                             <td><input type="int" name="acs" id="acs" placeholder="ACS"></td>
                             <td><input type="int" name="tot" id="tot" placeholder="Total sorted"></td>
                             <td><input type="submit" value="Submit"></td>
                            </tr>
                        </table>
                        </form>

添加.php:

    <?php
    $con = mysql_connect("localhost","db","pass") or die('Could not connect: '  .mysql_error());
mysql_select_db("db", $con) or die(mysql_error());

$date = date("D-m-Y");
$flightnr = $_POST['flightnr'];
$org = $_POST['org'];
$dest = $_POST['dest'];
$std = $_POST['dest'];
$adt = mysql_real_escape_string($_POST['adt']);
$delay = mysql_real_escape_string($_POST['delay']);
$net = mysql_real_escape_string($_POST['net']);
$gros = mysql_real_escape_string($_POST['gros']);
$core = mysql_real_escape_string($_POST['core']);
$acs = mysql_real_escape_string($_POST['acs']);
$tot = mysql_real_escape_string($_POST['tot']);

$sql="INSERT INTO fly (date, flightnr, org, dest, std, adt, delay, net, gros, core, acs, tot)VALUES('$date', '$flightnr', '$org', '$std', '$adt', '$delay', '$net', '$gros', '$core', '$acs', '$tot')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?> 

知道如何让它工作......

最佳答案

在 add.php 中,您检查的是 $row 而不是 $_POST。 应该是:

if (isset($_POST['atd']) && isset($_POST['..']) && ... )
{
    $values = Array($_POST['atd'], $_POST['..'], ...);
    $values = array_map("mysql_real_escape_string", $values);
    $sql = "INSERT INTO fly (date, flightnr, org, dest, std, adt, delay, net, gros, core, asc, tot) VALUES ('" . implode(',', $values) . "');";
}

无论如何,您应该使用 PDO 或 MySQLi,因为 MySQL 已被弃用。

关于php - 表单不会将数据发送到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146680/

相关文章:

javascript - 如何制作像Wordpress这样的图片 uploader ?

php - 我怎样才能在加入的方式上加上条件?

php - codeigniter 中 undefined variable 查询

php - mysqli 准备好的插入语句不起作用

mysql - 仅当日期时间为空时才更新 mysqli

javascript - 使用 jQuery ajax 函数验证表单时未设置 $_POST 变量

php - 通知 : Trying to get property of non-object in with num_rows

php - Drupal 7 token 替换

mysql - VB.net mysql 插入不工作

c# - 使只读富文本框中的当前行可编辑