问题
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