PHP& MySQL - 未定义索引:尝试使用PHP进行小型查询时的VIN

时间:2018-06-11 11:24:42

标签: php html mysql

我正在使用一本书来学习一些PHP和MySQL,并且我试图将HTML表单中的内容介绍到mysql数据库中,但我并没有真正了解正在发生的事情。

HTML



<html>

<head>
<title>Joy of PHP</title>
</head>

<body>
<h1>Sam's Used Cars
</h1>
<form action="SubmitCarReader.php" method="post">
	VIN: <input name="VIN" type="text" /><br />
	<br />
	Make: <input name="Make" type="text" /><br />
	<br />
	Model: <input name="Model" type="text" /><br />
	<br />
	Price: <input name="Asking_Price" type="text" /><br />
	<br />
	<input name="Submit1" type="submit" value="submit" /><br />
	&nbsp;</form>
</body>

</html>
&#13;
&#13;
&#13;

PHP

&#13;
&#13;
<html>
<head>
<title>Car saved</title>
</head>
<?php

$VIN = $_POST['VIN'];
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Asking_Price'];

$query = "INSERT INTO Masini_table
(VIN, Make, Model,Asking_Price)
VALUES( 
'$VIN',
'$Make',
'$Model',
$Price
)";

echo($query);

$mysqli = new mysqli('localhost', 'root', '', 'masini');
if(mysqli_connect_errno()) {
		printf("connect failed", mysqli_connect_error());
		die();
}
echo("connected <br>");
$mysqli->select_db("masini");
echo("selected <br>");
if($result = $mysqli->query($query))
{
		echo("introduced $Make $Model <br>");
}
else 
{
echo("did not introduce $VIN  <br>");	
}
$mysqli->close();
?>

</body>
</html>
&#13;
&#13;
&#13; 我使用的是Xampp。

错误:

&#13;
&#13;
Notice: Undefined index: VIN in C:\xampp\htdocs\SubmitCarReader.php on line 7

Notice: Undefined index: Make in C:\xampp\htdocs\SubmitCarReader.php on line 8

Notice: Undefined index: Model in C:\xampp\htdocs\SubmitCarReader.php on line 9

Notice: Undefined index: Asking_Price in C:\xampp\htdocs\SubmitCarReader.php on line 10
INSERT INTO Masini_table (VIN, Make, Model,Asking_Price) VALUES( '', '', '', )connected 
&#13;
&#13;
&#13;

我也发现奇怪的是在查询中引入了$ Price而没有&#39;&#39;喜欢&#39; $ VIN&#39;通过示例,但我尝试了两种方式,它仍然无法正常工作。

我也是PHP和MySQL的初学者,所以如果我错过了重要的东西,那么向你解释它真的很不错。 感谢。

1 个答案:

答案 0 :(得分:0)

您好,如下所示更改您的代码

<?php
if($_POST['Submit1'])
{
$VIN = $_POST['VIN'];
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Asking_Price'];

$query = "INSERT INTO Masini_table
(VIN, Make, Model,Asking_Price)
VALUES( 
'$VIN',
'$Make',
'$Model',
$Price
)";

echo($query);
}

$mysqli = new mysqli('localhost', 'root', '', 'masini');
if(mysqli_connect_errno()) {
printf("connect failed", mysqli_connect_error());
 die();
}
echo("connected <br>");
$mysqli->select_db("masini");
echo("selected <br>");
if($result = $mysqli->query($query))
{
 echo("introduced $Make $Model <br>");
}
else 
{
echo("did not introduce $VIN  <br>");   
}
$mysqli->close();
?>