Just wondering why this isn't working, I want to insert data from submit form to database

时间:2015-08-07 01:42:36

标签: php mysql database sqlite pdo

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.

4 个答案:

答案 0 :(得分:2)

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.

答案 1 :(得分:1)

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));



?>

答案 2 :(得分:0)

在您的INSERT查询中,您错过了''所有variables,最后错过了结束时)

这是您的查询

$sqlinsert("INSERT INTO Cars_On_Network
(Make, Model,  Badge, Price, Transmission, P_Plate_Legal)
 VALUES 
('$make, $model, $badge, $price, $trans, $ppl'");

应该是

$sqlinsert("INSERT INTO Cars_On_Network 
(Make, Model,  Badge, Price, Transmission, P_Plate_Legal)
 VALUES
('$make', '$model', '$badge', '$price', '$trans', '$ppl')");

答案 3 :(得分:0)

谢谢大家,为了将来参考,我将其更改为:)

<?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();
}


?>