I'm trying to send data from my submit form to the database, I'm using phpliteadmin as my database manager.
here is my php
<?php
if (isset($_POST['submitted'])) {
include('connect-phpliteadmin.php');
$make = $_POST['Make'];
$model = $_POST['Model'];
$badge = $_POST['Badge'];
$price = $_POST['Price'];
$trans = $_POST['Transmission'];
$ppl = $_POST['P_Plate_Legal'];
$sqlinsert("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make, $model, $badge, $price, $trans, $ppl'");
if (!mysqli_query($dbcon, $sqlinsert)) {
die("Error inserting new vehicle");
}
$newrecord = "New record added to the database";
}
echo "Make: " . $_POST['Make'] . "<br>";
echo "Model: " . $_POST['Model'] . "<br>";
echo "Badge: " . $_POST['Badge'] . "<br>";
echo "Price: " . $_POST['Price'] . "<br>";
echo "Transmission: " . $_POST['Transmission'] . "<br>";
echo "P Plate Legal: " . $_POST['P_Plate_Legal'] . "<br>";
try {
# Connect to SQLite database
$dbh = new PDO("sqlite:Car_Sales_Network");
# Prepare SQL statemen
#$sth = $dbh->prepare("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make, $model, $badge, $price, $trans, $ppl'");
# Run the query on the database
$sth->execute(array($make, $model));
?>
I'm not sure why this isn't working, I have followed a YouTube video and it didn't work. A fix would be greatly appreciated.
Cheers.
You SQL statement seems incorrect as far as I am aware.
Your query,
$sqlinsert("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make, $model, $badge, $price, $trans, $ppl'");
New query,
$sqlinsert("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make', '$model', '$badge', '$price', '$trans', '$ppl'");
As far as I'm aware you can't have ' '
for all the VALUES
.
It should be this:
FROM:
$sqlinsert("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make, $model, $badge, $price, $trans,$ppl'");`
TO:
$sqlinsert("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make', '$model', '$badge', '$price', '$trans','$ppl'");
<?php
if (isset($_POST['submitted'])) {
include('connect-phpliteadmin.php');
$make = $_POST['Make'];
$model = $_POST['Model'];
$badge = $_POST['Badge'];
$price = $_POST['Price'];
$trans = $_POST['Transmission'];
$ppl = $_POST['P_Plate_Legal'];
$sqlinsert("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make', '$model', '$badge', '$price', '$trans','$ppl'");
if (!mysqli_query($dbcon, $sqlinsert)) {
die("Error inserting new vehicle");
}
$newrecord = "New record added to the database";
}
echo "Make: " . $_POST['Make'] . "<br>";
echo "Model: " . $_POST['Model'] . "<br>";
echo "Badge: " . $_POST['Badge'] . "<br>";
echo "Price: " . $_POST['Price'] . "<br>";
echo "Transmission: " . $_POST['Transmission'] . "<br>";
echo "P Plate Legal: " . $_POST['P_Plate_Legal'] . "<br>";
try {
# Connect to SQLite database
$dbh = new PDO("sqlite:Car_Sales_Network");
# Prepare SQL statemen
#$sth = $dbh->prepare("INSERT INTO Cars_On_Network (Make, Model, Badge, Price, Transmission, P_Plate_Legal) VALUES ('$make', '$model' , '$badge', '$price', '$trans', '$ppl'");
# Run the query on the database
$sth->execute(array($make, $model));
?>
In your INSERT
query you missed ''
for all variables
and at end missed closing bracket )
This is your query
$sqlinsert("INSERT INTO Cars_On_Network
(Make, Model, Badge, Price, Transmission, P_Plate_Legal)
VALUES
('$make, $model, $badge, $price, $trans, $ppl'");
It should be like
$sqlinsert("INSERT INTO Cars_On_Network
(Make, Model, Badge, Price, Transmission, P_Plate_Legal)
VALUES
('$make', '$model', '$badge', '$price', '$trans', '$ppl')");
Thanks everyone, for future reference I changed it to this :)
<?php
try {
# Connect to SQLite database
$dbh = new PDO("sqlite:../Car_Sales_Network");
$make = $_POST['Make'];
$model = $_POST['Model'];
$badge = $_POST['Badge'];
$price = $_POST['Price'];
$trans = $_POST['Transmission'];
$ppl = $_POST['P_Plate_Legal'];
print_r($_POST);
$sth = $dbh->prepare('INSERT INTO Cars_On_Network
("car_make","car_model","car_badge","price","trans","P_Plate_Legal")
VALUES
(?, ?, ?, ?, ?, ?)');
$sth->execute(array($make, $model, $badge, $price, $trans, $ppl));
header("Location: ../Carsales_Network.php");
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>