How to store data in sql database in php

谁说我不能喝 提交于 2019-12-13 07:33:19

问题


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

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

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

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";
?>

回答1:


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

Try the following example:

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



回答2:


That's not the way to get it done.

$stmt = $db->prepare("INSERT INTO ".$table." ('column1', 'column2') VALUES ('value1', 'value2');"); $stmt->execute();

But I suggest you bind the query first to avoid SQL injection.



来源:https://stackoverflow.com/questions/26210259/how-to-store-data-in-sql-database-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!