表单不会将数据发送到mysql

时间:2014-01-15 19:41:01

标签: 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>

add.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";
}
?> 

知道如何让它发挥作用......

4 个答案:

答案 0 :(得分:1)

在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。

答案 1 :(得分:0)

这里有很多错过的拼写和错误..看看这个:

echo '<td><input type="int" name="acs" id="acs" placeholder="ACS"></td>'

$asc = mysql_real_escape_string($_POST['asc']);

所以你真的必须阅读你的代码,并检查你的拼写。

还有一个:

$dest = $row['dest'];

$std = $row['dest'];

这应该是这样的:

$dest = $row['dest'];
$std = $row['std'];

答案 2 :(得分:0)

在你的表单中添加隐藏字段     “&GT; 在你的add.php文件中获取你的数据 $ flightner = $ _ POST [ 'flightnr']; 然后将sql查询插入到..

    <input type="hidden" name="test" id="test" value="<?php echo $row['flightnr'];?>"/>        

嗨.. xzibiz 首先,你必须在特定的隐藏字段中存储值$ row ['flightnr']然后使用$ _POST ['test']来获取实际值

    $flightnr=$_POST['test'];       

错误原因...... php只读取由html输入字段发送的数据,这样你就可以使用php服务器变量$ _REQUEST ['html_input_field']

从输入的字段html中获取这些值

答案 3 :(得分:0)

我现在已经开始工作..我的新代码是:

$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>

add.php:

$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', '$dest', '$std', '$adt', '$delay', '$net', '$gros', '$core', '$acs', '$tot')";
$retval = mysql_query( $sql, $con );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($con);

?>