Başlangıç ​​PHP: Ben MYSQL veritabanına veri eklemek olamaz

7 Cevap php

Şu anda PHP öğrenme yaşıyorum ve ben tablo böyle kurulur "pumpl2" adında bir MySQL veritabanı veri eklemek çalışıyorum.

create table product
 ( productid int unsigned not null auto_increment primary key,
   price int(9) not null,
   value int(9) not null,
   description text
 );

Ben bir form var ve veritabanında form alanları eklemek istiyorum. İşte php dosyası gibi görünüyor budur.

<?php

// create short variable names
$price = $_POST['price'];
$value = $_POST['value'];
$description = $_POST['description'];

if (!$price || !$value || !$description) {
 echo "You have not entered all the required details.<br />"
  ."Please go back and try again.";
 exit;
}

@ $db = new mysqli('localhost', 'pumpl', '********', 'pumpl2');

if (mysqli_connect_errno()) {
 echo "Error: Could not connect to database. Please try again later.";
 exit;
}

$query = "insert into pumpl2 values
 ('".$price."', '".$value."', '".$description."')";
$result = $db->query($query);

if ($result) {
 echo  $db->affected_rows." product inserted into database.";
} else {
 echo "An error has occurred.  The item was not added.";
}

$db->close();

?>

Ben formu gönderdiğinizde, ben bir hata mesajı alıyorum "Bir hata oluştu. Madde eklenmiştir değildi."

Kimse sorunun ne olduğunu biliyor mu? Teşekkür ederiz!

7 Cevap

Bu size daha fazla bilgi vermelidir:

echo "An error has occurred: " . $db->error();

Sadece üç sütun eklemek ancak tabloda dört tanımlamıştır. Böylece açıkça sütunları isim var:

 INSERT INTO tableName (ColumnA, ColumnB, ColumnC) VALUES ('A', 'B', 'C')

Sen pumpl2 denilen tabloya eklemek için çalışıyoruz, ancak CREATE TABLE ifadesi product adında bir tablo oluşturdu.

Buna ek olarak, hem ZeissS noted, aşağıdaki göz önünde bulundurmanız gerekir:

CREATE TABLE product ( 
    productid int unsigned not null auto_increment primary key,
    price int(9) not null,
    value int(9) not null,
    description text
);
Query OK, 0 rows affected (0.09 sec)

INSERT INTO product VALUES (1, 1, 'test');
ERROR 1136 (21S01): Column count doesn't match value count at row 1

Bu hatayı çözmek için, açıkça sütunların listesini belirtmek gerekir:

INSERT INTO product (price, value, description) VALUES (1, 1, 'test');
Query OK, 1 row affected (0.03 sec)
$query = "insert into pumpl2.product (price, value, description) values('" .
         $db->read_escape_string($price) . "', '".
         $db->read_escape_string($value) . "', '" .
         $db->read_escape_string($description) . "')";
$result = $db->query($query);

Ve bir zorunlu XKCD karikatür:

Your query is wrong, you didn't have the columns specified. Try it with:

"INSERT INTO pumpl2 (price, value, description) VALUES ('".$price."', '".$value."', '".$description."')"

Bunun yanı sıra, veritabanı içine doğrudan girmek için $ _POST değerleri kullanmayın. Bu bir SQL Enjeksiyon için arayın. Ilk $ _POST veri mysql_real_escape_string kullanın, hatta daha iyi kullanılması tablolarını hazırlamıştır.

Bu birkaç nedeni olabilir.

Denemek

echo "Errormessage: ".$db->error;

Daha fazla bilgi almak için, neden Ekle işe yaramadı.

Sizin tablo denir products değil pumpl2. Ayrıca yapmanız gereken:

insert into product (price, value, description) values ( ...