将数据插入phpmyadmin时插入空白记录

时间:2013-12-19 16:15:51

标签: php mysql sql insert

我对php编码生活相对较新,我需要一些帮助。当我将数据插入我的phpmyadmin帐户时,它会插入我想要的记录,但它也只插入一个空白记录?建议?对于这个网站来说也是新手,如果在编码方面存在任何差距,他们实际上并不存在。

<?php

$host="xxxxx";
$username="xxxxx";
$password="xxxxx";
$db_name="xxxxx";
$tbl_name="LimitlessInventory";

mysql_connect("$host", "$username", "$password")or die("Cannot Connect");
mysql_select_db("$db_name")or die("Cannot Select Database");

$year=$_POST['year'];
$make=$_POST['make'];
$model=$_POST['model'];
$price=$_POST['price'];
$description=$_POST['description'];
$buyme=$_POST['buyme'];


echo "$year";
echo "$make";
echo "$model";
echo "$price";
echo "$description";
echo "$buyme";

$sql="INSERT INTO $tbl_name(year, make, model, price, description, buyme)VALUES('$year', '$make', '$model', '$price', '$description', '$buyme')";
$result=mysql_query($sql);

mysql_close();

?>
<html>
<body>
<title>Limitless Auto</title>
<form action="LimitlessInsert2.php" method="POST">
Year: <input type="text" size="4" maxlength="4" name="year" value="Ex.2012"><br />
Make: <input type="text" size="12" maxlength="12" name="make" value="Ex.Chevrolet"><br />
Model: <input type="text" size="12" maxlength="12" name="model" value="Ex.Corvette"><br />
Price: <input type="text" size="9" maxlength="9" name="price" value="Ex.$15,999.00"><br />
Description: <input type="text" size="75" maxlength="255" name="description"         value="Ex.2000 Miles 5.7L V8 Red"><br />
Link: <input type="text" size="255" maxlength="255" name="buyme" value="<a     href=http://solemnprophecy.com/DAT201/LimitlessBuyNow.php>Buy_ME!</a>"><br />
<input type="submit" name="submit">
</form>

<a href="http://solemnprophecy.com/DAT201/LimitlessInventory.php">Inventory Page</a>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

试试这个:

<?php

// Db Connection Details
$host="xxx";
$username="xxx";
$password="xxx";
$db_name="xxx";
$tbl_name="LimitlessInventory";

// Handle Post
if (isset($_POST['cmd']) && $_POST['cmd'] == 'Add')
{
    // Extract Post Vars
    extract($_POST);

    // Connect To DB
    mysql_connect($host, $username, $password) or die("Cannot Connect");
    mysql_select_db($db_name) or die("Cannot Select Database");

    // Insert Row
    mysql_query(sprintf("INSERT INTO $tbl_name(year, make, model, price, description, buyme) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
        mysql_real_escape_string($year),
        mysql_real_escape_string($make),
        mysql_real_escape_string($model),
        mysql_real_escape_string($price),
        mysql_real_escape_string($description),
        mysql_real_escape_string($buyme))) or die('Failed to insert row - '. mysql_error());

    // Close DB
    mysql_close();

    // Success
    echo "Data added";
}

?>
<form action="" method="POST">
    Year: <input type="text" size="4" maxlength="4" name="year" value="Ex.2012"><br />
    Make: <input type="text" size="12" maxlength="12" name="make" value="Ex.Chevrolet"><br />
    Model: <input type="text" size="12" maxlength="12" name="model" value="Ex.Corvette"><br />
    Price: <input type="text" size="9" maxlength="9" name="price" value="Ex.$15,999.00"><br />
    Description: <input type="text" size="75" maxlength="255" name="description" value="Ex.2000 Miles 5.7L V8 Red"><br />
    Link: <input type="text" size="255" maxlength="255" name="buyme" value='<a href=http://solemnprophecy.com/DAT201/LimitlessBuyNow.php>Buy_ME!</a>'><br />
    <input type="submit" name="cmd" value="Add">
</form>

作为建议,请避免使用mysql库,现在它已经过时了。使用mysqlipdo

答案 1 :(得分:0)

您正在使用MySQL数据库。 PhpMyAdmin只是管理数据库的工具。

最简单的解决方法是

if (isset($_POST)) {
    $sql="INSERT INTO $tbl_name(year, make, model, price, description, buyme)VALUES('$year', '$make', '$model', '$price', '$description', '$buyme')";
    $result=mysql_query($sql);
}

答案 2 :(得分:0)

关于你对comment “所说的内容,它正在将0插入年份类别中,因此我认为它是该字段并生病了”

如果输入字段留空,则是,它将插入空白条目。

那是因为你没有检查空输入。

使用条件语句,如:

if(empty($_POST['year'])){ 
die("You need to fill this."); 
}

并将您的代码包装在另一个条件语句中,以避免过早执行。

if(isset($_POST['submit'])){
...
}