gpt4 book ai didi

php - phpmyadmin插入数据时插入空白记录

转载 作者:行者123 更新时间:2023-11-29 13:22:00 33 4
gpt4 key购买 nike

我对 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>

最佳答案

试试这个:

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

关于php - phpmyadmin插入数据时插入空白记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20686505/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com