如何在php的sql数据库中存储数据

如何在php的sql数据库中存储数据

问题描述:

我已经使用php和jquery创建了注册表.我需要将输入详细信息存储到sql数据库.

I have created register form using php and jquery. I need to store input details to sql database.

我创建了config.php,并将其包含在index.php和Submit.php文件中.

I created config.php and included in index.php and submit.php file.

但是仍然没有存储在数据库中,我可以知道config.php代码中的错误是什么吗,有人可以帮助我吗?

But still didn't store in database, may i know what is my mistake in config.php code, can anyone please help me?

这是我的config.php代码:

This is my config.php code:

    <?php
// configuration
$username       = "root";
$password       = "";

// database connection

$conn = new PDO('mysql:host=localhost;dbname=crop', $username, $password);
$stmt = $conn->prepare("INSERT INTO crop VALUES (:one, :two, :three)");

// values to enter
$data = array(
    'one' => 'value one',
    'two' => 'value two',
    'three' => 'value tree'
);

if ($stmt->execute($data)) echo "Inserted successfully";
?>

那不是做到这一点的方法.您必须使用INSERT语句,而不是SELECT.

Thats not the way to do it. You have to use an INSERT statement, not SELECT.

尝试以下示例:

您必须根据数据库结构更改代码.在这里,我只是假设有一个名为table的表,并且有3个字段.

You have to change the code based on your database structure. In here i just assume there's a table called table and there are 3 fields.

<?php

$stmt = $con->prepare("INSERT INTO table VALUES (:one, :two, :three)");

// values to enter
$data = array(
    'one' => 'value one',
    'two' => 'value two',
    'three' => 'value tree'
);

if ($stmt->execute($data)) echo "Inserted successfully";