[英]Inserting a Blank Record When Inserting into data into phpmyadmin
i am relatively new to the php coding life and i need some help. 我对php编码生活比较陌生,我需要一些帮助。 when i am inserting data into my phpmyadmin account it inserts the record i would like but it also inserts just a blank record?
当我向phpmyadmin帐户中插入数据时,它会插入我想要的记录,但它还会插入空白记录? Suggestions?
建议? also new to this site if theres any gaps in coding they arent actually there.
如果实际上没有任何编码方面的空白,这对本网站也是新的。
<?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>
Try this: 尝试这个:
<?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>
As a word of advice, avoid mysql library, it's obsolete now. 作为建议,请避免使用mysql库,它现在已过时。 Use
mysqli
or pdo
. 使用
mysqli
或pdo
。
You are using MySQL database. 您正在使用MySQL数据库。 PhpMyAdmin is just a tool to manage the database.
PhpMyAdmin只是管理数据库的工具。
The easiest way to solve would be 最简单的解决方法是
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);
}
For what you said about your comment "it was inserting a 0
into the year category so i believe it to be that field and ill try that" 对于您对评论的评论, “它在年份类别中插入了
0
,因此我认为这是该字段,请尝试尝试”
If an input field is left blank, then yes, it will insert a blank entry. 如果输入字段留空,则是,它将插入一个空白条目。
That's because you're not checking for empty inputs. 那是因为您不检查空输入。
Use conditional statements like: 使用条件语句,例如:
if(empty($_POST['year'])){
die("You need to fill this.");
}
and wrap your code inside another conditional statement to avoid premature execution. 并将您的代码包装在另一个条件语句中,以避免过早执行。
if(isset($_POST['submit'])){
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.