0

I'm using a book to learn some PHP and MySQL and I'm trying to introduce things from an HTML form to a mysql DB and I don't really get what is happening.

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>

PHP

<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>
I'm using Xampp.

Errors:

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 

I also find odd introducing $Price in the query without the '' like '$VIN' by example but I tried it both ways and it still does not work.

I'm also a beginner in PHP and MySQL so if I'm missing something important it would be really nice for you to explain it to me. Thanks.

Andrei Daniel
  • 171
  • 1
  • 12
  • 2
    As I see that your code has injection vulnerabilities https://www.w3schools.com/sql/sql_injection.asp I doubt this book is up to date. Look into prepared statements and look up some more recent tutorials. – Niels Jun 11 '18 at 11:27
  • Nah it's not. I will but right now I tried to get the basics with this. It's from 2012 I think. – Andrei Daniel Jun 11 '18 at 11:30
  • 1
    The undefined index error indicates that your `$_POST` array is empty, or missing these indexes. Add this to your PHP page: `print_r($_POST);` to see it's content in full. – Nic3500 Jun 11 '18 at 11:31
  • It returns "Array ( )". – Andrei Daniel Jun 11 '18 at 11:32
  • 1
    This hasn't much to do with basics, a PHP book from 2012 will have other conventions, code structure and as you see very outdated functions/practices. PHP as changed a lot since 2012 so I really recommend ignoring that book and looking up good recent tutorial. And if it's really only about the basics, I'd suggest you start at codecademy. – Niels Jun 11 '18 at 11:33
  • Then can you recommend me a book which would help me? – Andrei Daniel Jun 11 '18 at 11:34
  • use `if (isset($_POST['value']))` to check – Ahmed Sunny Jun 11 '18 at 11:34
  • Bizarre, I cut and past your HTML page and made SubmitCarReader.php to contain just `` and it works, I do get the values in $_POST. Detail I noticed, in your PHP page, you have no ``, but that is not causing the original issue... You do type in some values in the HTML form before submitting right? – Nic3500 Jun 11 '18 at 11:37
  • @Nic3500 so you still get no idea? – Andrei Daniel Jun 11 '18 at 11:37
  • @AhmedSunny so it does not work. doesn't print anything – Andrei Daniel Jun 11 '18 at 11:38
  • @AndreiDaniel: taken that the code you use is what is posted here, that you do type some test values in the form fields, no I do not understand. If your $_POST is empty, obviously nothing else will work. Read this: https://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission they do have some suggestions (like parameter enable_post_data_reading) and others... – Nic3500 Jun 11 '18 at 11:44

1 Answers1

0

Hi Change your code as like below

<?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();
?>
sree
  • 389
  • 5
  • 17